Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: Race condition with Mail::Sender::MailMsg?

by DrewP (Initiate)
on Feb 15, 2012 at 15:08 UTC ( [id://953971]=note: print w/replies, xml ) Need Help??


in reply to Re: Race condition with Mail::Sender::MailMsg?
in thread Race condition with Mail::Sender::MailMsg?

Thanks, Marshall.

As suggested I have done 'safe' mode with an eval block, and the script is sending me mails as it should.

Now I just have to leave it running for several weeks to see if it logs a timeout and re-tries later as it should, or if it falls over and dies horribly!

I am very pleased and impressed to have had such a rapid response. I can't bring myself to learn python and I was worrying that I was the only person who still uses perl.

Thanks again - DrewP

Replies are listed 'Best First'.
Re^3: Race condition with Mail::Sender::MailMsg?
by Marshall (Canon) on Feb 15, 2012 at 15:21 UTC
    Debugging these "happens every few weeks" problems are hard!

    I am glad to hear that you are aware of the issues and "are on the way" - the journey may be long, but I'm glad to hear that my suggestion was understandable, implementable and hopefully ultimately helpful to you!

    Yeah, so show me the level of control that you've seen about Perl signals in Python! HA!
    Phython vs Perl isn't like tricycles vs motorcycles, it is more like tricycles vs jet airplanes.

      Just to confirm the suggested solution works. Today, just at the time the internet connection went down, my script logged the expiry of a 20 second timeout sending a simple message but carried on running.

      Of course it's easy for me because I know what sort of messages I will be sending and can thus set appropriate timeouts. As a general solution the calculation of the appropriate timeout becomes more complicated.

      In case it is of interest to anyone I have put my modified code below.

      sub sendmail { my ($arg_ref) = @_; print "Running sendmail()\n" if ($onterm && ($debug & $DEBUG_NOTIFY_ +MAILS)); =pod This subroutine is the interface to the mail sender functionality. Depending on incoming parameters it sends just a message or a message + with attachment If the attachment file does not exist it sends message with notificat +ion that file was not found =cut my $sendfailed = 0; my $sender = new Mail::Sender( {from => $arg_ref->{from}, smtp => $arg_ref->{smtp}, port => $arg_ref->{port}, auth => $arg_ref->{auth}, authid => $arg_ref->{authid}, authpwd => $arg_ref->{authpwd}, auth_encoded => $arg_ref->{auth_encoded}, encoding => 'Quoted-printable', charset => $charset, }); if ($sender > 0) { print "\$sender created OK\n" if ($onterm && ($debug & $DEBUG_NOTI +FY_MAILS)); if ((defined $arg_ref->{file}) && (-f $arg_ref->{file})) { # Sen +d message with attachment if the attached file exists print "sending mail with attachment\n" if ($onterm && ($debug & +$DEBUG_NOTIFY_MAILS)); my $mailfiletimeout = 120; #120 seconds eval { local $SIG{ALRM} = sub { die "MailFile Timeout\n" }; # NB: \n requ +ired alarm $mailfiletimeout; $sender->MailFile({to => $arg_ref->{to}, subject => $arg_ref->{subject}, encoding => "Base64", ## read this from parameters? msg => encode($charset,$arg_ref->{msg}), file => $arg_ref->{file} }); alarm 0; }; if ($@) { die unless $@ eq "MailFile Timeout\n"; # propagate unexpected erro +rs logerror("Timeout ($mailfiletimeout seconds) sending $arg_ref->{fi +le} to $arg_ref->{to} via $arg_ref->{smtp}"); return 0; } else { if (defined $sender->{'error'} ){ logerror( "Error sending mail with attachment '$arg_ref->{file}' + to $arg_ref->{to}: Error code $sender->{'error'} ($sender->{'error_m +sg'})"); $sendfailed = 1; } else { loginfo("File \"$arg_ref->{file}\" successfully emailed to $arg_ +ref->{to}"); } } } else { #send a message with no attachment #Should never receive a filename for a file that does not exist. + Add a line to the message if it ever happens $arg_ref->{msg} .= "\n\nAttempt to send file '$arg_ref->{file}' + abandoned: File does not exist" if defined $arg_ref->{file}; print "sending mail without attachment\n" if ($onterm && ($debug + & $DEBUG_NOTIFY_MAILS)); my $mailmessagetimeout = 20; #20 seconds eval { local $SIG{ALRM} = sub { die "MailMsg Timeout\n" }; # NB: \n requi +red alarm $mailmessagetimeout; $sender->MailMsg({to => $arg_ref->{to}, subject => $arg_ref->{subject}, encoding => "Quoted-printable", ## read this from parame +ters? msg => encode($charset,$arg_ref->{msg}), }); alarm 0; }; if ($@) { die unless $@ eq "MailMsg Timeout\n"; # propagate unexpected error +s logerror("Timeout ($mailmessagetimeout seconds) sending message to + $arg_ref->{to} via $arg_ref->{smtp}"); return 0; } else { if (defined $sender->{'error'} ){ logerror( "Error sending mail to $arg_ref->{to}: Error code $sen +der->{'error'} ($sender->{'error_msg'})"); $sendfailed = 1; } else { print "Mail sent to $arg_ref->{to}\n" if ($onterm && ($debug & + $DEBUG_NOTIFY_MAILS)); } } } # send a message with no attachment } # if ($sender > 0) else { logerror("Could not establish mail sender to $arg_ref->{to}: Error + code $sender ($Mail::Sender::Error)"); $sendfailed = 1; } print "Leaving sendmail (\$sendfailed = $sendfailed)\n" if ($onterm + && ($debug & $DEBUG_NOTIFY_MAILS)); return ($sendfailed) ? 0 : 1; }
        Thanks for posting your working code!
        some minor suggestions:
        .... eval { local $SIG{ALRM} = sub { die "MailFile Timeout\n" }; alarm $mailfiletimeout; #20 seconds $sender->MailFile(...); alarm 0; }; if ($@) { die unless $@ eq "MailFile Timeout\n"; # propagate # unexpected errors logerror("Timeout ....); return 0; } # no else{} is required, what "else" ???? # Simplify the logic - reduduce levels of indendation # if there is a serious error we don't get here... # there is no "else" # proceed with more code...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://953971]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2024-03-28 09:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found