我想在perl代码中发送电子邮件。因此,我使用了MIME::Lite
模块。
如果我删除last_send_successful检查,我就能按照自己的意愿发送电子邮件,否则我会得到下面提到的错误。我想知道电子邮件是否成功发送。以下是我使用的代码片段。
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发送的电子邮件已被投递?