作者:belltoy |
声明:可以转载, 转载时务必以超链接形式标明文章原始地址和作者信息及版权声明
网址:http://www.belltoy.net/send-mail-with-perl.html
网址:http://www.belltoy.net/send-mail-with-perl.html
在外地出差也搞什么邮件签到,真无聊~
前一段看欧锦赛,时差还没调整过来,早上起不来zzzzz
不过上有政策,下有对策嘛
就在服务器上写了一个perl脚本,用Net::SMTP发邮件,用crontab来定时跑,一下就解决了,哈哈
perl脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | #!/usr/bin/perl use Net::SMTP; use MIME::Base64; ################ # 自动签到脚本 # ################ $host = '10.182.131.153'; #SMTP服务器地址 ########################### # # $host: smtp服务器 # $auth: 邮件账户 # $password: 邮件账户密码 # $to: 要发送的目标 # $mail_body: 邮件内容 # ########################### sub send_mail { my($host, $auth, $password, $to, $mail_body) = @_; my $smtp = Net::SMTP->new( Host => $host, Hello => $host, Timeout => 30, Debug => 1 ); $smtp->auth(substr($auth, 0, index($auth, '@')), $password); $smtp->mail($auth); $smtp->to($to); $smtp->bcc($auth); $smtp->data(); $smtp->datasend("Content-Type:text/plain;charset=GB2312/n"); $smtp->datasend("Content-Transfer-Encoding:base64/n"); $smtp->datasend("From:$auth /n"); $smtp->datasend("To:$to /n"); $smtp->datasend("Subject:=?gb2312?B?".encode_base64($mail_body,'')."?=/n/n"); $smtp->datasend("/n"); $smtp->datasend(encode_base64($mail_body,'')." /n"); $smtp->dataend(); $smtp->quit; } #获取命令行参数 if(@ARGV < 1) { $conf_file = './mailusers.conf'; #默认配置文件 } else { $conf_file = $ARGV[0]; #获取配置文件名 } #打开配置文件和日志文件 open CONF_FILE, $conf_file or die "Open config file [$conf_file] failed! /n"; open LOG_FILE, '>>send.log' or die "Open send.log failed! $!/n"; while(<LOG_FILE>) { chomp; if($_ =~ /^#+/) { next; #跳过注释行 } @line = split //s+/, $_; if(@line != 4) { next; #跳过空行 } #发送邮件 send_mail($host, $line[0], $line[1], $line[2], $line[3]); print LOG_FILE "[" . localtime() . "] send_mail($host, $line[0], $line[1], $line[2], $line[3]); /n"; } close CONF_FILE; close LOG_FILE; |
自动发信的账户和目标都配在配置文件里,配置文件格式如下mails.conf:
#<auth> <password> <To> <mail body> zzq@nlgx.dmp 000000 qiandao@nlgx.dmp 签到
然后就是在 crontab 里配自动运行是时间了:
#分 时 日 月 星期 命令 41 8 * * 1-5 /usr/bin/perl /home/aig/.run/checkin.pl /home/aig/.run/mails.conf 58 13 * * 1-5 /usr/bin/perl /home/aig/.run/checkin.pl /home/aig/.run/mails.conf