use strict;
use warnings;
use Net::SMTP;
use Authen::SASL;
use MIME::Base64;
use Encode;
my $mailhost = "主机名"; # the smtp host
my $mailfrom = '你的email地址'; # your email address
my $hello = 'localhost';
my $sendName = '这个不知道有啥用'; #the sender name
my $sendPassword ='密码'; #the sender password
my $subject = "邮件标题"; #mail subject
#$subject = Encode::encode('utf8', $subject); #perl对文字的处理默认使用utf-8所以这里不再用utf-8编码
my $smtp; # the connet of mail server
sub new{
my $type = shift;
bless {}, $type;
}
sub sentMail{
$smtp = Net::SMTP->new($mailhost, Hello => $hello, Timeout => 120, Debug => 0);
# anth login, type your user name and password here
$smtp->auth($sendName,$sendPassword);
my($this,$hashCont)=(@_);
my $body = getcontent($hashCont);
my $mailto = $hashCont->{mailto};
# Send the From and Recipient for the mail servers that require it
$smtp->mail($mailfrom);
$smtp->to($mailto);
# Start the mail
$smtp->data();
# Send the header
$smtp->datasend("From: $mailfrom\n");
$smtp->datasend("To: $mailto\n");
$smtp->datasend("Content-Type:text/plain;charset=UTF-8\n"); #这里设置邮件内容的编码,即用户的客户端解码方式
$smtp->datasend("Subject:=?UTF-8?B?".encode_base64($subject, '')."?=\n\n"); #encode_base64是MIME::Base64模块中的函数,$title为UTF-8编码过的.用这个函数保证标题不乱码
$smtp->datasend($body."\n\n"); #$body为UTF-8 编码过的,perl默认已经编成utf8了,这里不用再编一次
# $smtp->datasend("Subject:=?utf-8?B?".$subject."?=\n");
# $smtp->datasend("Subject: $subject\n");
# $smtp->datasend("\n");
# $smtp->datasend($body."\n\n");
# $smtp->datasend(Encode::decode( "utf8", $body)."\n\n");
$smtp->dataend();
$smtp->quit;
}
sub smtpQuit{
$smtp->quit;
}
转载于:https://blog.51cto.com/716378/584665