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

I have this script that is used to send news letters to subscribers:
$sender = new Mail::Sender; $sender->Open({ smtp => $smtp, from => "bounce\@mydomain.com", fake_from => $from, to => "subscribers\@mydomain.com", bcc => $to, encoding => "quoted-printable", subject => $subject, ctype => $htype, headers => "Errors-To: bounce\@mydomain.com", }) or usage(), die "Sender error: $sender, $Mail::Sender::Erro +r!\n"; # body of email. $sender->SendEnc(@body) or usage(), die "Sender error: $sender, $M +ail::Sender::Error!\n"; # send email $sender->Close or usage(), die "Sender error: $sender, $Mail::Send +er::Error!\n";

The subscriber list is broken into 80 recipients at a time. These are assigned to the $to variable. The problem occurs if there is any problem with one of the recipients. Although we try to ensure that all recipients are validated sometimes bad ones are missed. When that happens the smtp server sends an error back to the sender script causing it to exit before it is finished.

How can I make the script continue to the next recipient, with out exiting, should the smtp server return an error?

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
Re: Handling smtp errors gracefully using Mail::Sender
by Jenda (Abbot) on Nov 25, 2003 at 15:38 UTC

    If you have a fairly recent version of Mail::Sender (0.8.08 or newer) you may use the skip_bad_recipients option:

        skip_bad_recipients
            If this option is set to false or not specified then Mail::Sender
            stops trying to send a message as soon as the first recipient's
            address fails. If it is set to a true value Mail::Sender skips the
            bad addresses and tries to send the message at least to the good
            ones. If all addresses are rejected by the server it reports an "All
            recipients were rejected" message.
    
            If any addresses were skipped the "$sender->{'skipped_recipients'}"
            will be a reference to a hash containing the failed address and the
            server's response.
    

    Jenda
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
       -- Rick Osborne

    Edit by castaway: Closed small tag in signature

      Sorry, I found the problem, I knew as soon as I hit the send button I would find the problem. Thanks, KG
Re: Handling smtp errors gracefully using Mail::Sender
by delirium (Chaplain) on Nov 25, 2003 at 14:09 UTC
    You could change "or die" to "or warn", and redirect STDERR to a file that you can look through later to see if anything bad happened.
Re: Handling smtp errors gracefully using Mail::Sender
by neilwatson (Priest) on Nov 25, 2003 at 16:20 UTC
    Thanks for your help. Both suggestions have helped.

    Neil Watson
    watson-wilson.ca