Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

MIME::Lite error handling

by jlongino (Parson)
on Feb 28, 2003 at 21:52 UTC ( #239562=perlquestion: print w/replies, xml ) Need Help??

jlongino has asked for the wisdom of the Perl Monks concerning the following question:

I'm not sure if this is a documentation bug or if I've come across a special case. I hit a snag when trying to relay MIME::Lite send error mesasages. If I have a malformed "To: " address such as in the following snippet:
sub Send_Msg { my ($to, $from, $subject, $body) = @_; use MIME::Lite; my $msg = new MIME::Lite To => '"some name"', From => $from, Subject => $subject, Type => 'TEXT', Data => $body; MIME::Lite->send('smtp', "some_valid_smtp_server_name", Timeout=>6 +0); ## the following is the offending statement $msg->send || die "MIME::Lite->send failed: $!\n"; }
The correct address should be of the form '"some name" <>'. In the above code the program dies with an smtp message but does not display the intended die message. The statement is patterned after the MIME::Lite documentation example:
As an instance method with no arguments, sends the message by the default mechanism set up by the class method. Returns whatever the mail-handling routine returns: this should be true on success, false/exception on error:
$msg = MIME::Lite->new(From=>...); $msg->send || die "you DON'T have mail!";
However replacing the offending line with the following works correctly:
eval { $msg->send }; die "MIME::Lite->send failed: $@\n" if $@;
Does this reflect your experience and if so, is it worth notifying the module author? Thanks.


Replies are listed 'Best First'.
Re: MIME::Lite error handling
by robartes (Priest) on Feb 28, 2003 at 22:21 UTC
    Good point. The way I read the docs, the module is behaving as it should:

    Returns whatever the mail-handling routine returns: this should be true on success, false/exception on error

    The way I read this, the send method can throw an exception, which is what is happening in your case (hence, your catching it with eval). So, it is behaving as documented.

    It could be a bit clearer though, something along the lines of ... returns false or throws an exception on error..

    Update: closed italics tag. Thanks AlexL.


Re: MIME::Lite error handling
by Anonymous Monk on Oct 12, 2011 at 05:54 UTC
    Thanks a lot, even almost a decade later you tip is saving life of many people :)
      i tried to this code in then call this from other script but it didin't capture error each time it is succesful even if am not providing the To or cc mail recieptents.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://239562]
Front-paged by traveler
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2021-10-20 13:15 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (81 votes). Check out past polls.