2014-12-08 22:52:16
来 源
中存储网
sendmail
跑一个脚本需要把生成的日志发送到它邮箱里,现在脚本里加的报警邮件是使用一个perl脚本来报的,想报一个附件,一看还不行,后从网上看了看找到一个 。

今天一个同事跑一个脚本需要把生成的日志发送到它邮箱里,现在脚本里加的报警邮件是使用 一个perl脚本来报的,想报一个附件,一看还不行,后从网上看了看找到一个
1.sendmail.pl
#!/usr/bin/perl
#file main.pl
use Net::SMTP;
my $mailhost = "mail.***.com"; # the smtp host 邮件服务器地址
my $mailfrom = '123@***.com'; # your email address 报警的邮箱
#my @mailto = ('fayland@gmail.com', 'not_four@hotmail.com'); # the recipient list
my @mailto = $ARGV[0]; # the recipient list
my $subject = $ARGV[1];
my $text = $ARGV[2];
open(FILE,$text) || die "Can not open list filen";
undef $/;
$text=<FILE>;
$smtp = Net::SMTP->new($mailhost, Hello => 'localhost', Timeout => 120, Debug => 1);
# anth login, type your user name and password here
$smtp->auth('user','pass');
foreach my $mailto (@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("To: $mailto
");
$smtp->datasend("From: $mailfrom
");
$smtp->datasend("Subject: $subject
");
$smtp->datasend("
");
# Send the message
$smtp->datasend("$text
");
# Send the termination string
$smtp->dataend();
}
$smtp->quit;

保存为sendmail.pl,然后赋予执行权限,脚本里可以写以下的

红色的字是报警邮件的主题
蓝色的:报警邮件发给谁
粉色的:当前报警的日期、时间

123.txt中可以写上邮件的内容
比如,索引数据报警,请技术查看!
./sendmail.pl 123@***.com `date +%Y年%m月%d日%H点%M分`什么什么报警 123.txt

2.使用sendmail发附件,需要使用uuencode命令,它是包含在sharutiles组建

先检测一下yum search sharutils
======================================================== Matched: sharutils ========================================================
sharutils.x86_64 : The GNU shar utilities for packaging and unpackaging shell archives.
安装:centos: yum -y install sharutils
命令的话请自己查看man帮助
file_path:附件的目录
file:发送的附件
引号中是邮件的主题
uuencode file_path file | mail -s "file" 123@***.com

例如:uuencode /tmp/123.tar 123.tar | mail -s "123的包

声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。