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

I am trying to add a check for the smtp server not being available. Here is the code (without the error check):
my $mailer = new Mail::Mailer('smtp', Server => $mailserver); my %headers=(); $headers{'To'} = "$recipient_email"; $headers{'From'} = "$sender_email"; $headers{'Subject'} = "$subject_email"; $mailer->open(\%headers);
If the $mailserver is not found or the smtp service is not available, then the last line fails with either "No such host is known." or "The requested name is valid and was found in the database, but it does not have the correct associated data being resolved for."

I have tried:
$mailer->open(\%headers) || die ("cannot find smtp server");
with the same results.

I have also tried various if statements, but am not sure for what condition to check.

Any suggestions? Thanks!!

Replies are listed 'Best First'.
Re: No smtp server found
by mr.dunstan (Monk) on Jun 22, 2001 at 21:45 UTC
    I don't think Mail::Mailer will tell you that. You might want to try making an independent tcp/ip connection to the smtp server, port 25 and see if it succeeds, and use that separate socket connect to be your error handler ... and if you're successful, try and send the mail. It's not fool proof but it's a decent way to go ...

    mr.dunstan
Re: No smtp server found
by Anonymous Monk on Jun 25, 2001 at 18:30 UTC
    try eval{$mailer->open(\%headers)} || die ("cannot find smtp server");