有時候我們在部署腳本的時候,我們想知道,我們的程序執行的怎么樣了,想得到執行的結果,這樣我們也能放心很多是吧,那么在程序執行成功或失敗的時候能夠給我沒發個郵件很是很不錯的。
其實利用perl發郵件的方法有很多種,包括你在cpan上搜索mail關鍵字是一大堆,經過實踐,MIME::Lite用來發郵件還是很合適的,最不可思議的是它可以幫你輕松的發送帶有附件的郵件哦。
下面我們就以MIME::Lite發郵件為例:
在cpan上面有關于它的詳細的用法(http://search.cpan.org/~rjbs/MIME-Lite-3.028/lib/MIME/Lite.pm)
它發郵件的方式有兩種,第一種最簡單就是利用系統自身的mail程序,比如sendmail來進行,運行sendmail當然也許要具有root的權限了
另一個就是通過smtp的方式了,我們會以網易的163郵箱為例說明。
我們先以默認發送方式(sendmail)為例說明:
To => ‘[email protected]',
Cc => ‘[email protected], [email protected]',
Subject => ‘hello,my first mail from chenqing.org',
Type => ‘multipart/mixed',
Data =>' other data'
);
$msg->attach(
Type => ‘image/png',
Disposition => ‘attachment',
Filename => ‘other.png',
Path => ‘/home/king/perl/logo.png'
);
$msg->send;
再來一個html格式的:
To => ‘[email protected]',
Cc => ‘[email protected], [email protected]',
Subject => ‘hello,my first mail from chenqing.org',
Type => ‘multipart/mixed',
Data =>' other data'
);
$msg->attach(
Type => ‘text/html',
Data => qq{
<body>
這是我的 <b>good</b> image:
<img src=”cid:logo.png”>
</body>
},
);
$msg->attach(
Type => ‘image/png',
Disposition => ‘attachment',
Filename => ‘other.png',
Id => ‘logo.png',
Path => ‘/home/king/perl/logo.png'
);
$msg->send;
下面看看怎么用smtp的方式發送:
use MIME::Lite;
use MIME::Base64;
use Authen::SASL;
my $host='smtp.163.com';
my $pass='yourpass';
my $user='[email protected]';
my $msg = MIME::Lite->new(
From => ‘[email protected]',
To => ‘[email protected]',
Cc => ‘[email protected], [email protected]',
Subject => ‘hello,my first mail from chenqing.org',
Type => ‘multipart/mixed',
Data =>' other data'
);
$msg->attach(
Type => ‘text/html',
Data => qq{
<body>
這是我的 <b>good</b> image:
<img src=”cid:logo.png”>
</body>
},
);
$msg->attach(
Type => ‘image/png',
Disposition => ‘attachment',
Filename => ‘other.png',
Id => ‘logo.png',
Path => ‘/home/king/perl/logo.png'
);
MIME::Lite->send(‘smtp', $host, Timeout=>60, AuthUser=>$user, AuthPass=>$pass);
$msg->send;
是不是很簡單呢?
新聞熱點
疑難解答