perl发送邮件(email),代码如下:
#! /usr/bin/perl
# desc:send email
# edit www.jb200.com
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 = ('test1@jb200.com', 'test2@jb200.com'); # 你邮件的接收者
my $recis = "";
for (my $i = 0; $i < @recipients; $i++) {
if ($i != @recipients - 1) {
$recis .= $recipients[$i]."; ";
} else {
$recis .= $recipients[$i];
}
}
my @ccs = ('cc1@jb200.com', 'cc2@jb200.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@jb200.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";