使用perl在linux上设置SMTP传出邮件

时间:2010-11-07 13:52:13

标签: linux perl smtp sendmail

我在使用perl发送邮件时遇到问题。当我输入以下命令时: my $ smtp = Net :: SMTP-> new($ ServerName,Debug => 0);其中$ ServerName =“localhost”。 $ smtp未设置。它是null。任何人都可以告诉我为什么它设置不当。由于在我的其他Linux服务器上运行了相同的脚本。

真的很感激帮助。

由于 DD

2 个答案:

答案 0 :(得分:2)

显然,

Net::SMTP的错误处理相当平庸。 你能做的最好的是:

my $smtp = Net::SMTP->new($ServerName)
            or die "Unable to connect to SMTP server '$ServerName'";

即使传递Debug => 1也无法在无法创建对象时执行任何操作。

三年多来,还没有新版本的libnet(包括Net :: SMTP)。从2010年5月开始有一个developer release似乎有一些改进:documents

On failure undef will be returned and $@ will contain the reason for the failure.

(最新的正确版本没有这样做。)

如果您愿意尝试安装开发人员版本,则可以将代码改进为:

my $smtp = Net::SMTP->new($ServerName)
            or die "Unable to connect to SMTP server '$ServerName': $@";

您的连接失败的可能原因:

  • $ ServerName不是有效的主机名
  • $ ServerName上没有运行SMTP服务
  • 服务器不喜欢标准问候localhost.localdomain。将Hello => 'your.fully.qualified.host.name'传递给构造函数。

您可以尝试在尝试运行此脚本的主机上的命令行上连接到SMTP服务器:

$ telnet smtp.example.com
EHLO localhost.localdomain
quit

答案 1 :(得分:0)

您是否尝试启用调试?

 $smtp = Net::SMTP->new(
 Host => 'mailhost',
 Hello => 'my.mail.domain',
 Timeout => 30,
 Debug => 1,
 );

然后看一下调试输出的内容。

相关问题