Thursday, February 08, 2007

Perl ERROR: Can't call method "mail" on an undefined value at smtp.pl line 10.

As confusing at it sounds, this means your smtp object has not been sucessfully constructed. I know the error message reads more like a problem with the "mail" method, but it is actually because the SMTP obj was not initialized.


Try this:
#!/usr/bin/perl -w
use Net::SMTP;
$smtp_test = Net::SMTP->new('smtp.mail.com',
Timeout => 30,
Debug => 1,)|| print "ERROR creating SMTP obj: $! \n";
print "SMTP obj created.";

If you see "ERROR createing SMTP obj" then you know the SMTP object is not created correctly.


Some times, this is because the SMTP package was not installed right, some times, it may because the SMTP protocal is blocked, sometimes it means something else is wrong.

6 comments:

Anonymous said...

In my case, McAfee was blocking outbound tcp/25 from my server.

A simple 'telnet mailhost 25' showed me the issue.

OCComputer said...

Thanks! This was exaclty my problem. I was getting a "Connection refused" from the SMTP server I had designated in the object creation.

pauline said...

Thanks for the explanation! Although I do not really understand why it is not working. I try your test, but I get nothing...neither an ERROR nor an obj created.
Do you know what that could mean?

Alan Tan said...

hi Pauline,
You may have a problem more than SMTP.
If your Perl gave you error "can't call method mail on an undefined value at smtp.pl line 10", but not showing anything for my test, you'd better check your perl run time.
The test script may not have been run at all, or output may be redirected to some where.
check perl's return code and check logs.

You may want to do perl -d to debug the testing script.

Anonymous said...

ERROR creating SMTP obj: Connection timed out

dwaler said...

Thanks for this post. When enabling debug, I found out I had made a mistake while defining my mailhost (' instead of "). This post saved me further head-ache!