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

I am using the following piece of code. How can I trap the program error if the smtp server address is not correct ?
$msg = MIME::Lite->new ( From =>$SendFrom, To =>$SendTo, Subject =>$SendSubject, Type =>'TEXT', Data =>$SendText ); MIME::Lite->send('smtp', $SmtpServer, Timeout=>60); $msg->send();

Replies are listed 'Best First'.
Re: Error handling with MIME::Lite
by jlongino (Parson) on Sep 11, 2002 at 06:02 UTC
    After some experimentation, it looks as though this might be your best bet assuming you don't want to use any extra modules:
    MIME::Lite->send('smtp', $SmtpServer, Timeout=>60); eval { $msg->send }; print $@ if $@; ### or do_something() if $@;
    This will trap the errors and allow you to continue processing but it doesn't tell you if $SmtpServer is invalid. An invalid host would return a message something like this:
    Failed to connect to mail server: Invalid argument at mime.lite.p line ##
    If the $SmtpServer doesn't allow relaying, you might get a message like this:
    SMTP RCPT command failed:
    What if the server times out? If you need to do more extensive error testing you might want to try using something like Net::SMTP, which is a subclass of Net::Cmd and IO::Socket::INET to send the message.

    --Jim