Perl中使用MIME::Lite發(fā)送郵件實(shí)例
有時(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ā)送帶有附件的郵件哦。
下面我們就以MIME::Lite發(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)限了
另一個(gè)就是通過(guò)smtp的方式了,我們會(huì)以網(wǎng)易的163郵箱為例說(shuō)明。
我們先以默認(rèn)發(fā)送方式(sendmail)為例說(shuō)明:
#!/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;
再來(lái)一個(gè)html格式的:
#!/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;
下面看看怎么用smtp的方式發(fā)送:
#!/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;
是不是很簡(jiǎn)單呢?
相關(guān)文章
Perl刪除前導(dǎo)和拖尾空白(刪除左右空格、空白字符)
這篇文章主要介紹了Perl刪除前導(dǎo)和拖尾空白(刪除左右空格、空白字符),本文給出了多個(gè)方法實(shí)現(xiàn)解決這個(gè)需求,需要的朋友可以參考下2015-06-06Perl使用nginx FastCGI環(huán)境做WEB開(kāi)發(fā)實(shí)例
這篇文章主要介紹了Perl使用nginx FastCGI環(huán)境做WEB開(kāi)發(fā)實(shí)例,實(shí)現(xiàn)了路由系統(tǒng)和模板系統(tǒng),需要的朋友可以參考下2014-06-06Perl中的10個(gè)操作日期和時(shí)間的CPAN模塊介紹
這篇文章主要介紹了Perl中的10個(gè)操作日期和時(shí)間的CPAN模塊介紹,本文介紹了Date::Manip、DateTime、Time::Format、Time::Interval、Date::Convert、Benchmark、Time::Normalize、Regexp::Common::time等10個(gè)模塊,需要的朋友可以參考下2015-02-02