Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Net::SMTP halts script

by nperrins (Initiate)
on Jul 23, 2014 at 09:39 UTC ( [id://1094744]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Guys,

This is not something I had ever considered before, but I can find no solution on the web.

We have a script that loops round, does some work and sends an email with Net::SMTP. However, if the SMTP->new cannot find the mailhost, it dumps out of the whole script with an undefined mailhost error.

An example of this is a simple while ( $x < 4 ) loop round an SMTP email and a print $x gives 4 emails. Change the mailhost to an incorrect name and it crashes with an "undefined" error...but doesn't go past loop 1.

Is there any way of catching this and skipping it?

(I know the answer is to get the mailhost name correct, but with Microsoft now blocking port 25 to relay, users can change their relay servers if and when - so having a change in relay crashing your programs is not good.)

Replies are listed 'Best First'.
Re: Net::SMTP halts script
by Athanasius (Archbishop) on Jul 23, 2014 at 09:55 UTC

    According to the documentation, on failure new returns undef and $@ contains an appropriate error message. This seems to work as advertised for me:

    19:50 >perl -MNet::SMTP -wE "my $smtp = Net::SMTP->new('invalid'); say + 'Next'; if (defined $smtp) { say 'SMTP defined' } else { say 'SMTP u +ndefined'; say $@; } say 'Still alive';" Next SMTP undefined Net::SMTP: Bad hostname 'invalid' Still alive 19:50 >p5u v Net::SMTP Net::SMTP C:\Perl\Strawberry\strawberry-perl-5.20.0.1-64bit-PDL\perl\lib +\Net\SMTP.pm: 2.33 19:50 >

    Are you using an old version of the module? Are you sure it’s Net::SMTP->new() that’s causing the crash? Please give a minimal but self-contained script demonstrating the problem, together with the exact error message(s) that result.

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      The version of perl you're using may come into play. If you're on a production machine and want to upgrade, I highly recommend Perlbrew.

      The more direct method of dealing with this is wrapping the call in an eval and checking the result in $@ afterward to see if you had success.

        Thanks. I will take a look.

      Using Fedora Core 20. Perl v5.18.2. Compiled Jan 14.

      When I say "crash", I simply mean it halts the program to sent the email giving the error

      "Mon Aug 18 14:44:47 2014 bb.cgi: Can't call method "mail" on an undefined value at bb.cgi line 85."

      Even though my $INVALID is defined as "duffhost.duffhost.com".

      If I add anything after this code , it does not run.

      If I replace $INVALID with $GATEWAY (a real address) it works fine.

      If you put two together, the working one first, it runs, sends the first email and then halts.If you put two together, the working one last, it halts.

      ---------

      $smtp = Net::SMTP->new("$INVALID", Debug => 3 );

      $smtp->mail("$FROM");

      $smtp->to("$EMAIL");

      $smtp->cc("$USEREMAIL");

      $smtp->data();

      $smtp->datasend("From: $FROM \n");

      $smtp->datasend("To: $EMAIL \n");

      $smtp->datasend("Cc: $USEREMAIL\n");

      $smtp->datasend("Subject: $SUBJECT \n");

      $smtp->datasend("\n");

      $smtp->datasend("$MAILTEXT \n");

      $smtp->datasend("\n");

      $smtp->dataend();

      $smtp->quit;

        As I showed above, Net::SMTP->new() returns undef on failure. If you then try to call a method on this undef value, you effectively have undef->mail($FROM);of course this method call will die! You need an explicit test for undef, to ensure that the mail(), to(), cc(), etc., methods are only ever called on defined objects. For example (untested):

        my $smtp = Net::SMTP->new($INVALID, Debug => 3); if (defined $smtp) { $smtp->mail($FROM); $smtp->to($EMAIL); $smtp->cc($USEREMAIL); $smtp->data(); $smtp->datasend("From: $FROM \n"); $smtp->datasend("To: $EMAIL \n"); $smtp->datasend("Cc: $USEREMAIL\n"); $smtp->datasend("Subject: $SUBJECT \n"); $smtp->datasend("\n"); $smtp->datasend("$MAILTEXT \n"); $smtp->datasend("\n"); $smtp->dataend(); $smtp->quit; } else { print $@, "\n"; }

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-18 19:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found