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

I've been using Net::SMTP to send an e-mail with my build results. It's been working fine for over a year. I recently setup a developer build box and I'm trying to re-use my scripts on this new machine. I'm having trouble getting Net::SMTP to work on this new machine. I keep getting the following message when I run the script and it's driving me nuts:

"Can't call method "mail" on an undefined value at C:\source\Release_Build_Scripts\sendmail2.pl line 414, <$fh> line 3."

I can't for the life of me figure out what's not working. I'm logging in as a different user on this machine than I do on my build box. So I even tried logging in as myself on this new box, and running the script, thinking maybe it was a user permission problem with the SMTP server, but I got the same error. I've ported this script to another box before so I don't know why this is any different. Any help or a point in the right direction would be greatly appreciated.

The snippet of code that uses Net::SMTP is below (I removed superfluous lines). Note that I tried inserting a print command after the "$smtp = Net::SMTP->new" line and it said that the value $smtp was undefined so I'm guessing this line is the problem. Note that the error message says line 414, it's refering to the "$smtp->mail($username)" line.
$username = $ENV{"USERNAME"}; tie @MailingList, 'Tie::File', "release_list.txt" or die "Cannot open build number file: $!"; $num_recips = @MailingList; for ($j=0; $j<$num_recips; $j++) { $tolist = $MailingList[$j]; $smtp = Net::SMTP->new('smtpmail.server.com'); $smtp->mail($username); $smtp->to($tolist); $smtp->data(); $smtp->datasend("To: $tolist\n"); $smtp->datasend("From: newuser\@server.com\n"); $smtp->datasend("Subject: Build $bldmsg\n"); $smtp->datasend("\n"); $smtp->datasend("BUILD SUMMARY:\n"); $smtp->datasend("\n"); $smtp->dataend(); $smtp->quit; }

Replies are listed 'Best First'.
Re: Trouble getting Net::SMPT to work
by deibyz (Hermit) on Apr 20, 2005 at 13:26 UTC
    It seems that you can't connect to the specified mail server, so the Net::SMTP object isn't created. You should check the return value of new:

    $smtp = Net::SMTP->new('smtpmail.server.com') or die "Couldn't connect to SMTP server\n";
    Make sure you have acces to the server.
Re: Trouble getting Net::SMPT to work
by bofh_of_oz (Hermit) on Apr 20, 2005 at 13:32 UTC
    For debugging, I would first suggest using strict and warnings - that might be of some help when you are going through the code...

    Another thing, use debug in the SMTP initialization as well:

    $smtp = Net::SMTP->new('smtpmail.server.com', Hello => 'company.com', Timeout => 30, Debug => 1); #Set to 0 in production version
    When you run the script with the debug, you should see much more output, hopefully showing you where your problem is...
Re: Trouble getting Net::SMPT to work
by gellyfish (Monsignor) on Apr 20, 2005 at 13:33 UTC

    I would suspect that the mail server is not accepting your connection for some reason - to be sure you can turn on the debugging output in the constructor by changing it to:

    $smtp = Net::SMTP->new('smtpmail.server.com', Debug => 1);
    If this does not produce any output then it is likely that there is a network problem - either the address of the mail server cannot be resolved or you cannot connect to the server for some other reason. You could try connecting to port 25 of the server using telnet from the command line.

    /J\

      I had the debug flag in and got no additional output. I tried to telnet to port 25 of the SMTP server and I got back:

      "Connecting To smtpmail.server.com...Could not open connection to the host, on port 25: Connect failed"

      When I did the same thing on another box, logged in as myself I can connect:

      "220 ips-dc87-xch.server.com ESMTP Server (Microsoft Exchange Internet Mail Service 5.5.2657.72) ready"

      So it does look like some sort of network problem with this new box or new user. I'm working with my local IT folks to try and figure it out. Thanks for your help.
        Thanks for everyone's help. It turns out that by Default, McAfee VirusScan was blocking sending mail using Port 25 of the SMTP mail server. I had to allow access to that port.
Re: Trouble getting Net::SMTP to work
by Transient (Hermit) on Apr 20, 2005 at 13:21 UTC
    how about trying a simple:
    #!/usr/bin/perl use warnings; use strict; use Net::SMTP; my $smtp = Net::SMTP->new('smtpmail.server.com'); die "Unable to create smtp!\n$!\n" unless defined($smtp); $smtp->quit;
    BTW, you might want to change the spelling in your title to be SMTP. ;) (not trying to being anal/picky, but to assist searches)
      Transient wrote...
      >(not trying to being anal/picky...

      That sounds positively hemorrhoidal.