Perl发送带MIME信息附件的邮件

(0 comments)

模块创建并发送带 MIME 指定类型附件的邮件。MIME 是 Multimedia Internet Mail Extensions 的缩写,而且也是在邮件中附带各种文件文档的标准方式。

下面一段代码发送带MIME信息的附件,附件中的图片会在邮件客户端自动显示。

#! /usr/local/bin/perl -w
 
use MIME::Lite;
 
 
my $mailhost = "smtp.liaojl.com";
my $mailfrom = 'liaojl@liaojl.com';
# the recipient list
my @mailto = ('liaojl@liaojl.com', 'adminstrator@liaojl.com');
my $subject = "My Subject";
my $text = "The first line.\nThe second line.";
 
 
foreach my $mailto (@mailto) {
    # Send the From and Recipient for the mail servers that require it
    $msg = MIME::Lite->new(From  => $mailfrom,
        To   => $mailto,
        Subject => $subject,
        Data => "abc",
        Type  => 'multipart/mixed');
 
    $msg->attach(Type    => 'image/jpeg',
        Path    => '/home/liaojl/win40.jpg',
        Filename  => 'gnat-face.jpg');
 
    $msg->attach(Type => 'TEXT',
        Data => 'I hope you can use this!');
 
    $msg->send('smtp', 'smtp.liaojl.com');
}

这儿有几个有用的附件编码类型:

  • TEXT 代表 text/plain,为 Type 的默认值;
  • BINARY 是application/octet-stream的缩写;
  • multipart/mixed 表明邮件有附件;
  • application/msword 表明附件为微软的Word文档;
  • application/vnd.ms-excel 表明附件为微软的Excel文档;
  • application/pdf 表明附件为PDF文档;
  • image/gif,image/jpeg,image/png 分别指定GIF,JPEG,PNG文件;
  • audio/mpeg 指定MP3格式文件;
  • video/mpeg 指定MPEG格式影片;
  • video/quicktime 指定Quicktime格式文件。

发送邮件的唯一两种方法是sendmail和Net::SMTP。调用send方法时,若第一个参数为"smtp",则用Net::SMTP发送邮件。send的其它参数都传给Net::SMTP。

Currently unrated

Comments

There are currently no comments

New Comment

required

required (not published)

optional

required