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

After looking through many of the mail modules, I've settled on Mail::Sender. I need authenticated SMTP, so that drops out several other options and I don't want to pull in multiple modules manually to get the necessary authenticating done

My problem comes in with using Mail::Sender from within CGI scripts. I copied the exact same code snippet to a command line script and never get the same problem. The problem is simple: for every few emails sent, Mail::Sender fails with a "Service not available, connection not established" error. The culprit-type script is below. Does Mail::Sender change behaviour under CGI, because I have never once got this error from the command line version:

#!c:/perl/bin/perl -w $|++; use strict; use Mail::Sender; $Mail::Sender::NO_X_MAILER = 1; $Mail::Sender::NO_MESSAGE_ID = 1; open my $log, '>>', 'c:/logcmd.log' or die "open failed: $!"; my $email = Mail::Sender->new( { smtp => 'smtp.somewhere.net', auth => 'LOGIN', authid => 'someuser', authpwd => 'somepasswd', on_errors => 'die', debug => $log } ); $email->Open( { from => 'MyName <me@myserver.com>', to => 'TheirName <them@theirserver.com>', subject => 'Some Subject' } ); $email->SendEnc(<<"END_EMAIL"); This is the body of the email message. Yes, that is right. foo bar baz.... blah. END_EMAIL $email->Close(); close $log;

Replies are listed 'Best First'.
Re: Mail::Sender connection failing under CGI
by hossman (Prior) on Feb 28, 2004 at 00:42 UTC

    A quick peek at the source for Mail::Sender v0.8.10 indicates that a message similar to the one you mentioned is generated by the SERVNOTAVAIL error method, which seems to get used if and only if the method get_response returns a value which does not start with a 1, 2, or 3 immediately after connecting to a server.

    The return value of get_response seems to be a slightly cleaned up version of whatever the remote SMTP server sent across the wire, so if it doesn't start with a 1, 2 or 3 -- then it would seem that your SMTP server is reporting back and error code as soon as you connect.

    There seem to be an undocumented "$Mail::Sender::LastResponse" that you can try logging immediately after failure to see exactly what the response from the SMTP server was.

    My guess: when you put this on your website, it started getting hit so much, your server occasionally respondes back with "busy" signal (maybe 554 .. but it could be any error code)

Re: Mail::Sender connection failing under CGI
by AidanLee (Chaplain) on Feb 28, 2004 at 18:13 UTC
    could it be an authentication issue? IE, the user you are at the command line is permitted to send mail to the server, but the user the web server runs as is not? stab in the dark.
      If you're right, how to overcome this?

        If you're right, how to overcome this?

        have the cgi add a job to a queue ... the mail actually gets sent by a cron job reading from the queue running under the user account and not the webserver account