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

Hi,

I cannot work out why the following script does not work

#!/usr/bin/perl -wT use Net::SMTP; $smtp = Net::SMTP->new('mailservername.net', Debug => 1, ); $smtp->mail('peter@mydomain.com'); $smtp->to("peter\@home.emailaddress.com"); $smtp->data(); $smtp->datasend("To: Peter\n"); $smtp->datasend("\n"); $smtp->datasend("test send\n"); $smtp->dataend(); $smtp->quit;

There are no error messages, and no email received, yet when I use the (NMS) FormMail.pl on the same domain (my feedback form), I get an email back in one minute (I'm using Perl 5.006001). The script is starting to 'talk' to the mail server, because when I try to get the server 'banner', it gives me:

"mailservername.net ESMTP Exim 4.20 #1 Sat, 04 Oct 2003 01:28:37 -0400 "

Are there any methods to debug this script ? To somehow see what the email server is doing, any way to get 'echos' displayed, or any modules to add to give me debug information (The "error_log" file is empty, btw) Are there any issues with the code that could make it not work ?

Peter

Replies are listed 'Best First'.
Re: Net::SMTP and debugging ?
by demerphq (Chancellor) on Oct 04, 2003 at 12:17 UTC

    Im guessing that you need to authenticate yourself with the SMTP server. Without seeing the full output of the conversation with the server (what you posted is not it) its hard to tell whats going wrong.

    I suspect that your problem with debugging this may be that you are running this as a CGI and that Net::SMTP (because of its Net::CMD heritage) writes the conversation with the SMTP server to STDERR. I think you need to use CGI::Carp to get this to be redirected to your webserver error logs.

    As a last point you may want to just go and install an email handling module like MIME::Lite and save yourself the hassle of talking to the SMTP server directly. However if authentication is the problem then MIME::Lite will unfortunately not be directly suitable until 3.02 comes out. However I am working on it.


    ---
    demerphq

      First they ignore you, then they laugh at you, then they fight you, then you win.
      -- Gandhi


      Im guessing that you need to authenticate yourself with the SMTP server. Without seeing the full output of the conversation with the server (what you posted is not it) its hard to tell whats going wrong.

      I will find out if I need to supply authentication. What I posted was all I saw in conversing with the server, nothing else was echoed back

      ... writes the conversation with the SMTP server to STDERR. I think you need to use CGI::Carp to get this to be redirected to your webserver error logs.

      Is there any method to get STDERR, at least, get echoed back to the terminal ? I will check out CGI::Carp, thanks.

      Does MIME::Lite handle attachments ? Wow, there are far too many modules for sending email, which one is easy/simple for a Perl novice like me. :)

      Thanks,

      Peter

      Hi,

      Thanks for putting me onto CGI::Carp, it has been great to be able to see _where_ the script is crashing. I just directed the log file to a path I could view via a browser, as SSH access take foreever to load.

      I've now been able to use Net::SMTP successfully, once I put "localhost" as the domain/mailserver, it worked like a charm. The only problem I found was I could not do this

      $from = 'sales@domain.com'; $toaddress = 'fredflintstone@disney.com'; $name = 'Fred Flintstone'; $smtp->mail('$from\n"); $smtp->to("$toaddress\n");

      but got around the problem by putting email headers like this

      $smtp->data(); $smtp->datasend("To: $name <$toaddress>\n"); $smtp->datasend("Subject: Confirmation and Thankyou\n"); $smtp->datasend("X-Order-IP: $ENV{REMOTE_ADDR}\n"); $smtp->datasend("\n"); $smtp->datasend("Hi $name,\n");

      Peter

        Glad CGI::Carp was helpful. However I must say that I strongly discourage hand generating SMTP mails. Use MIME::Lite instead. Let it do the work of making sure things are set up correctly.

        The new version will have much more powerful features for handling SMTP transport, but you dont need any of those features from what I can tell.


        ---
        demerphq

          First they ignore you, then they laugh at you, then they fight you, then you win.
          -- Gandhi