【问题标题】:Error: need MAIL command when using smtp with perl错误:使用带有 perl 的 smtp 时需要 MAIL 命令
【发布时间】:2018-01-01 09:21:44
【问题描述】:

我尝试使用 Mime::Lite perl 模块并通过 smtp 身份验证发送电子邮件。但不幸的是,这不起作用。它显示了

错误:需要 MAIL 命令,错误:命令未实现

这是更新后的代码 sn-p 和调试输出。

#!/usr/bin/perl
use warnings;
use strict;
use MIME::Lite;
use Net::SMTP;
use MIME::Base64;


my $smtp = Net::SMTP->new(<mail host>,Port=>587,Debug=>1)or die;
$smtp->starttls();
$smtp->auth($username,$password) or die $!;
my $msg = MIME::Lite -> new ( 
  From  => 'from@mail.com',
  TO    => 'to@mail.com', 
  Subject  => 'Testing Text Message',
  Data     => 'How\'s it going.' );

$smtp->mail(<from mail>);
$smtp->to(<to mail>);
$smtp -> data();
$smtp -> datasend( $msg->as_string() );
$smtp -> dataend(); 
print $smtp ->message();
$smtp -> quit;

调试输出:

Net::SMTP>>> Net::SMTP(3.10)
Net::SMTP>>>   Net::Cmd(3.10)
Net::SMTP>>>     Exporter(5.68)
Net::SMTP>>>   IO::Socket::INET6(2.71)
Net::SMTP>>>     IO::Socket(1.36)
Net::SMTP>>>       IO::Handle(1.34)
Net::SMTP=GLOB(0x1e0a920)<<< 220 email-smtp.amazonaws.com ESMTP SimpleEmailService-2108164273 5RjAQr5ZFI284sDt1KWu
Net::SMTP=GLOB(0x1e0a920)>>> EHLO localhost.localdomain
Net::SMTP=GLOB(0x1e0a920)<<< 250-email-smtp.amazonaws.com
Net::SMTP=GLOB(0x1e0a920)<<< 250-8BITMIME
Net::SMTP=GLOB(0x1e0a920)<<< 250-SIZE 10485760
Net::SMTP=GLOB(0x1e0a920)<<< 250-STARTTLS
Net::SMTP=GLOB(0x1e0a920)<<< 250-AUTH PLAIN LOGIN
Net::SMTP=GLOB(0x1e0a920)<<< 250 Ok
Net::SMTP=GLOB(0x1e0a920)>>> STARTTLS
Net::SMTP=GLOB(0x1e0a920)<<< 220 Ready to start TLS
Net::SMTP::_SSL=GLOB(0x1e0a920)>>> EHLO localhost.localdomain
Net::SMTP::_SSL=GLOB(0x1e0a920)<<< 250-email-smtp.amazonaws.com
Net::SMTP::_SSL=GLOB(0x1e0a920)<<< 250-8BITMIME
Net::SMTP::_SSL=GLOB(0x1e0a920)<<< 250-SIZE 10485760
Net::SMTP::_SSL=GLOB(0x1e0a920)<<< 250-STARTTLS
Net::SMTP::_SSL=GLOB(0x1e0a920)<<< 250-AUTH PLAIN LOGIN
Net::SMTP::_SSL=GLOB(0x1e0a920)<<< 250 Ok
Died at test_script.pl line 17.

请告诉我解决方案。

提前谢谢你!

【问题讨论】:

  • 您的代码中没有任何错误处理。我猜在您发出$smtp-&gt;mail(...) 之前有些事情失败了,例如身份验证失败。请在您的代码中添加调试(即在调用Net::SMTP_auth-&gt;new 时添加Debug =&gt; 1)并将完整的调试输出包含在您的问题中。
  • 1) 为什么使用Net::SMTP_auth 而不是Net::SMTP?当前版本的Net::SMTP 支持 SMTP AUTH。 2) 将,Debug=&gt;1) 添加到Net::SMTP 构造函数(新)以获取SMTP 会话的记录。
  • @SteffenUllrich 感谢您的 cmets。我已将调试输出添加到问题中,请告诉我如何解决此问题

标签: perl email smtp amazon-ses smtp-auth


【解决方案1】:

您的代码没有任何错误检查,这就是您错过身份验证失败的原因:

Net::SMTP_auth=GLOB(0x13085a8)>>> AUTH LOGIN
Net::SMTP_auth=GLOB(0x13085a8)<<< 530 Must issue a STARTTLS command first

并且由于身份验证失败,它不会接受发送邮件,即$smtp-&gt;mail('admin@testadmin.com'); 将导致Error: need MAIL command, Error: command not implemented

不幸的是,非常古老的 Net::SMTP_auth(2006 年最新更新)不支持 STARTTLS。但是,当前版本的 Net::SMTP 支持 auth(因此您不需要 Net::SMTP_auth)和 starttls(从 Net::SMTP 版本 3.xx 开始)。

使用 Net::SMTP 3.xx,您的代码应如下所示:

my $smtp = Net::SMTP->new( '<emailhost>') or die;
$smtp->starttls or die;
$smtp->auth('<username>', '<password>') or die;
...

【讨论】:

  • 我们使用 Net::SMTP 版本 3.10,现在它没有显示任何错误,但在 $smtp -> auth('LOGIN', '', '') 或死;
  • @Sayooj:请参阅有关如何使用 Net::SMTP 的更新答案:首先您需要调用 starttls,然后您需要以不同的方式调用 auth
  • 我已经尝试过你提到的,通过调用starttls,然后以不同的方式调用auth。但结果是一样的。您能否分享任何相同的工作代码sn-p
  • @Sayooj:如果您用正确的值替换 &lt;emailhost&gt; 等,我给出的示例代码对我有用。刚刚针对mail.gmx.net:587 进行了测试。我猜您缺少 SASL 模块 (Authen::SASL),因为它不会尝试根据调试输出进行身份验证。
猜你喜欢
  • 2016-08-06
  • 1970-01-01
  • 2015-11-13
  • 2012-04-18
  • 1970-01-01
  • 2023-02-17
  • 2015-11-05
  • 2011-11-19
  • 1970-01-01
相关资源
最近更新 更多