Hi, I'm having some trouble with Net::SMTP when used in a for loop.
The first iteration works, but on the second iteration the script dies with Can't call method "mail" on an undefined value at..., the line referred to being $smtp->mail('hacker@hacker.net'); #from.
Any ideas?! Thanks for your time!


use strict; use warnings; use Net::SMTP; my $smtp; #@data is defined and populated somewhere foreach my $line (@data) { my @linearray=split(/,/,$line); my $host=$linearray[2]; $host =~ s/\r|\n//g; next unless ($host =~ m/([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3}).([0- +9]{1,3})/); # skip if it's not an IP (i.e. it's the header row) print "Connecting to $host..."; # create object $smtp = Net::SMTP->new( Hello => 'hacker.net', Timeout => 3, Host => $host, Debug => 1, ); $smtp->mail('hacker@hacker.net'); #from my $tocheck=$smtp->recipient('my_email@sanitised.com'); #to if ($tocheck==0) { #check if it doesn't like that 'to' address print "$host is NOT an open relay\n"; #$smtp->quit; next; } $smtp->data(); $smtp->datasend("Test\n"); $smtp->datasend("\n"); $smtp->datasend("A simple test message\n"); $smtp->dataend(); $smtp->quit; }

RESOLVED: Resolved. Tail slightly between the legs on this one. It turns out that the list of IPs given to me had already been contacted about the issue and therefore some has started putting measures in place to fix the problem. One such measure was blocking TCP port 25 with a source IP different to a trusted relay (not my IP just to clarify). Therefore creation of the Net::SMTP was simply failing. So reintroduction of the block to test creation of the object (which I'd removed while trying to debug this issue) fixed it all up. Thanks for your help guys - much appreciated.


In reply to Net::SMTP undef weirdness by noisey

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.