Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Net::SMTP Module query

by gilesy (Initiate)
on Nov 12, 2012 at 10:18 UTC ( [id://1003405]=perlquestion: print w/replies, xml ) Need Help??

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

Hi guys, I'm attempting to use the Net::SMTP module in order to send emails from a server that i'm remotely logging into. Please see the basic code I've set up below. I am attempting to get this working first before fleshing out the actual script.
#!/usr/bin/perl use Net::SMTP; use strict; use warnings; print "Content-type: text/html\n\n"; #print "test"; my $MailHost = "smtp.server.com"; my $MailFrom = "from\@address.com"; my $MailTo = "gilesy\@address.com"; my $subject = "Hello Gilesy"; my $MailBody = "This is the mail body"; #confirm connection with smtp server my $smtp_test = Net::SMTP->new('smtp.server.com', Timeout => 30, Debug => 1,)|| print "ERROR creating SMTP obj: $! \n"; print "SMTP obj created."; my $smtp = Net::SMTP->new($MailHost); # Send the From and Recipient for the mail servers that require it $smtp->mail($MailHost); $smtp->to($MailTo); # Start the mail $smtp->data(); # Send the header. $smtp->datasend("To: $MailTo\n"); $smtp->datasend("From: $MailFrom\n"); $smtp->datasend("Subject: $subject\n"); $smtp->datasend("\n"); # Send the message $smtp->datasend("$MailBody\n\n"); # Send the termination string $smtp->dataend(); $smtp->quit;
When I run this, I get the following output:
Net::SMTP>>> Net::SMTP(2.31) Net::SMTP>>> Net::Cmd(2.29) Net::SMTP>>> Exporter(5.64_01) Net::SMTP>>> IO::Socket::INET(1.31) Net::SMTP>>> IO::Socket(1.31) Net::SMTP>>> IO::Handle(1.28) Net::SMTP=GLOB(0xXXXXXXX)<<< 220 smtp.server.com Microsoft ESMTP MAIL +Service ready at Mon, 12 Nov 2012 09:56:34 +0000 Net::SMTP=GLOB(0xXXXXXXX)>>> EHLO localhost.localdomain Net::SMTP=GLOB(0xXXXXXXX)<<< 250-smtp.server.com Hello [12.34.56.78] Net::SMTP=GLOB(0xXXXXXXX)<<< 250-SIZE Net::SMTP=GLOB(0xXXXXXXX)<<< 250-PIPELINING Net::SMTP=GLOB(0xXXXXXXX)<<< 250-DSN Net::SMTP=GLOB(0xXXXXXXX)<<< 250-ENHANCEDSTATUSCODES Net::SMTP=GLOB(0xXXXXXXX)<<< 250-STARTTLS Net::SMTP=GLOB(0xXXXXXXX)<<< 250-X-ANONYMOUSTLS Net::SMTP=GLOB(0xXXXXXXX)<<< 250-AUTH NTLM Net::SMTP=GLOB(0xXXXXXXX)<<< 250-X-EXPS GSSAPI NTLM Net::SMTP=GLOB(0xXXXXXXX)<<< 250-8BITMIME Net::SMTP=GLOB(0xXXXXXXX)<<< 250-BINARYMIME Net::SMTP=GLOB(0xXXXXXXX)<<< 250-CHUNKING Net::SMTP=GLOB(0xXXXXXXX)<<< 250-XEXCH50 Net::SMTP=GLOB(0xXXXXXXX)<<< 250 XRDST SMTP obj created.
From what I can see, there are no errors here. However, when I run this, I do not get any emails. This could be to do with server security but I guess I want to rule out that my code is the issue. So could anyone confirm if my code is ok? If so, would you have any suggestions as to what might be causing the issue?

Replies are listed 'Best First'.
Re: Net::SMTP Module query
by karlgoethebier (Abbot) on Nov 12, 2012 at 11:54 UTC
    perl -c mail.pl Possible unintended interpolation of @address in string at mail.pl lin +e 11. Possible unintended interpolation of @address in string at mail.pl lin +e 12. Global symbol "@address" requires explicit package name at mail.pl lin +e 11. Global symbol "@address" requires explicit package name at mail.pl lin +e 12.
    my $MailFrom = "from\@address.com"; my $MailTo = "gilesy\@address.com";

    Try this. Didn't check the rest. Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

      Hi Karl, Thanks for the swift reply. I actually edited my code when posting to this site and do have that code in my script
      my $MailFrom = "from\@address.com"; my $MailTo = "gilesy\@address.com";
      ..... but the email isn't being sent. I'm getting the output I displayed in original post.

        Update:

        Sorry, the first line was wrong for you. But i think you noted this.

        #!/usr/bin/perl use Net::SMTP; use strict; use warnings; print "Content-type: text/html\n\n"; my $MailHost = "mailhost"; my $MailFrom = "someone\@somewhere"; my $MailTo = "someone\@somewhere"; my $subject = "Super!\n"; my $MailBody = "It works!\n"; my $smtp = Net::SMTP->new($MailHost) || die $!; $smtp->mail($MailFrom); $smtp->to($MailTo); $smtp->data(); $smtp->datasend("To: $MailTo\n"); $smtp->datasend("From: $MailFrom\n"); $smtp->datasend("Subject: $subject\n"); $smtp->datasend("\n"); $smtp->datasend("$MailBody\n\n"); $smtp->dataend(); $smtp->quit;

        Please try this. Regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1003405]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-23 09:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found