How to check if last email sent was successfully delivered or not using MIME::Lite perl -
i wanted send emails in perl code. used mime::lite
module.
i able send emails wanted if removed last_send_successful check, else error mentioned below.i want know if email sent successfully. below code snippet used.
sub sendemailwithcsvattachments { $retries = 3; $retry_duration = 500000; # in microseconds $return_status; ( $from, $to, $cc, $subject, $body, @attachments_path_array ); $from = shift; $to = shift; $cc = shift; $subject = shift; $body = shift; @attachments_path_array = shift; $msg = mime::lite->new( => $from, => $to, cc => $cc, subject => $subject, type => 'multipart/mixed' ) or die "error while creating multipart container email: $!\n"; $msg->attach( type => 'text', data => $body ) or die "error while adding text message part email: $!\n"; foreach $file_path (@attachments_path_array) { $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 email: $!\n"; } $sent = 0; while ( !$sent && $retries-- > 0 ) { eval { $msg->send(); }; if ( !$@ && $msg->last_send_successful() ) { $sent = 1; } else { print "sending failed $to."; print "will retry after $retry_duration microseconds."; print "number of retries remaining $retries"; usleep($retry_duration); print "retrying..."; } } if ($sent) { $sent_message = $msg->as_string(); print "email sent successfully:"; print "$sent_message\n"; $return_status = 'success'; } else { print "email sending failed: $@"; $return_status = 'failure'; } }
the error getting is:
can't locate object method "last_send_successful" via package "mime::lite"
this means method not present. given in reference using.
so missing or there alternative check if last send successful or reference using incorrect?
is check redundant using eval block?
will using eval catch error of email not getting delivered? (most no want confirm)
how make sure email delivered mime::lite?
you don't need use eval
block or fancy ensure mail has been sent; last_send_successful
for. when send subroutine completes work, sets internal variable ($object->{last_send_successful}
); last_send_successful
sub checking. not necessary use eval
block unless trying prevent script dying or throwing runtime or syntax error.
you can simplify code following:
$msg->send; if ($msg->last_send_successful) { # woohoo! message sent } else { # message did not send. # take appropriate action }
or
$msg->send; while (! $msg->last_send_successful) { # message did not send. # take appropriate action }
Comments
Post a Comment