perl使用Net::SMTP_auth模块发邮件的例子,供大家学习参考。
#!/usr/bin/perl
use strict;
use Net::SMTP_auth;
use Text::iconv;
use MIME::Base64;
use Data::Dumper;
my $mailhost = 'mail.163.com'; ###邮件服务器
my $mailfrom = 'test_1@163.com';
my @mail_account = ('test_2@163.com');
my $mailuser = 'test';
my $mailpasswd = '123,';
my $filename= '/home/dbbakup.log';
my $subject= 'dbbackup message';
open(MYFILE,"$filename") || die "$!";
my $text;
while (<MYFILE>){
$text .= $_."<br>";
}
close(MYFILE);
&sendmail($text,$subject);
sub SendMail() {
my $text = $_[0];
my $subject = $_[1];
my $smtp = Net::SMTP_auth->new( $mailhost, Timeout => 120, Debug => 0, Hello => 'localhost' );
$smtp->auth( 'LOGIN', $mailuser, $mailpasswd );
foreach my $mailto (@mail_account) {
$smtp->mail($mailfrom);
$smtp->to($mailto);
$smtp->data();
$smtp->datasend("Content-Type:text/html;charset=utf-8n");
$smtp->datasend("Content-Transfer-Encoding:base64n");
$smtp->datasend( "From:=?utf-8?B?" . encode_base64( 'JianKong', '' ) . "?= <$mailfrom> n" );
$smtp->datasend( "To:=?utf-8?B?" . encode_base64( 'JianKong', '' ) . "?= <$mailto> n" );
$smtp->datasend( "Subject:=?utf-8?B?" . encode_base64( $subject, '' ) . "?=nn" );
$smtp->datasend();
$smtp->datasend( encode_base64( $text, '' ) . " n" );
$smtp->dataend();
}
$smtp->quit;
}