有時(shí)候我們?cè)诓渴鹉_本的時(shí)候,我們想知道,我們的程序執(zhí)行的怎么樣了,想得到執(zhí)行的結(jié)果,這樣我們也能放心很多是吧,那么在程序執(zhí)行成功或失敗的時(shí)候能夠給我沒(méi)發(fā)個(gè)郵件很是很不錯(cuò)的。
其實(shí)利用perl發(fā)郵件的方法有很多種,包括你在cpan上搜索mail關(guān)鍵字是一大堆,經(jīng)過(guò)實(shí)踐,MIME::Lite用來(lái)發(fā)郵件還是很合適的,最不可思議的是它可以幫你輕松的發(fā)送帶有附件的郵件哦。
在cpan上面有關(guān)于它的詳細(xì)的用法(http://search.cpan.org/~rjbs/MIME-Lite-3.028/lib/MIME/Lite.pm)
它發(fā)郵件的方式有兩種,第一種最簡(jiǎn)單就是利用系統(tǒng)自身的mail程序,比如sendmail來(lái)進(jìn)行,運(yùn)行sendmail當(dāng)然也許要具有root的權(quán)限了
#!/usr/bin/perl -w
use MIME::Lite;
my $msg = MIME::Lite->new(
From => ‘chenqing663@163.com',
To => ‘chenqing663@foxmail.com',
Cc => ‘some@other.com, some@more.com',
Subject => ‘hello,my first mail from chenqing.org',
Type => ‘multipart/mixed',
Data =>' other data'
);
$msg->attach(
Type => ‘image/png',
Disposition => ‘a(chǎn)ttachment',
Filename => ‘other.png',
Path => ‘/home/king/perl/logo.png'
);
$msg->send;
#!/usr/bin/perl -w
use MIME::Lite;
my $msg = MIME::Lite->new(
From => ‘chenqing663@163.com',
To => ‘chenqing663@foxmail.com',
Cc => ‘some@other.com, some@more.com',
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 => ‘a(chǎn)ttachment',
Filename => ‘other.png',
Id => ‘logo.png',
Path => ‘/home/king/perl/logo.png'
);
$msg->send;
#!/usr/bin/perl -w
use MIME::Lite;
use MIME::Base64;
use Authen::SASL;
my $host='smtp.163.com';
my $pass='yourpass';
my $user='xxx@163.com';
my $msg = MIME::Lite->new(
From => ‘xxx@163.com',
To => ‘chenqing663@foxmail.com',
Cc => ‘some@other.com, some@more.com',
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 => ‘a(chǎn)ttachment',
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;