How to check if last sent email was successfully sent or not using MIME :: Lite perl

I wanted to send emails in Perl code. So I used the MIME::Lite


I can send email as I wanted if I removed the last_send_successful check, otherwise I will get the error below. I want to know if the email was sent successfully. Below is the code snippet that I used.

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";

        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);
            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";
            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';


The error I am getting:

Can't locate object method "last_send_successful" via package "MIME::Lite"


This means that this method is not available. But this is given in the link I am using.

  • Am I missing something or is there an alternative to check if the last transfer was successful or the link I am using is incorrect?

  • Is this check overkill since I'm already using the eval block?

  • Will the eval catch use an email error that will not be delivered? (Probably not, but I want to confirm)

  • How do I ensure email delivery with MIME :: Lite?


source to share

1 answer

You don't need to use a block eval

or do anything interesting to get your mail sent; for what last_send_successful

. When the send function successfully completes its work, it sets the internal variable ( $object->{last_send_successful}

); this is what sub is testing last_send_successful

. There is usually no need to use a block eval

unless you are trying to prevent the script dying or throwing a runtime or syntax error.

You can simplify your code like this:


if ($msg->last_send_successful) {
    # woohoo! Message sent
else {
    # message did not send.
    # take appropriate action




while (! $msg->last_send_successful) {
    # message did not send.
    # take appropriate action




All Articles