Net::SMTP::Retryable - Net::SMTP wrapper
use Net::SMTP::Retryable; %options = ( connectretries => 2, sendretries => 5, retryfactor => .25 ); $smtp = Net::SMTP->new($mailhost, %options); $smtp = Net::SMTP->new([ $mailhost1, $mailhost2, ...], %options); $smtp->SendMail(mail=>$from, to=>$to, data=>$body); $smtp->mail($from); $smtp->to($to); $smtp->data($body);
Net::SMTP offers some automatic redundancy by allowing you to specify multiple hosts. On connection, a connection will be tried to each host until one succeeds. However, if you loose your connection, it is up to you to reconnect and resend your message. This leads to code which has reconnection and retries and usually ends up as a mess.
This class adds retry and reconnect logic to Net::SMTP commands The following Net::SMTP commands are patched to offer retry logic:
$smtp->mail() $smtp->send() $smtp->send_or_mail() $smtp->send_and_mail() $smtp->mail() $smtp->to() $smtp->cc() $smtp->bcc() $smtp->recipient() $smtp->data()
If any of the above commands fail, the connection to the server will be reset and all commands in the current mail transaction will be replayed.
This also works with other mail packages which use Net::SMTP as a transport like MIME::Entity.
This is an alpha version and there are a few things which will undoubtedly change. First, the default retry numbers are up for discussion and will probably change. Second, the retry logic is very simple and needs to be given a bit of thought.
I'd like this to really be a subclass of Net::SMTP. Right now, it Net::SMTP::Retryable delegates to Net::SMTP. In order to subclass, I'd have to build a few more methods instead of using the AUTOLOADer and figure out how to handle getting a new Net::SMTP object on retries. The former is just a matter of typing, the later is not so simple.
Marc Prewitt < mprewitt at the domain flatiron in the dot org tld >
This is a short-cut method for sending a mail in one command.
$smtp->SendMail( mail=>$from, send=>$from, send_or_mail=>$from, send_and_mail=>$from # FROM methods to=>$to, cc=>$cc, bcc=>$bcc, recipient=>$recipient, # TO methods data=>$data # BODY ) || warn "Mail failed";
Sends an email using the Net::SMTP mail/send/send_or_mail/send_and_mail, to/cc/bcc/recipient, data methods. One FROM and one TO arguments are required. The values of the arguments can be scalars or array refs. Use an array ref if you need to send additional parameters or multiple parameters to the underlying Net::SMTP method.
Returns undef if any of the methods fails otherwise returns the return value of the last method executed.
$net_smtp = $net_smtp_retryable->get_smtp();
Returns the underlying smtp object.
$smtp = Net::SMTP::Retryable->new( $mailhost, %options ); $smtp = Net::SMTP::Retryable->new( \@mailhosts, %options ); B<PARAMETERS:> $mailhost - Outgoing SMTP host to connect to or array of hosts to connect to %options - Optional parameters: connectretries => $number of times to retry a connection (default=0) sendretries => $number of times to retry a send attempt (default=0) retryfactor => number of seconds to pause between each reconnect attempt. Number can be less than 1, number is doubled on each successive reconnect attempt. (default=1)
RETURN VALUES: Reference to instantiated object.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.