perl模块之MIME::Lite发送有附件的邮件

#!/usr/bin/perl
use strict;
use warnings;

use MIME::Lite; #这个用来组织邮件信息内容
use Pod::Usage;
use Getopt::Long;
use MIME::Base64;
use MIME::Words qw/:all/;
use Authen::SASL;
my $options = {
        from    => 'bot',
};

GetOptions( $options,
            'help|?',       # 帮助
            'html!',        # HTML 邮件支持
            'subject=s',    # 邮件标题
            'from=s',       # 发件人
            'to=s',         # 收件人
            'cc=s',         # 抄送
            , # 附件
        ) or pod2usage();

pod2usage() if $options->{help};
pod2usage() unless $options->{subject} and $options->{to};

my $cc   = $options->{cc} || '';
my $lines = <>;

#$lines=encode_base64("$lines",'');

# 邮件服务器的连接信息
MIME::Lite->send("smtp", "smtp.163.com",AuthUser=>‘myusername',AuthPass=>'******',Debug => 0, Timeout => 60);

# 组织邮件信息内容
my $msg = MIME::Lite->new(
                From => $options->{from},
                To => $options->{to},
                cc => $cc,
                Subject => encode_mimeword($options->{subject},'b','utf-8'),
                #Subject => $options->{subject},
                #Subject => '=?utf-8?B?'.encode_base64("$options->{subject}").'?=',
                Type => 'multipart/mixed'
                );


if (not $options->{html} ){
    $msg->attach(  
            Encoding =>'base64',
            Type =>'text/plain;charset=utf-8',
            Data => $lines
            );
   
}else{
    $msg->attach(
            Encoding =>'Base64',
            Type =>'text/html; charset=utf-8',
            Data => $lines
            );
}
### attach: $options->{attach}
for ( 0 .. $#{$options->{attach}} ) {
    $msg->attach(
           Type =>'auto', #建议设置成auto要不就application/octet-stream
           Path => $options->{attach}[$_],
           Encoding => 'Base64',
           Disposition => 'attachment'
           );
}
print STDERR "正在发送.......\n";
$msg->send() or die "Couldn't send whole message: $!\n";

__END__

=head1 NAME

sendMail - 发送邮件

=head1 SYNOPSIS

sendMail [选项] [正文文件名]

正文文件名省略时,邮件正文来自标准输入

选项:

    --help                  显示帮助信息
    --subject               邮件标题
    --from                  发件人
    --to                    收件人
    --cc                    抄送
    --html                  发送 HTML 格式邮件
    --attach                发送附件

调用方法:

echo "aaaaa你好---"|perl demo.pl --subject hello --from --to --attach /home/a.zip

 

 

 

 

sub sendEmail
{
 
 my($from,$to,$subject,$body,$att)=@_;
 $subject= encode_utf8(decode_entities($subject));
 $body=encode_utf8(decode_entities($body));
 #$body=~s/\n\r|\r\n/<br>/;
 $boundary = "====" . time() . "===="; %mail = (
         from => "$from",
         to => "$to",
         subject => "$subject"
         #,'content-type' => "multipart/alternative; boundary=\"$boundary\""
        );
  $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";

=cut
 if($att){
 %mail = (
         from => "$from",
         to => "$to",
         subject => "$subject",
   attachmentArr=>[$att],
         'content-type' => "multipart/alternative; boundary=\"$boundary\""
        );
  }
  else
  {
   %mail = (
         from => "$from",
         to => "$to",
         subject => "$subject",
         'content-type' => "multipart/alternative; boundary=\"$boundary\""
        );
  }
=cut
 $boundary = '--'.$boundary;
if($att)
{
 $filename=$1 if($att=~/.*\/(.*?)$/);
 open (F, $att) or die "Cannot read $file: $!";
 binmode F; undef $/;
 $attcontent = encode_base64(<F>);
 close F;


 $mail{body} = <<END_OF_BODY;
 
$boundary
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable

$body
$boundary
Content-Type: application/octet-stream; name="$filename"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="$filename"

$attcontent
$boundary--

END_OF_BODY
}
else
{
$mail{body} = <<END_OF_BODY;

$boundary
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable

$body
$boundary--

END_OF_BODY
}
sendmail(%mail) || print "Error: $Mail::Sendmail::error\n";
=cut
my $sm = new SendMail;
    $sm->From($from);
    $sm->To($to);
    $sm->Subject("BD campaign: $subject");
 $body=qq{$body};
    $sm->setMailBody($body);
    $sm->sendMail();
=cut 
 
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值