华域联盟 perl Perl中使用MIME::Lite发送邮件实例

Perl中使用MIME::Lite发送邮件实例

有时候我们在部署脚本的时候,我们想知道,我们的程序执行的怎么样了,想得到执行的结果,这样我们也能放心很多是吧,那么在程序执行成功或失败的时候能够给我没发个邮件很是很不错的。

其实利用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)为例说明:

复制代码 代码如下:

#!/usr/bin/perl -w

use MIME::Lite;

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     => ‘image/png',
Disposition => ‘attachment',
Filename => ‘other.png',
Path => ‘/home/king/perl/logo.png'
);

$msg->send;

再来一个html格式的:

复制代码 代码如下:

#!/usr/bin/perl -w

use MIME::Lite;

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'
);

$msg->send;

下面看看怎么用smtp的方式发送:

复制代码 代码如下:

#!/usr/bin/perl -w

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;

是不是很简单呢?

本文由 华域联盟 原创撰写:华域联盟 » Perl中使用MIME::Lite发送邮件实例

转载请保留出处和原文链接:https://www.cnhackhy.com/77365.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部