在 for 循环中使用 Net::SMTP 时,我遇到了一些问题。第一次迭代有效,但在第二次迭代中,脚本因无法在未定义的值上调用方法“mail”而死...,该行称为
$smtp->mail('hacker@hacker.net'); #from.
有任何想法吗?!谢谢你的时间!
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) {
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;
}