基于perl-Net::SMTP发送邮件通知

Perl 环境安装

linux已经默认安装了 perl,我们可以通过以下命令来查看是否已安装

$ perl -v

This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi
Copyright 1987-2009, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
-v

This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi
Copyright 1987-2009, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

如果输出以上信息说明已安装,如果还未安装,接下来进行安装。

通过浏览器打开 http://www.perl.org/get.html。

下载适用于 Unix/Linux 的源码包。

下载 perl-5.x.y.tar.gz 文件后执行以下操作。

$ tar -xzf perl-5.x.y.tar.gz
$ cd perl-5.x.y
$ ./Configure -de
$ make
$ make test
$ make install

可完成安装接下来我们如果 perl -v 命令查看是否安装成功。


安装perl后默认安装Net::SMTP模块,但没有安装Net::SMTP_auth模块,需要另外安装

Net::SMTP_auth要用到Digest::HMAC_MD5和Authen::SASL

Net::SMTP_auth下载:https://metacpan.org/pod/Net::SMTP_auth

Digest::HMAC_MD5下载:https://metacpan.org/pod/Digest::HMAC_MD5

Authen::SASL下载:https://metacpan.org/pod/distribution/Authen-SASL/lib/Authen/SASL.pod

所有perl模块都可以在http://search.cpan.org找到下载,下载后解开安装包,安装方式都一样

perl Makefile.PL
make
make install

如上操作可完成安装

 

Perl 脚本编写

vim sendmail.pl

#!/usr/bin/perl -w  
use Net::SMTP_auth;
use strict;
my $mailhost = 'smtp.163.com';  #发送服务器
my $mailfrom = 'xxx@163.com';  #发送者邮箱
my @mailto   = ('123456@126.com'); #接受者 邮箱
my $subject  = 'nginx出现异常';
my $text = "亲爱的xx:\n 您好!77nginx出现异常,请及时查看!";
my $user   = 'xxx@163.com';  #发送者邮箱
my $passwd = '1234566';
&SendMail();

## Send notice mail  
sub SendMail() {
    my $smtp = Net::SMTP_auth->new( $mailhost, Timeout =>520, Debug => 1 )  or die "Error.\n";
       $smtp->auth( 'LOGIN', $user, $passwd );
       foreach my $mailto (@mailto) {
                $smtp->mail($mailfrom);
                $smtp->to($mailto);
                $smtp->data();
                $smtp->datasend("To: $mailto\n");
                $smtp->datasend("From:$mailfrom\n");
                $smtp->datasend("Subject: $subject\n");
                $smtp->datasend("\n");
                $smtp->datasend("$text\n\n");
                $smtp->dataend();
       }
       $smtp->quit;
}

添加可执行权限:chmod +x senmail.pl;

执行:perl sendmail.pl或./sendmail.pl

参考

http://www.runoob.com/perl/perl-environment.html

https://metacpan.org/pod/Net::SMTP_auth

https://metacpan.org/pod/Net::SMTP_auth

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值