Perl之Net::SMTP

用户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 = 'xx@qq.com';

my $password = 'xxxxx';  # 授权码

my $from = 'xx@qq.com';

 

#获得邮件域名部分,用于连接的时候表名身份

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;

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值