Link to home
Start Free TrialLog in
Avatar of shahrahulb
shahrahulb

asked on

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
}
Avatar of Sergy Stouk
Sergy Stouk
Flag of Canada image

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";
exit;
};
Avatar of shahrahulb
shahrahulb

ASKER

sstouk u r right. i cannot connect to server.

is there any solution to this problem??

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?

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

Where 172.20.212.11s is just an example of your SMTP Server IP address...
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 ... 172.20.212.111


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


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
ASKER CERTIFIED SOLUTION
Avatar of Sergy Stouk
Sergy Stouk
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ozo
> There is no specific SMTP Server for a particular Server.
Actually, some Servers do restrict where SMTP connections can go.  (or come from)