我想用perl代碼發送電子郵件。所以我使用MIME::Lite
模塊。如何檢查發送的最後一封電子郵件是否已成功發送或未使用MIME :: Lite perl
如果我刪除了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 ([email protected] && $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: [email protected]";
$return_status = 'failure';
}
}
我得到的錯誤是:
Can't locate object method "last_send_successful" via package "MIME::Lite"
這意味着這種方法不存在。但它在我使用的參考文獻中給出。
所以我錯過了一些東西,或者有其他方法可以檢查最後一次發送是否成功或者我使用的引用不正確?
由於我已經在使用eval塊,所以這個檢查是多餘的嗎?
將使用eval捕獲電子郵件的錯誤未送達? (很可能不,但想確認)
如何確保電子郵件與MIME :: Lite一起交付?
perl的-MMIME ::精簡版-e '打印$ MIME ::精簡版:: VERSION' – askovpen 2014-09-20 16:40:35