[总结]Sendmail问题

    最近公司的maillist更新了,我之前用perl sendmail package不好用了,不知道为何会导致退信。

    该脚本是用的Net::SMTP和MIME::Base64两模块,挺好用的,文件内容用base64编码过的,可以带附件。

   sub sendMail { my ($to, $subject, $body, $attach, $from) = @_; my $CRLF = "/r/n"; my $Raw_Bond = "=======Boundary======="; my $Bond = "--=======Boundary======="; my @receivor = split /[,;]/, $to; my $smtp = Net::SMTP->new('135.251.124.94'); $smtp->mail($from); $smtp->recipient (@receivor); $smtp->data(); $smtp->datasend("Subject: $subject/n"); my $head = "MIME-Version: 1.0" . $CRLF . "Content-Type: multipart/mixed; boundary=/"$Raw_Bond/"" . $CRLF; $head .= "Content-Transfer-Encoding: base64" . $CRLF . $CRLF; $smtp->datasend($head); if ($body) { my $content_head = "Content-Type: text/plain;" . $CRLF . "Content-Transfer-Encoding: base64" . $CRLF . $CRLF; $smtp->datasend ("$Bond" . $CRLF); $smtp->datasend($content_head); $smtp->datasend(encode_base64($body, $CRLF)); } if ($attach) { my $tempAttach = $attach; $tempAttach =~ s/^.*//(.*)$/$1/g; $smtp->datasend($CRLF . $CRLF . $Bond . $CRLF); $smtp->datasend('Content-Type: application/octet-stream; name=' . "/"$tempAttach/"" . $CRLF); $smtp->datasend('Content-Transfer-Encoding: base64' . $CRLF); $smtp->datasend('Content-Disposition: attachment; filename=' . "/"$tempAttach/"" . $CRLF . $CRLF); open (ATTA, "< $attach") or die "Open file: $attach error, $!/n"; my $buffer; while (read (ATTA, $buffer, 10*57)) { $smtp->datasend(encode_base64($buffer, $CRLF)); } close (ATTA); } $smtp->dataend(); $smtp->quit(); }  

 

    改用MIME::Lite模块后就把问题解决了,舒坦。

 

   要注意的是两者的发送方写法不一样,上面例子中的$smtp->recipient (@receivor);  可以接受数组
   而在MIME::Lite中,就用$to就可以了,$to中的邮件名用逗号分开就可以了。

   sub SendBuildMail { my ($to, $subject, $body, $attach, $from) = @_; my $msg = MIME::Lite->new( From => $from, To => $to, Subject => $subject, Type => 'multipart/mixed' ); if ($body) { $msg->attach( Type => 'TEXT', Data => $body ); } if ($attach) { my $dirname = dirname($attach); my $filename = basename($attach); $msg->attach( Type => 'BINARY', Path => $dirname, Filename => $filename, Disposition => 'attachment' ); } $msg->send('smtp','135.251.123.221', Debug=>1 ); } 

 

   上面的方法发送的是text形式的邮件,发html的时候会有问题,需改成一下方式发送。 将html的内容进行base64加密,不然会出现”! “这样的乱码。

   sub SendDailyHtmlMail { my ($to, $subject, $body, $attach) = @_; my $msg = MIME::Lite->new( From => '[email protected]', To => $to, Subject => $subject, Encoding => 'base64', Type => 'text/html', Data => $body )or warn $!; if ($attach) { my $dirname = dirname($attach); my $filename = basename($attach); $msg->attach( Type => 'BINARY', Path => $dirname, Filename => $filename, Disposition => 'attachment' ); } $msg->send('smtp','135.251.123.221', Debug=>1 ); }

你可能感兴趣的:(html,加密,perl,buffer,Path,encoding)