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

I'm trying to send an e-mail using the Net::SMTP module, but it runs into an error when it gets to the dataend method. The e-mail then isn't sent. What am I doing wrong? Here is my code:
use Net::SMTP; $mailhost = 'smtp.gmail.com'; $smtp = Net::SMTP->new($mailhost) || die "Error: $!"; print "\nWho would you like the e-mail to be from?"; $from = <STDIN>; chomp $from; print "\nWho would you like to send the e-mail to?"; $to = <STDIN>; chomp $to; print "\nWhat would you like the subject of your e-mail to be?\n"; $subj = <STDIN>; chomp $subj; print "\nWhat is your message?\n"; $message = <STDIN>; chomp $message; $smtp->mail($from); $smtp->to($to); $smtp->data(); #Start the data sending #These are the headers for the e-mail. Newline character signifies end + of headers. $smtp->datasend("From: $from\n"); $smtp->datasend("To: $to\n"); $smtp->datasend("Subject: $subj\n"); $smtp->datasend("\n"); $smtp->datasend("$message"); $smtp->dataend() || print "Dataend failed: $!"; #End the data sending $smtp->quit; #Close the smtp connection

Replies are listed 'Best First'.
Re: Can't get Net::SMTP to work
by mr_mischief (Monsignor) on Apr 14, 2008 at 20:39 UTC
    You're dealing with a mail host outside your own control. There's a good chance you're failing to communicate with it or you're getting a failure response from it.

    Take a look at Net::Cmd and in particular the code and message methods to see what the error might be.

    A common error when sending email through an SMTP outside your own network is that your ISP will firewall all port 25 traffic to any server other than their own SMTP servers they supply to you. This is a spam prevention measure, and depending on the size (small), the type of your connection (MAC authenticated with custom filtering possible), and service level (high) of your ISP, you might (but don't count on it) have a chance to get port 25 opened up for you by asking and promising not to send spam.

Re: Can't get Net::SMTP to work
by bingos (Vicar) on Apr 14, 2008 at 20:53 UTC

    It is always a good idea to use the Debug option for trying to hunt down problems with Net::SMTP

    $smtp = Net::SMTP->new($mailhost, Debug => 1) || die "Error: $!";
Re: Can't get Net::SMTP to work
by Metaphysical (Initiate) on Apr 14, 2008 at 21:05 UTC
    Ok, i turned the debug option on and i get this:
    Net::SMTP=GLOB(0x8174bb4)>>> MAIL FROM:<me@you.com> Net::SMTP=GLOB(0x8174bb4)<<< 530 5.7.0 Must issue a STARTTLS command f +irst. g3sm15050196wra.2 Net::SMTP=GLOB(0x8174bb4)>>> RCPT TO:<metaphysical33@gmail.com> Net::SMTP=GLOB(0x8174bb4)<<< 530 5.7.0 Must issue a STARTTLS command f +irst. g3sm15050196wra.2 Net::SMTP=GLOB(0x8174bb4)>>> DATA Net::SMTP=GLOB(0x8174bb4)<<< 530 5.7.0 Must issue a STARTTLS command f +irst. g3sm15050196wra.2 Net::SMTP=GLOB(0x8174bb4)>>> From: me@you.com Net::SMTP=GLOB(0x8174bb4)>>> To: me@gmail.com Net::SMTP=GLOB(0x8174bb4)>>> Subject: you Net::SMTP=GLOB(0x8174bb4)>>> da man Net::SMTP=GLOB(0x8174bb4)>>> . Net::SMTP=GLOB(0x8174bb4)<<< 502 5.5.1 Unrecognized command. g3sm15050 +196wra.2 Net::SMTP=GLOB(0x8174bb4)>>> QUIT Net::SMTP=GLOB(0x8174bb4)<<< 502 5.5.1 Unrecognized command. g3sm15050 +196wra.2