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

Hi,

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->datasend("$message\n");

    $smtp->dataend();                        # Finish sending the mail
    $smtp->quit;                             # Close the SMTP connection -ab
}
LVL 1
shahrahulbAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sstoukCommented:
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.
0
sstoukCommented:
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";
exit;
};
0
shahrahulbAuthor Commented:
sstouk u r right. i cannot connect to server.

is there any solution to this problem??

0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

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
0
sstoukCommented:
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?

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

0
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
0
sstoukCommented:
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('172.20.212.111'); # connect to an SMTP server

Where 172.20.212.11s is just an example of your SMTP Server IP address...
0
shahrahulbAuthor Commented:
how do i find ip address of mail server. is there a perl script which returns ip address when given hostname
0
sstoukCommented:
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?

0
sstoukCommented:
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 ... 172.20.212.111


0
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
my($addr)=inet_ntoa((gethostbyname($hostname))[4]);
print "$addr\n";

will let u know as soon i try this
0
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

Rahul
0
sstoukCommented:
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?


0
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?

Rahul
0
sstoukCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ozoCommented:
> There is no specific SMTP Server for a particular Server.
Actually, some Servers do restrict where SMTP connections can go.  (or come from)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.