{"id":658,"date":"2023-01-13T10:30:59","date_gmt":"2023-01-13T02:30:59","guid":{"rendered":"http:\/\/www.biocloudservice.com\/wordpress\/?p=658"},"modified":"2023-01-14T17:24:51","modified_gmt":"2023-01-14T09:24:51","slug":"perl%e5%8f%91email%ef%bc%9a%e8%83%bd%e5%a4%9f%e7%94%a8%e6%9d%a5%e5%8f%91%e9%99%84%e4%bb%b6%e7%9a%84gmail%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"http:\/\/www.biocloudservice.com\/wordpress\/?p=658","title":{"rendered":"perl\u53d1email\uff1a\u80fd\u591f\u7528\u6765\u53d1\u9644\u4ef6\u7684gmail\u4ee3\u7801"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>\n#!\/usr\/bin\/perl\nuse strict;\n#use warnings;\nuse utf8;\n\nuse Authen::SASL;\nuse MIME::Lite;\nuse Net::SMTP::SSL;\n\nsend_mail( 'test', 'xxx&lt;br&gt;&lt;img src=\"cid:testimg\"&gt;', \n    #mime_type =&gt; 'text\/html', \n    from =&gt; 'a@gmail.com', \n     to =&gt;  'c@163.com',\n\n    \n    host =&gt; 'smtp.gmail.com', \n    usr =&gt; 'a@gmail.com', \n\n    passwd =&gt; 'xxxxxx', #\n    port =&gt; 465, \n    #timeout =&gt; 30, \n    #debug =&gt; 0, \n\n    attach =&gt; &#91; \n        {\n            mime_type =&gt; 'image\/png', \n            id =&gt; 'testimg', \n            path =&gt; 'C:\\Users\\Administrator\\Desktop\\1.png', \n        },\n    ], \n);\n\nsub send_mail {\n    my ($subject, $message, %opt) = @_;\n\n    my $msg = make_mail_msg($subject, $message, %opt);\n\n    my $smtp = Net::SMTP::SSL-&gt;new(\n        $opt{host},\n        Port    =&gt; $opt{port} || 465,\n        Timeout =&gt; $opt{timeout} || 30,\n        Debug   =&gt; 1,\n    ) or die \"Net::SMTP::SSL create fail\\n\";\n\n    if($opt{usr} and $opt{passwd}){\n        $smtp-&gt;auth( $opt{usr}, $opt{passwd} ) || die \"smtp auth fail\";\n    };\n\n    $smtp-&gt;mail( $opt{from} );\n    $smtp-&gt;to( format_mail_addr($opt{to}) );\n    $smtp-&gt;cc( format_mail_addr($opt{cc}) ) if($opt{cc});\n    $smtp-&gt;bcc( format_mail_addr($opt{bcc}) ) if($opt{bcc});\n\n    $smtp-&gt;data();\n    $smtp-&gt;datasend( $msg-&gt;as_string );\n    $smtp-&gt;dataend();\n\n    $smtp-&gt;quit;\n} ## end sub send_mail\n\nsub make_mail_msg {\n    my ($subject, $message, %opt) = @_;\n\n    my $msg = MIME::Lite-&gt;new(\n        Subject =&gt; $subject,\n        Type    =&gt; 'multipart\/alternative', \n    );\n\n    my $body = MIME::Lite-&gt;new( \n        Type =&gt; $opt{mime_type} || 'text\/html', \n        Data =&gt; $message, );\n    $body-&gt;attr( 'content-type.charset' =&gt; $opt{charset} || 'UTF-8' );\n    $msg-&gt;attach($body);\n\n    for my $f (@{$opt{attach}}) {\n        $msg-&gt;attach(\n            Type =&gt; $f-&gt;{mime_type},\n            Id   =&gt; $f-&gt;{id},\n            Path =&gt; $f-&gt;{path},\n        );\n    } \n\n    return $msg;\n}\n\nsub format_mail_addr {\n    my ($m) = @_;\n    return ref($m) eq 'ARRAY' ? @$m : $m;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":""},"categories":[46],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/658"}],"collection":[{"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=658"}],"version-history":[{"count":6,"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/658\/revisions"}],"predecessor-version":[{"id":940,"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/658\/revisions\/940"}],"wp:attachment":[{"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=658"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}