我正在尝试从 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 上启动它时没有出现错误。
最佳答案
您不要在电子邮件的标题中设置邮件服务器和端口;而是将其设置在 %mailcfg
中哈希:
use Mail::Sendmail qw(sendmail %mailcfg)
$mailcfg{port} = 1025; #localhost is already the default server...
关于Perl Mail::Sendmail 无法连接到 SMTP 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11527717/