使用 Perl 通过 G Suite 发送电子邮件

Sending email through G Suite using Perl

在通过我们的 G Suite account/address 发送电子邮件近十年后,该脚本在昨晚托管公司服务器升级后停止工作。 (不幸的是,托管公司目前已将此归档在“不是我们的问题”下,所以那方面真的没有任何帮助。我希望至少排除任何 我们 做错的事情,并能够通过一些有用的取证返回给他们。)

这是相关的 Perl 摘录:

    $smtp = Net::SMTP->new('smtp.gmail.com', Port => 465, Timeout => 30, Hello => 'mail.mydomain.com', SSL => 1, Debug => 1);
    # $login and $pass are valid address@mydomain.com and password
    # used for logging into the Gmail interface at mail.mydomain.com
    print "auth($login, $pass)\n";
    $smtp->auth($login, $pass) or die "Unable to authenticate";
    $smtp->mail($login);
    $smtp->recipient($email);
    $smtp->data($message) or die "data() failed sending to: $email\n";
    $smtp->quit;

这是输出:

    Net::SMTP::_SSL>>> Net::SMTP::_SSL
    Net::SMTP::_SSL>>>   IO::Socket::SSL(2.060)
    Net::SMTP::_SSL>>>     IO::Socket::IP(0.39)
    Net::SMTP::_SSL>>>       IO::Socket(1.39)
    Net::SMTP::_SSL>>>         IO::Handle(1.39)
    Net::SMTP::_SSL>>>           Exporter(5.73)
    Net::SMTP::_SSL>>>   Net::SMTP(3.11)
    Net::SMTP::_SSL>>>     Net::Cmd(3.11)
    Net::SMTP::_SSL=GLOB(0x7fc8de8ab9d8)<<< 220 smtp.gmail.com ESMTP [###] - gsmtp
    Net::SMTP::_SSL=GLOB(0x7fc8de8ab9d8)>>> EHLO mail.mydomain.com
    Net::SMTP::_SSL=GLOB(0x7fc8de8ab9d8)<<< 250-smtp.gmail.com at your service, [###.###.###.###]
    Net::SMTP::_SSL=GLOB(0x7fc8de8ab9d8)<<< 250-SIZE 35882577
    Net::SMTP::_SSL=GLOB(0x7fc8de8ab9d8)<<< 250-8BITMIME
    Net::SMTP::_SSL=GLOB(0x7fc8de8ab9d8)<<< 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
    Net::SMTP::_SSL=GLOB(0x7fc8de8ab9d8)<<< 250-ENHANCEDSTATUSCODES
    Net::SMTP::_SSL=GLOB(0x7fc8de8ab9d8)<<< 250-PIPELINING
    Net::SMTP::_SSL=GLOB(0x7fc8de8ab9d8)<<< 250-CHUNKING
    Net::SMTP::_SSL=GLOB(0x7fc8de8ab9d8)<<< 250 SMTPUTF8
    auth(address@mydomain.com, [password])
    Unable to authenticate at mymodule.pm line 459.

一些其他注意事项:

  1. 域和用户地址@mydomain.com(和以前一样)都启用了不太安全的应用程序访问。
  2. 这些是有效的凭据。它们目前可用于登录邮件。mydomain.com(web/Gmail 界面),与之前用于此方法的方法相同。
  3. 用于通过 Gmail 发送邮件的方法在 SO 和其他地方的网络上描述得相当普遍,包括 https://support.google.com/a/answer/176600使用 Gmail SMTP 服务器其他设置选项下
  4. 同样,这在服务器升级之前一直有效。但是由于我在错误输出中没有看到任何内容,所以我不确定从哪里开始寻找破损。

通常发生这种情况时我遗漏了一些明显的东西,那么我是否遗漏了一些明显的东西?

鉴于调试输出未显示任何身份验证尝试,很可能需要 Authen::SASL module is not installed or not properly installed. From the documentation of Net::SMTP:

auth ( USERNAME, PASSWORD )
auth ( SASL )
Attempt SASL authentication. Requires Authen::SASL module. The first form constructs a new Authen::SASL object using the given username and password; the second form uses the given Authen::SASL object.

要确定问题,不仅要用自定义错误 die(),还要记录 $smtp->message 可能会很有用。基于 this code,它可能会记录错误 "Need MIME::Base64 and Authen::SASL todo auth".