利用Perl发送邮件例子

 #! /usr/bin/perl use 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 sended my $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 mail my $smtp_host = "your smtp server"; # 你的smtp服务器 my @recipients = ('[email protected]', '[email protected]'); # 你邮件的接收者 my $recis = ""; for (my $i = 0; $i < @recipients; $i++) { if ($i != @recipients - 1) { $recis .= $recipients[$i]."; "; } else { $recis .= $recipients[$i]; } } my @ccs = ('[email protected]', '[email protected]'); # 抄送者 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 = '[email protected]'; # 发送者 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";

你可能感兴趣的:(html,Date,server,File,perl,System)