Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Net::SMTP and the undefined value

by Quicksilver (Scribe)
on Jul 30, 2007 at 14:22 UTC ( #629570=perlquestion: print w/replies, xml ) Need Help??

Quicksilver has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I'm putting together a form to mail some data to one of users and am using Net::SMTP to mail them. However I keep coming across an error message whilst testing saying that I have an undefined value in $smtp->mail( $from ); but I'm sure I have defined this further up the form. I would be grateful if somebody could point out what I have missed as this is the first time I've used this module and I'd like to leanr how to make it work properly. Many thanks.
#! c:\perl\bin\perl.exe use strict; use warnings; use cgi; use Net::SMTP; #constants my $mailserver_url = ""; my $to = "foo\@bar"; my $from = "bar\@foo"; my $subject = "Mail time"; my $smtp = Net::SMTP->new('$mailserver_url'); #Sending the message $smtp->mail( $from ); $smtp->to( $to ); $smtp->data(); $smtp->datasend("To: $to\n"); $smtp->datasend("From: $from\n"); $smtp->datasend("Subject: $subject\n"); $smtp->datasend("\n"); # done with header $smtp->datasend($name, $company, $country, $email); $smtp->dataend(); $smtp->quit(); # all done. message sent.

Replies are listed 'Best First'.
Re: Net::SMTP and the undefined value
by moritz (Cardinal) on Jul 30, 2007 at 14:27 UTC
    The undef value is probably $smtp, because '$mailserver_url' is not a valid URL.

    Try it without the single ticks, and check the return value of Net::SMTP->new()

      Moritz, Many thanks, it seems to have done the trick. Iain
Re: Net::SMTP and the undefined value
by vkhera (Novice) on Jul 30, 2007 at 20:57 UTC
    After every $smtp->methodname() call, add some error checking. Like this:

     $smtp->mail($from) or die $smtp->message();

    and then you'll see exactly where your error is. Visual inspection of your code seems correct. It would be more helpful also if you show the actual error message you got *and* where you got it. There is no error reporting in the code you posted, so nobody knows for sure what is happening to you.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://629570]
Approved by ikegami
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2021-06-24 21:02 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (132 votes). Check out past polls.