用户Perl写个脚本,使用Net::SMTP模块发邮件:
#!/usr/bin/perl -w
use warnings;
use utf8;
binmode(STDIN, ':encoding(utf8)');
binmode(STDOUT, ':encoding(utf8)');
binmode(STDERR, ':encoding(utf8)');
use JSON;
use MIME::Lite;
#use Net::SMTP_auth; #需要安装模块
use Net::SMTP;
use Data::Dumper;
use IO::Socket::SSL;
use Encode;
#use MIME::Base64;
###以腾讯企业邮箱为例###
#smtp邮件服务器和端口
my $smtpHost = 'smtp.exmail.qq.com';
my $smtpPort = '25';
my $sslPort = '465';
#smtp服务器认证用户名及授权码
# 授权码 设置 参考链接: http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256
my $username = '[email protected]';
my $password = 'xxxxx'; # 授权码
my $from = '[email protected]';
#获得邮件域名部分,用于连接的时候表名身份
my @hello = split /\@/,$from;
my ($REQ) = @ARGV; # 脚本的输入参数:json格式的字符串,包含 to(接收者邮箱地址),subject(邮箱标题),body(邮箱内容,可能是个网站链接)
my $result = "failed";
if (!$REQ) {
print $result;
exit;
}
#$json = JSON->new;
my $input_json = decode_json($REQ); #$json->decode($REQ);
if ($input_json->{obj} eq "mail" && length($input_json->{to})) {
my $datasend = "";
my $to = $input_json->{to};
my $subject = $input_json->{subject};
my $cc;
my $msg = MIME::Lite->new(
From => $from,
To => $to,
Cc => $cc,
Subject=> $subject,
Type =>'text/html;charset=UTF-8', #设置成html格式,链接地址 会自动配上超链接
Data => $input_json->{body},
#Type =>'multipart/related',
);
$msg->attr("Content-Type" => "text/html;charset=UTF-8");
$msg->attr("Content-Transfer-Encoding" => "7bit");
my $smtp = Net::SMTP->new($smtpHost, Hello => $hello[1], Timeout => 10, SSL => 1, Debug => 1,);#采用SSl方式, 开启Debug => 1,
if ($smtp && $smtp->auth($username, $password)) {
#发送邮件
$smtp->mail($from);
$smtp->to($to);
$smtp->data();
$smtp->datasend(Encode::encode( "utf8", $msg->as_string ));
$smtp->datasend("\r\n");
$smtp->dataend();
$smtp->quit();
$result = "success";
} else {
$result = "auth failed";
}
}
print $result;
exit;