本文共 1852 字,大约阅读时间需要 6 分钟。
#! /usr/bin/perluse strict;use POSIX;use DBI;use Net::SMTP;use Date::Parse;my $start = strftime("%Y%m%d%H%M%S", localtime(time()));print "[$start]Start Execute Day Report/n";# make file that will be sendedmy $file1 = "test1.txt";my $file2 = "test2.txt";my $tmp_file = ".tmp";system("cat $file1 $file2 > $tmp_file");open(FILE, $tmp_file) || die "Cannot open $tmp_file";my @lines = <FILE>;my $send_html = "<html><div align='left' style="font-size:12px" >";for (my $i = 0; $i < @lines; $i++) { $send_html .= $lines[$i]."<br>";}$send_html .= "</div></html>";# send mailmy $smtp_host = "your smtp server"; # 你的smtp服务器my @recipients = ('test1@domain.com', 'test2@domain.com'); # 你邮件的接收者my $recis = "";for (my $i = 0; $i < @recipients; $i++) { if ($i != @recipients - 1) { $recis .= $recipients[$i]."; "; } else { $recis .= $recipients[$i]; }}my @ccs = ('cc1@domain.com', 'cc2@domain.com'); # 抄送者my $ccs_str = "";for (my $i = 0; $i < @ccs; $i++) { if ($i != @ccs - 1) { $ccs_str .= $ccs[$i]."; "; } else { $ccs_str .= $ccs[$i]; }}my $sender = 'sender@domain.com'; # 发送者my $date = strftime("%Y-%m-%d", localtime(time() - 60 * 60 * 24));my $subject = "subject daily running [$date]"; # 邮件主题my $smtp = Net::SMTP->new($smtp_host, Timeout => 60, Debug => 1) || die "Cannot connect to mta($smtp_host)";$smtp->mail($sender);$smtp->recipient(@recipients, {Notify => ['FAILURE'], SkipBad => 1});$smtp->data();$smtp->datasend("To: $recis/n");$smtp->datasend("From: $sender/n");$smtp->datasend("CC: $ccs_str/n");$smtp->datasend("Subject: $subject/n");$smtp->datasend("Content-type: text/html;charset=GBK/n");$smtp->datasend("/n$send_html/n");$smtp->dataend();$smtp->quit;my $end = strftime("%Y%m%d%H%M%S", localtime(time()));print "[$end]End Execute Day Report/n";
转载地址:http://zofmd.baihongyu.com/