开发者

How can I debug my Net::SMTP Perl program?

I'm building a application that sends a test message to another email, the program executes without errors, but when I check my email there isn't any new email, take a look at my code:

my $smtpserver = 'smtp.vix.terra.com.br';
my $smtpuser = 'na开发者_如何学JAVAthanpc';
my $fromemail = 'nathanpc@terra.com.br';

    my $smtp = Net::SMTP-> new($smtpserver, Timeout => 120);
    $smtp-> mail($smtpuser);
    $smtp-> to('eeepc904@gmail.com');
    $smtp-> data();
    $smtp-> datasend("To: eeepc904\@gmail.com\n");
    $smtp-> datasend("From: nathanpc\@terra.com.br\n");
    $smtp-> datasend("\n");
    $smtp-> datasend("test\n");
    $smtp-> dataend();
    $smtp-> quit;


Just because you didn't get the email doesn't mean the email wasn't sent. It could be that it hasn't been delivered yet, or it was delivered and was filtered, or many other things.

There are many, many things that can go wrong with email.

  • Where's the part of the script with warnings and strict enabled, and you load Net::SMTP? Help yourself with those before running to Stackoverflow.
  • Why don't you check that you were able to connect to the mail server?
  • Why haven't you enabled the Debug option in your call to new?
  • Were there any warnings or error messages?
  • What happens when you try the same SMTP conversation by manually connecting to the server? Post the entire transcript.

There is a lot that you can do to help yourself before asking here, and relying on Stackoverflow for even the most basic questions doesn't give you a chance to develop your own skills.

#!perl

use warnings;
use strict;

use Net::SMTP;

my $smtpserver = 'smtp.vix.terra.com.br';
my $smtpuser   = 'nathanpc';
my $fromemail  = 'nathanpc@terra.com.br';

my $smtp = Net::SMTP->new($smtpserver, Timeout => 10, Debug => 1);
die "Could not connect to server!\n" unless $smtp;

$smtp->mail($smtpuser);
$smtp->to('eeepc904@gmail.com');
$smtp->data();
$smtp->datasend("To: eeepc904\@gmail.com\n");
$smtp->datasend("From: $fromemail\n");
$smtp->datasend("\n");
$smtp->datasend("test\n");
$smtp->dataend();
$smtp->quit;


I think this should be fine

    use Net::SMTP;                          # includes NET:SMTP Moduls

$mailServer  = "mail.server.com";       # Name of  SMTP Servers.

$nachricht   = "nachricht.txt";         # Message
$absender    = "absender\@hier.com";    # Sender Email Adress.
$betreff     = "Neue Nachricht";        # Subject
$empfaenger  = "empfaenger\@dort.com";  # reciver Email Adress



$smtp = Net::SMTP->new($mailServer);    # Create New  SMTP Objekt.
                                        # Parameter is the  Name of SMTP
                                        # Server.

$smtp->mail($absender);                 

$smtp->to($empfaenger);                 
                                        .

$smtp->data();                          

$smtp->datasend("Subject: $betreff\n");

$smtp->datasend("To: $empfaenger\n");

$smtp->datasend("\n");                  

close MESSAGE;

$smtp->dataend(); 

$smtp->quit;  


Just sniff the traffic to see if there is any SMTP traffic (default port is 25). If you see there is and it corresponds to what you sent (with no errors), you are fine as far as your code is concerned. Your code can't be responsible for what happens after it has successfully been sent (250 Ok: queued...).


Just call:

$smtp->debug(1);

after creating smtp object to get nice transcription of whole session to console.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜