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

本文介绍如何使用Perl的Mail::Sender模块发送带附件的邮件,包括发送文本邮件和带附件邮件的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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";

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值