2008年12月4日

[程式] Perl 寄信給gmail的方法 (以網站復原為例)

如果監控的網站斷線恢復了,怎麼發個mail通知呢? 用Perl的做法如下

發信程式前面加上收件者, 這邊是寄給 qqq 跟 abc
chomp($theman="qqq\@gmail.com,abc\@gmail.com");
.
.
.

監控部份省略

.
.
.

需要發信的地方加上, 呼叫mailgood副程式
&mailgood($theman);

.
.
.


寄信的副程式
sub mailgood {
$subject = encode("big5",decode("utf8","網站狀態 - $ip 復原"));
$nowtime = `date +%Y\/%m\/%d-%H:%M:%S`;
$mailcontent = encode("big5",decode("utf8","復原時間 $nowtime"));
$to=shift;
# sendmail program location
my $mailprg = "/usr/sbin/sendmail -t";
# sender Email
my $from = "taiwanwolf\@abc.com";
open (MAIL,"|$mailprg") || die "$!\n";
print MAIL "Content-Type: text/plain; charset=big5\n";
print MAIL "Return-Path: $from\n";
print MAIL "From: $from\n";
print MAIL "To: $to\n";
print MAIL "Subject: $subject\n";
print MAIL "\n";
print MAIL "$mailcontent \n";
}

沒有留言: