Perl Mail::Sender发送带附件的邮件

(0 comments)

Mail::Sender模块支持发送带附件的邮件,先安装Mail::Sender模块:

# perl -MCPAN -e shell
cpan> install Mail::Sender

发送一封文本格式的邮件,代码如下:

#! /usr/local/bin/perl -w
 
use Mail::Sender;
 
ref ($sender = new Mail::Sender { from => 'liaojl@liaojl.com',
      smtp => 'smtp.liaojl.com', boundary => 'This-is-a-mail-boundary-435427'})
or die "Error($sender) : $Mail::Sender::Error\n";
 
ref $sender->Open({to => 'liaojl@liaojl.com, administrator@liaojl.com', 
                              cc => 'root@liaojl.com',
                              subject => 'Hello dear friend'})
        or die "Error: $Mail::Sender::Error\n";
my $FH = $sender->GetHandle();
print $FH "How are you?\n\n";
print $FH <<'*END*';
I've found these jokes.
 
 Doctor, I feel like a pack of cards.
 Sit down and I'll deal with you later.
 
 Doctor, I keep thinking I'm a dustbin.
 Don't talk rubbish.
 
Hope you like'em. Jenda
*END*
 
$sender->Close;

发送带附件的邮件

#! /usr/local/bin/perl -w
 
use Mail::Sender;
 
ref ($sender = new Mail::Sender { from => 'liaojl@liaojl.com',
      smtp => 'smtp-shanghai', boundary => 'This-is-a-mail-boundary-435427'})
or die "Error($sender) : $Mail::Sender::Error\n";
 
$sender->OpenMultipart({to => 'liaojl@liaojl.com',
                        subject => 'Mail::Sender.pm - new module'});
$sender->Body;
$sender->SendEnc(<<'*END*');
Here is a new module Mail::Sender.
It provides an object based interface to sending SMTP mails.
It uses a direct socket connection, so it doesn't need any
additional program.
 
Enjoy, Jenda
*END*
$sender->Attach(
 {description => 'Perl module Mail::Sender.pm',
  ctype => 'application/x-zip-encoded',
  encoding => 'Base64',
  disposition => 'attachment; filename="Sender.zip"; type="ZIP archive"',
  file => 'sender.zip'
 });
$sender->Close;

调用MailFile方法可以添加多个附件,代码如下所示:

#! /usr/local/bin/perl -w
 
use Mail::Sender;
 
ref ($sender = new Mail::Sender { from => 'daniel.liao@wuerth-phoenix.com',
      smtp => 'smtp-shanghai', boundary => 'This-is-a-mail-boundary-435427'})
or die "Error($sender) : $Mail::Sender::Error\n";
 
 (ref ($sender->MailFile(
  {to =>'daniel.liao@wuerth-phoenix.com', subject => 'this is a test',
   msg => "Hi Johnie.\nI'm sending you the pictures you wanted.",
   file => 'redhat.jpg,win40.jpg'
  }))
  and print "Mail sent OK."
 )
 or die "$Mail::Sender::Error\n";

不过上面的方法并没有指明文件类型,所以邮件客户端不会自动将图片显示在邮件最后。

Currently unrated

Comments

There are currently no comments

New Comment

required

required (not published)

optional

required