我不久前编写了这个函数,它运行得非常好。
它需要在您的系统上安装 MIME::Lite 模块 - 不确定这是否会成为绊脚石(它肯定在我的位置)
如果代码不符合最新标准,我们深表歉意,我相信它已经有 3 年的历史了,并且可以在 Perl 5.6.1 上运行。
sub emailer($$$$$$;$);
use MIME::Lite;
sub emailer($$$$$$;$)
{
#-------------------------------------------------------------------------#
# Get incoming parameters #
#-------------------------------------------------------------------------#
my ( $exchange_svr, $to, $cc, $from, $subject, $message, $attachment ) = @_;
#-------------------------------------------------------------------------#
# create a new message to be sent in HTML format #
#-------------------------------------------------------------------------#
my $msg = MIME::Lite->new(
From => $from,
To => $to,
Cc => $cc,
Subject => $subject,
Type => 'text/html',
Data => $message
);
#-------------------------------------------------------------------------#
# Check if there is an attachment and that the file actually does exist #
# Only plain text documents are supported in this functioN. #
#-------------------------------------------------------------------------#
if ( $attachment )
{
#---------------------------------------------------------------------#
# if the attachment does not exist then show a warning #
# The email will arrive with no attachment #
#---------------------------------------------------------------------#
if ( ! -f $attachment )
{
print "WARNING - Unable to locate $attachment";
}
else
{
#-----------------------------------------------------------------#
# add the attachment #
#-----------------------------------------------------------------#
print "ATTACH", "$attachment";
$msg->attach(
Type => "text/plain",
Path => $attachment,
Disposition => "attachment"
);
}
}
#-------------------------------------------------------------------------#
# send the email #
#-------------------------------------------------------------------------#
MIME::Lite->send( 'smtp', $exch_svr, Timeout => 20 );
$msg->send() or die "SENDMAIL ERROR - Error sending email";
}
使用时是这样的
emailer( $exchange_server,
"someone@somewhere.com",
"someoneelse@somewhere.com",
"me@mydesk.com",
"Subject in here",
"The Message in here",
"/full/path/to/attachment" );
您可以选择添加第 7 个参数,即附件(您需要提供完整路径),并且附件必须是文本文件(我正在发送 CSV 文件)
编辑
我刚刚重新阅读了您的帖子,发现您确实想发送附件,所以我将该部分添加到示例中