【发布时间】:2014-11-15 00:10:44
【问题描述】:
我想用 perl 代码发送电子邮件。所以我使用了MIME::Lite 模块。
如果我删除了 last_send_successful 检查,我可以随意发送电子邮件,否则我会收到下面提到的错误。我想知道电子邮件是否发送成功。下面是我使用的代码 sn-p。
sub sendEmailWithCSVAttachments {
my $retries = 3;
my $retry_duration = 500000; # in microseconds
my $return_status;
my ( $from, $to, $cc, $subject, $body, @attachments_path_array );
$from = shift;
$to = shift;
$cc = shift;
$subject = shift;
$body = shift;
@attachments_path_array = shift;
my $msg = MIME::Lite->new(
From => $from,
To => $to,
Cc => $cc,
Subject => $subject,
Type => 'multipart/mixed'
) or die "Error while creating multipart container for email: $!\n";
$msg->attach(
Type => 'text',
Data => $body
) or die "Error while adding text message part to email: $!\n";
foreach my $file_path (@attachments_path_array) {
my $file_name = basename($file_path);
$msg->attach(
Type => 'text/csv',
Path => $file_path,
Filename => $file_name,
Disposition => 'attachment'
) or die "Error while adding attachment $file_name to email: $!\n";
}
my $sent = 0;
while ( !$sent && $retries-- > 0 ) {
eval { $msg->send(); };
if ( !$@ && $msg->last_send_successful() ) {
$sent = 1;
} else {
print "Sending failed to $to.";
print "Will retry after $retry_duration microseconds.";
print "Number of retries remaining $retries";
usleep($retry_duration);
print "Retrying...";
}
}
if ($sent) {
my $sent_message = $msg->as_string();
print "Email sent successfully:";
print "$sent_message\n";
$return_status = 'success';
} else {
print "Email sending failed: $@";
$return_status = 'failure';
}
}
我得到的错误是:
Can't locate object method "last_send_successful" via package "MIME::Lite"
这意味着该方法不存在。但它在我使用的参考文献中给出。
那么我是否遗漏了什么,或者是否有其他方法可以检查上次发送是否成功或我使用的参考不正确?
因为我已经在使用 eval 块,所以这个检查是多余的吗?
使用 eval 会捕获电子邮件未送达的错误吗? (很可能没有,但想确认)
如何确保使用 MIME::Lite 发送电子邮件?
【问题讨论】:
-
perl -MMIME::Lite -e 'print $MIME::Lite::VERSION'