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

hey monks...I come seeking wisdom (again!)..

I have a dsl connection at home that only allows dynamic IP. so I am working on a script that monitors what my ip is and then emails me when it changes. The script runs fine most of the time, but every so often I get the following error:

"can't call method 'auth' on an undefined value at.....line 85"

$smtp=Net::SMTP->new($out_server); $smtp->auth($username,$pwd); $smtp->mail($smtp_usr); $smtp->recipient($goto); $smtp->data(); $smtp->datasend("To: $goto\n"); $smtp->datasend("\n"); foreach $line (@text) {$smtp->datasend($line);} $smtp->datasend("$curr_month/$curr_dom/$curr_year/$curr_hours:$cur +r_min:$curr_sec\n"); $smtp->dataend(); $smtp->quit;
all of the variables are defined by the time it gets here, and it usually works, but sometimes it crashes and I don't know what the difference is....any ideas?


thanx
-Kevin

Replies are listed 'Best First'.
Re: Net::SMTP error
by pg (Canon) on Dec 11, 2003 at 20:08 UTC

    Obviously you failed to connect to the server in those bad cases.

    It is always a better practice to check return code on eah step. You cannot expect that the connection works all the time, but you should expect your code to handle exceptional situations.

    BTW, better specify timeout when you create SMTP:

    $smtp = Net::SMTP->new('mailhost', Timeout => 60);
Re: Net::SMTP error
by Gerard (Pilgrim) on Dec 11, 2003 at 20:07 UTC
    I am no expert on this, but I would assume that  $smtp=Net::SMTP->new($out_server); is failing. A possible reason for this could be transient conditions. In my experience, dsl connections drop off more than you expect, and you could be trying when you have no connection to the internet?

    Try and do some error checking, on the first line of your code, to see if it connects before continuing to authenticate, etc.
    hope this helps
    Regards,
    Gerard
Re: Net::SMTP error
by K_M_McMahon (Hermit) on Dec 11, 2003 at 20:46 UTC
    So if should try something like?
    if (!$smtp=Net::SMTP->new($out_server)) {try again later;}


    -kevin
      Sounds good to me. Personally I do
      my $smtp = Net::SMTP->new($smtpServer, Timeout => 60) || &do_some_stuf +f $!;

      But whatever works for you
      Regards,
      Gerard
        Ummm, perhaps you meant to use 'or' instead of '||' here. '||' has higher precedence than '=', where 'or' has lower precedence than '=', so it is not doing what you think it's doing when you interchange between the two.

        In you code you had -
        my $smtp = Net::SMTP->new($smtpServer, Timeout => 60) || &do_some_stuf +f $!;
        where I assume that &do_some_stuff does some error handling. May be you are not aware, it is actually doing this instead -
        my $smtp = (Net::SMTP->new(....) || &do_some_stuff...);
        if the object creation fails, your $smtp ends up with whatever is returned from 'do_some_stuff'.

        So the more correct way to do error handling here should be -
        my $smtp = Net::SMTP->new(....) or &do_some_stuff $!;
        where $smtp is assigned to the new object first, and if it is undefined because of failed connection, then the 'do_some_stuff' error handling will kick in.

        Thanx for your help!!

        -Kevin