Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5350
  • Last Modified:

getting error Can't call method "mail" on an undefined value


when i run my sendmail.pl script from xyz server, it runs fine and sends the email. but when i run from different server "abc", it give me following error:
Can't call method "mail" on an undefined value at line......

when i go to script that line contains: " $smtp->mail($senderEmail);  "

my script:

sub sendMail(){
    my ($message, $subject) = @_;
    my $smtp;
    print "Content-type: text/html\n\n";

    $smtp = Net::SMTP->new($mailServer); # connect to an SMTP server
    $smtp->mail($senderEmail);           # use the sender's address here
    $smtp->to($recipientEmail);          # recipient's address
    $smtp->data(); # Start the mail

 # Send the header.
    $smtp->datasend("To: $recipientEmail\n");
    $smtp->datasend("From: $senderEmail\n");
    $smtp->datasend("Subject: $subject\n");

    $smtp->datasend("\n");               # Send the body.

    $smtp->dataend();                        # Finish sending the mail
    $smtp->quit;                             # Close the SMTP connection -ab
  • 9
  • 7
1 Solution
One of the reasons is that the object $smtp was not created on a different server, because that server cannot access $mailServer

try to ping the $mailServer value from Server "abc" and see if it can properly communicate with the SMTP  server.
This line:
$smtp = Net::SMTP->new($mailServer); # connect to an SMTP server

Did not execute properly and did nto create an Object.

I would modify the script to look lile:

$smtp = Net::SMTP->new($mailServer); # connect to an SMTP server

if (defined $smtp)
# Do all the SMTP Processing Here
} else {
        print "Could Not Connect to Server: $mailServer";
shahrahulbAuthor Commented:
sstouk u r right. i cannot connect to server.

is there any solution to this problem??

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

shahrahulbAuthor Commented:
also if i use
mailx command to send email it works from the same server where my perl script cannot send the email

which means i can definitely send email from this server
From this point it is not a scripting problem any longer - it is a Networking issue, so it should be resolved accordingly.

Check the Host Name resolution and the DNS Name resolution.
What if you use IP address, instead of the Host Name of the SMTP Server on the abc Computer?

From the xyz, ping the SMTP server and find what its IP address is.
Then use it on abc server and ping SMTP Server by IP.
If it works, then use the IP instead of the Host Name in your script.

If it does not work, check why you cannot communicate with SMTP Server from abc computer, what is different about these two computers in terms of Default Gateway IP address, Network Segment, IP Scope etc. Is it behind the firewall and the other computer is not?

Oops, you said that you can send an e-mail from abc using mailx...?
Does the mailx using IP and script uses host name?

shahrahulbAuthor Commented:
the mailx command i use is :

cat filename | mailx -s "This is test"  rahulshah@blahblah.com

it does send the email to my email address.

why does the script fails
Double-check that tiyr $mailServer variable does not contain any New Line or Space characters...
Imbed the IP address of the SMTP Server directly into your script as in:
$smtp = Net::SMTP->new(''); # connect to an SMTP server

Where is just an example of your SMTP Server IP address...
shahrahulbAuthor Commented:
how do i find ip address of mail server. is there a perl script which returns ip address when given hostname
No, you just need to ping the SMTP Server from the xyz computer.
Use "ping" command from the command prompt.
Are you on UNIX or Windows?

I did not mean there were no scripts in Perl that do it, I meant that it is simple to do without any scripts...
If you are on Windows:
Click "START" -> "Run"
Type "cmd" and click "OK"
the command prompt window will appear

Type "ping -a SMTPSERVERNAME" hit Enter

Where SMTPSERVERNAME is a name of your SMTP Server

You should see something like:

pinging ...

shahrahulbAuthor Commented:
i wrote a small script to find ip address:

use IO::Socket;
use Net::SMTP;

$hostname="mail.blah.com"; #change this to your hostname
print "$addr\n";

will let u know as soon i try this
shahrahulbAuthor Commented:
after entering the ip address on abc machine
$smtp = Net::SMTP->new('11.111.1.......); # connect to an SMTP server

i still get same error. even ip address is not working.
is there a way to find out what mail server is my machine abc using

Could you "Ping" your SMTP Server from command prompt on abc Computer?
Ping it using the Host Name and after that using the IP address, are both Ping command give a successful responce from SMTP Server or is there an error?

shahrahulbAuthor Commented:
No i cannot ping using ip and hostname on abc. it only works on xyz
i think so abc is using different smtp server. is there a way i can find what smtp server abc is using?

Smtp Server is not in a Server Configuration.
There is no specific SMTP Server for a particular Server.
SMTP Server is configured inside the Application which communicates with it.
If you have an application that works by communicating with SMTP Server on abc - check that application's configuration to find it out, nothing to do with the server itself.
> There is no specific SMTP Server for a particular Server.
Actually, some Servers do restrict where SMTP connections can go.  (or come from)

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 9
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now