使用Mail :: Sendmail使用Windows机器从perl脚本发送电子邮件时出错

时间:2012-08-06 17:18:44

标签: windows perl email sendmail

我有一个简单的测试脚本,用于使用cpan模块Mail :: Sendmail向我自己发送电子邮件。我在使用Strawberry Perl的Windows机器上运行并通过命令行,一切似乎都很好。我收到一条错误消息connect to localhost failed (No connection could be made because the target machine refused it.)

我的脚本是:

use Mail::Sendmail qw(sendmail %mailcfg);
$mailcfg{from} = 'dhagan@idatech.com';

print "Testing Mail::Sendmail version $Mail::Sendmail::VERSION\n";
print "Default server:  $Mail::Sendmail::mailcfg{smtp}->[0]\n";
print "Default sender:  $Mail::Sendmail::mailcfg{from}\n";

%mail = (   To      =>  'dhagan@email.com',
            From    =>  'dhagan@email.com',
            Message =>  'Test!'

        );

sendmail(%mail) or die $Mail::Sendmail::error;

print "OK.  Log says:\n", $Mail::Sendmail::log;

这有什么原因会发生吗?

1 个答案:

答案 0 :(得分:1)

默认情况下,Mail :: Sendmail配置为将邮件发送到localhost,但您没有在那里运行SMTP服务器。

您必须配置合适的服务器 - 请参阅帮助。

 Default SMTP server(s)
       This is probably all you want to configure. It is usually done
       through $mailcfg{smtp}, which you can edit at the top of the
       Sendmail.pm file.  This is a reference to a list of SMTP servers.
       You can also set it from your script:

       "unshift @{$Mail::Sendmail::mailcfg{'smtp'}} , 'my.mail.server';"

       Alternatively, you can specify the server in the %mail hash you
       send from your script, which will do the same thing:

       "$mail{smtp} = 'my.mail.server';"