基于perl-Net::SMTP发送邮件通知
Perl 环境安装linux已经默认安装了 perl,我们可以通过以下命令来查看是否已安装$ perl -vThis is perl, v5.10.1 (*) built for x86_64-linux-thread-multiCopyright 1987-2009, Larry WallPerl may be copied only under the terms of ei...
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
更多推荐
所有评论(0)