Linux下使用Perl來發信郵件過程

ysjxjf發表於2011-07-20

Linux下使用Perl來發信郵件過程

[@more@]
[日期:2007-12-02] 來源: 作者:Linux

最近一直很鬱悶,要維護10所大學伺服器,假如伺服器的磁碟滿了,或者其它原因,能不能早點發現呢?我就 想到了,弄個指令碼,監控伺服器,有異常時,發封郵件給我,就OK了。我原來一直想使用系統自帶的sendmail來實現我的想法,由於對sendmail 不熟,一直弄不好。只到今天,嘿嘿,在看一個網站時,看到使用php發郵件的方法。記得裝軟體時,沒有裝php,但是有裝perl啊,就這麼辦,下面是 Linux使用perl來發郵件的過程,如下:

1、登入系統

2、查詢是否有安裝perl

  [root@CentOS ~]# rpm -qa | grep perl
    perl-5.8.5-36.RHEL4

3、OK,來檢查一下網路

  [root@CentOS ~]# ping act0r.bokee.com
  PING act0r.bokee.com (58.49.57.34) 56(84) bytes of data.
  64 bytes from 58.49.57.34: icmp_seq=0 ttl=54 time=128 ms

4、安裝發郵件所需要的模組

  [root@CentOS ~]# perl -MCPAN -e shell

    基本上是一路回車,只有到最後面時,會讓你選擇你所在的區域,及國家。

  cpan>install Authen::SASL

  cpan>q

5、vi /etc/init.d/sendmail.sh

#!/usr/bin/perl
use Net::SMTP;

my $mailhost = "smtp.163.com"; # SMTP伺服器
my $mailfrom = 'notfour@163.com'; # 你的EMAIL地址
my @mailto = ('fayland@gmail.com', 'not_four@hotmail.com'); # 收信人地址
my $subject = "此為標題";
my $text = "此為正文n第二行位於此。";

$smtp = Net::SMTP->new($mailhost, Hello => 'localhost', Timeout => 120, Debug => 1); #為0時,不輸出除錯資訊

# anth login, type your user name and password here
$smtp->auth('user','pass'); ###使用者名稱和密碼

foreach my $mailto (@mailto) {
# Send the From and Recipient for the mail servers that require it
$smtp->mail($mailfrom);
$smtp->to($mailto);

# Start the mail
$smtp->data();

# Send the header
$smtp->datasend("To: $mailton");
$smtp->datasend("From: $mailfromn");
$smtp->datasend("Subject: $subjectn");
$smtp->datasend("n");

# Send the message
$smtp->datasend("$textnn");

# Send the termination string
$smtp->dataend();
}
$smtp->quit;


6、給sendmail執行許可權

  [root@CentOS ~]# chmod 755 /etc/init.d/sendmail.sh

7、測試

  [root@CentOS ~]# /etc/init.d/sendmail.sh

8、嘿嘿,看看信傳送了沒有。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/66634/viewspace-1052784/,如需轉載,請註明出處,否則將追究法律責任。

相關文章