2012年12月4日星期二

Perl : 用Mail::Sender发送邮件

use Mail::Sender;

sub send_mail {
# from,to,cc, subject, message, attach = [ ]
# smtp_server : xxx.xxx.xxx.xxx
# message_charset : default gbk
# ctype : default text/html
# auth : default LOGIN

my ($m) = @_;
$m->{message_charset} ||= 'gbk';
$m->{ctype} ||= 'text/html';
$m->{auth} ||= 'LOGIN';

#print "send_mail : $m->{subject}\nfrom:$m->{from}\nto:$m->{to}\ncc:$m->{cc}\n";

my $sender = new Mail::Sender {
smtp => $m->{smtp_server},
from => $m->{from},
#debug => '/tmp/mail_debug.txt',
boundary => '----this-is-a-mail-boundary-----',
};
die "Can't create the Mail::Sender object: $Mail::Sender::Error"
unless ref $sender;

$sender->OpenMultipart( {
to => $m->{to},
cc => $m->{cc},
replyto => $m->{from},
# fake_from => $m->{fake_from},
# auth => $m->{auth},
# authid => $m->{user},
# authpwd => $m->{passwd},
from => $m->{from},
ctype => $m->{ctype},
subject => $m->{subject},

} );

$sender->Body( {
charset => $m->{message_charset},
msg => $m->{message},
} );

for my $attach_file (@{$m->{attach}}) {
$sender->Attach( {
description => "$attach_file",
ctype => 'text/html',
disposition => "attachment;filename=$attach_file",
file => "$attach_file",
id => "$attach_file",
} )
|| die "Error in attachment: $Mail::Sender::Error\n"
if ( -f $attach_file );
} ## end for my $attach_file (@$attach_files_ref)
$sender->Close;

} ## end sub send_mail

没有评论:

发表评论