Perl Mail :: Sendmail无法连接到SMTP服务器

时间:2012-07-17 17:27:53

标签: perl smtp sendmail

我正在尝试从perl脚本发送电子邮件,但我很难连接到我的SMTP服务器。我正在使用MailCatcher- https://github.com/sj26/mailcatcher/ - 运行默认设置(localhost:1025)。

MailCatcher正在运行,我可以使用其他服务向其发送邮件。什么可能是错的任何线索? (还尝试使用Net :: SMTP发送邮件,但也无法连接)

print "Testing Mail::Sendmail version $Mail::Sendmail::VERSION\n";

my %mail = (
  To      => 'temp@test.com',
  From    => 'test@test.com',     
  Subject => 'Test message'
);

$mail{server} = 'localhost:1025';

$mail{'mESSaGE : '} = "The message key looks terrible, but works.";

print Dumper(%mail);

if (sendmail %mail) { print "Mail sent OK.\n" }
else { print "Error sending mail: $Mail::Sendmail::error \n" }

输出

Testing Mail::Sendmail version 0.79
Default server: localhost
Default sender: 
$VAR1 = 'Subject';
$VAR2 = 'Test message';
$VAR3 = 'server';
$VAR4 = 'localhost:1025';
$VAR5 = 'To';
$VAR6 = 'temp@test.com';
$VAR7 = 'message : ';
$VAR8 = 'The message key looks terrible, but works.';
$VAR9 = 'From';
$VAR10 = 'test@test.com';
Error sending mail: connect to localhost failed (Connection refused)
connect to localhost failed
connect to localhost failed (Connection refused)
connect to localhost failed
connect to localhost failed (Connection refused) no (more) retries!

编辑:

原来,mailcatcher在另一个端口上运行,并且当我试图在25上启动它时没有出错。

2 个答案:

答案 0 :(得分:3)

您不要在邮件中的标题中设置邮件服务器和端口;而是将其设置在%mailcfg哈希:

use Mail::Sendmail qw(sendmail %mailcfg)
$mailcfg{port} = 1025; #localhost is already the default server...

答案 1 :(得分:1)

你可以尝试这个..它对我有用..

use strict;
use warnings;
use Email::Sender::Simple qw(sendmail); 
use Email::Sender::Transport::SMTP(); 
use Email::Simple();   
use Email::Simple::Creator();       

my $smtpserver = 'server';
my $smtpport = 25;      
my $smtpuser = 'User_Name';   
my $smtppassword = 'pass';         
my $transport = Email::Sender::Transport::SMTP->new({   
    host => $smtpserver,  
    port => $smtpport,            
    sasl_username => $smtpuser,       
    sasl_password => $smtppassword,   
});            

my $email = Email::Simple->create(header => [
        To      => 'me@example.com',
        From    => 'sender@example.com',
        Subject => 'Hello there!',
    ],  body => "This is one actually worked !!\n",             
);             

sendmail($email, { transport => $transport });
?>