php smtp 无法在 ubuntu 服务器中发送电子邮件

Posted

技术标签:

【中文标题】php smtp 无法在 ubuntu 服务器中发送电子邮件【英文标题】:php smtp cannot sent email in ubuntu server 【发布时间】:2021-06-17 15:29:27 【问题描述】:

我正在尝试发送忘记密码的邮件。代码在本地机器上运行良好,我可以收到电子邮件。但是当我在 ubuntu 服务器版本 20.04 上部署时出现错误。

?php

$mail->IsSMTP();
$mail->CharSet = 'UTF-8';

$mail->Host       = "smtp.gmail.com"; // SMTP server example

$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Port       = 587;                    // set the SMTP port for the GMAIL server

$mail->Username   = 'username@gmail.com'; // SMTP account username example
$mail->Password   = 'password';        // SMTP account password example

$mail->SMTPSecure = 'tls';    
// Content
$mail->ishtml(true);                                  // Set email format to HTML
$mail->SetFrom("username@gmail.com");

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->AddAddress('to_user@gmail.com');     // Add a recipient
$mail->send()
?>

我已尝试使用 465 端口并更改为 $mail->SMTPSecure = 'tls'; 但没有运气 我还在 gmail 中启用了不太安全的应用程序,并且在 gmail 中没有阻止连接,并且端口在 ubuntu 服务器 587、465 中打开了仍然没有运气。 让我知道在打开端口等的任何方面是否有问题.. 任何帮助将不胜感激

编辑:错误看起来像这样

2021-03-20 12:32:33 Connection: opening to smtp.gmail.com:587, timeout=300, options=array ()
2021-03-20 12:32:33 Connection: opened
2021-03-20 12:32:33 SMTP -> get_lines(): $data is ""
2021-03-20 12:32:33 SMTP -> get_lines(): $str is "220 smtp.gmail.com ESMTP f2sm8786335pfq.129 - gsmtp"
2021-03-20 12:32:33 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP f2sm8786335pfq.129 - gsmtp
2021-03-20 12:32:33 CLIENT -> SERVER: EHLO 103.117.180.121
2021-03-20 12:32:34 SMTP -> get_lines(): $data is ""
2021-03-20 12:32:34 SMTP -> get_lines(): $str is "250-smtp.gmail.com at your service, [103.117.180.121]"
2021-03-20 12:32:34 SMTP -> get_lines(): $data is "250-smtp.gmail.com at your service, [103.117.180.121]"
2021-03-20 12:32:34 SMTP -> get_lines(): $str is "250-SIZE 35882577"
2021-03-20 12:32:34 SMTP -> get_lines(): $data is "250-smtp.gmail.com at your service, [103.117.180.121]250-SIZE 35882577"
2021-03-20 12:32:34 SMTP -> get_lines(): $str is "250-8BITMIME"
2021-03-20 12:32:34 SMTP -> get_lines(): $data is "250-smtp.gmail.com at your service, [103.117.180.121]250-SIZE 35882577250-8BITMIME"
2021-03-20 12:32:34 SMTP -> get_lines(): $str is "250-STARTTLS"
2021-03-20 12:32:34 SMTP -> get_lines(): $data is "250-smtp.gmail.com at your service, [103.117.180.121]250-SIZE 35882577250-8BITMIME250-STARTTLS"
2021-03-20 12:32:34 SMTP -> get_lines(): $str is "250-ENHANCEDSTATUSCODES"
2021-03-20 12:32:34 SMTP -> get_lines(): $data is "250-smtp.gmail.com at your service, [103.117.180.121]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES"
2021-03-20 12:32:34 SMTP -> get_lines(): $str is "250-PIPELINING"
2021-03-20 12:32:34 SMTP -> get_lines(): $data is "250-smtp.gmail.com at your service, [103.117.180.121]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING"
2021-03-20 12:32:34 SMTP -> get_lines(): $str is "250-CHUNKING"
2021-03-20 12:32:34 SMTP -> get_lines(): $data is "250-smtp.gmail.com at your service, [103.117.180.121]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING"
2021-03-20 12:32:34 SMTP -> get_lines(): $str is "250 SMTPUTF8"
2021-03-20 12:32:34 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [103.117.180.121]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2021-03-20 12:32:34 CLIENT -> SERVER: STARTTLS
2021-03-20 12:32:34 SMTP -> get_lines(): $data is ""
2021-03-20 12:32:34 SMTP -> get_lines(): $str is "220 2.0.0 Ready to start TLS"
2021-03-20 12:32:34 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2021-03-20 12:32:34 CLIENT -> SERVER: EHLO 103.117.180.121
2021-03-20 12:32:34 SMTP -> get_lines(): $data is ""
2021-03-20 12:32:34 SMTP -> get_lines(): $str is "250-smtp.gmail.com at your service, [103.117.180.121]"
2021-03-20 12:32:34 SMTP -> get_lines(): $data is "250-smtp.gmail.com at your service, [103.117.180.121]"
2021-03-20 12:32:34 SMTP -> get_lines(): $str is "250-SIZE 35882577"
2021-03-20 12:32:34 SMTP -> get_lines(): $data is "250-smtp.gmail.com at your service, [103.117.180.121]250-SIZE 35882577"
2021-03-20 12:32:34 SMTP -> get_lines(): $str is "250-8BITMIME"
2021-03-20 12:32:34 SMTP -> get_lines(): $data is "250-smtp.gmail.com at your service, [103.117.180.121]250-SIZE 35882577250-8BITMIME"
2021-03-20 12:32:34 SMTP -> get_lines(): $str is "250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH"
2021-03-20 12:32:34 SMTP -> get_lines(): $data is "250-smtp.gmail.com at your service, [103.117.180.121]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH"
2021-03-20 12:32:34 SMTP -> get_lines(): $str is "250-ENHANCEDSTATUSCODES"
2021-03-20 12:32:34 SMTP -> get_lines(): $data is "250-smtp.gmail.com at your service, [103.117.180.121]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES"
2021-03-20 12:32:34 SMTP -> get_lines(): $str is "250-PIPELINING"
2021-03-20 12:32:34 SMTP -> get_lines(): $data is "250-smtp.gmail.com at your service, [103.117.180.121]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING"
2021-03-20 12:32:34 SMTP -> get_lines(): $str is "250-CHUNKING"
2021-03-20 12:32:34 SMTP -> get_lines(): $data is "250-smtp.gmail.com at your service, [103.117.180.121]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING"
2021-03-20 12:32:34 SMTP -> get_lines(): $str is "250 SMTPUTF8"
2021-03-20 12:32:34 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [103.117.180.121]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2021-03-20 12:32:34 Auth method requested: UNKNOWN
2021-03-20 12:32:34 Auth methods available on the server: LOGIN,PLAIN,XOAUTH2,PLAIN-CLIENTTOKEN,OAUTHBEARER,XOAUTH
2021-03-20 12:32:34 Auth method selected: LOGIN
2021-03-20 12:32:34 CLIENT -> SERVER: AUTH LOGIN
2021-03-20 12:32:35 SMTP -> get_lines(): $data is ""
2021-03-20 12:32:35 SMTP -> get_lines(): $str is "334 VXNlcm5hbWU6"
2021-03-20 12:32:35 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2021-03-20 12:32:35 CLIENT -> SERVER: bmluamFzdG9ybTU1NTVAZ21haWwuY29t
2021-03-20 12:32:35 SMTP -> get_lines(): $data is ""
2021-03-20 12:32:35 SMTP -> get_lines(): $str is "334 UGFzc3dvcmQ6"
2021-03-20 12:32:35 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2021-03-20 12:32:35 CLIENT -> SERVER: dmlja3lAMTEwNQ==
2021-03-20 12:32:36 SMTP -> get_lines(): $data is ""
2021-03-20 12:32:36 SMTP -> get_lines(): $str is "534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbv"
2021-03-20 12:32:36 SMTP -> get_lines(): $data is "534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbv"
2021-03-20 12:32:36 SMTP -> get_lines(): $str is "534-5.7.14 AMiGCo8rutfSvEneAss3krvj8-TcwiFNjox9TLQZAYp0eH4CisKZ6Ss0njU4ZLe824Xt9"
2021-03-20 12:32:36 SMTP -> get_lines(): $data is "534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbv534-5.7.14 AMiGCo8rutfSvEneAss3krvj8-TcwiFNjox9TLQZAYp0eH4CisKZ6Ss0njU4ZLe824Xt9"
2021-03-20 12:32:36 SMTP -> get_lines(): $str is "534-5.7.14 1vhLWdKAHx3F1CpeyCpDoW0-8wVcFionwIjNdKfzuOmZsUisBB9trv-52_4oxkDP>"
2021-03-20 12:32:36 SMTP -> get_lines(): $data is "534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbv534-5.7.14 AMiGCo8rutfSvEneAss3krvj8-TcwiFNjox9TLQZAYp0eH4CisKZ6Ss0njU4ZLe824Xt9534-5.7.14 1vhLWdKAHx3F1CpeyCpDoW0-8wVcFionwIjNdKfzuOmZsUisBB9trv-52_4oxkDP>"
2021-03-20 12:32:36 SMTP -> get_lines(): $str is "534-5.7.14 Please log in via your web browser and then try again."
2021-03-20 12:32:36 SMTP -> get_lines(): $data is "534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbv534-5.7.14 AMiGCo8rutfSvEneAss3krvj8-TcwiFNjox9TLQZAYp0eH4CisKZ6Ss0njU4ZLe824Xt9534-5.7.14 1vhLWdKAHx3F1CpeyCpDoW0-8wVcFionwIjNdKfzuOmZsUisBB9trv-52_4oxkDP>534-5.7.14 Please log in via your web browser and then try again."
2021-03-20 12:32:36 SMTP -> get_lines(): $str is "534-5.7.14 Learn more at"
2021-03-20 12:32:36 SMTP -> get_lines(): $data is "534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbv534-5.7.14 AMiGCo8rutfSvEneAss3krvj8-TcwiFNjox9TLQZAYp0eH4CisKZ6Ss0njU4ZLe824Xt9534-5.7.14 1vhLWdKAHx3F1CpeyCpDoW0-8wVcFionwIjNdKfzuOmZsUisBB9trv-52_4oxkDP>534-5.7.14 Please log in via your web browser and then try again.534-5.7.14 Learn more at"
2021-03-20 12:32:36 SMTP -> get_lines(): $str is "534 5.7.14 https://support.google.com/mail/answer/78754 f2sm8786335pfq.129 - gsmtp"
2021-03-20 12:32:36 SERVER -> CLIENT: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbv534-5.7.14 AMiGCo8rutfSvEneAss3krvj8-TcwiFNjox9TLQZAYp0eH4CisKZ6Ss0njU4ZLe824Xt9534-5.7.14 1vhLWdKAHx3F1CpeyCpDoW0-8wVcFionwIjNdKfzuOmZsUisBB9trv-52_4oxkDP>534-5.7.14 Please log in via your web browser and then try again.534-5.7.14 Learn more at534 5.7.14 https://support.google.com/mail/answer/78754 f2sm8786335pfq.129 - gsmtp
2021-03-20 12:32:36 SMTP ERROR: Password command failed: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbv534-5.7.14 AMiGCo8rutfSvEneAss3krvj8-TcwiFNjox9TLQZAYp0eH4CisKZ6Ss0njU4ZLe824Xt9534-5.7.14 1vhLWdKAHx3F1CpeyCpDoW0-8wVcFionwIjNdKfzuOmZsUisBB9trv-52_4oxkDP>534-5.7.14 Please log in via your web browser and then try again.534-5.7.14 Learn more at534 5.7.14 https://support.google.com/mail/answer/78754 f2sm8786335pfq.129 - gsmtp
SMTP Error: Could not authenticate.
2021-03-20 12:32:36 CLIENT -> SERVER: QUIT
2021-03-20 12:32:36 SMTP -> get_lines(): $data is ""
2021-03-20 12:32:36 SMTP -> get_lines(): $str is "221 2.0.0 closing connection f2sm8786335pfq.129 - gsmtp"
2021-03-20 12:32:36 SERVER -> CLIENT: 221 2.0.0 closing connection f2sm8786335pfq.129 - gsmtp
2021-03-20 12:32:36 Connection: closed
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

我的 ssl 也未激活如果这可能是问题,请告诉我

【问题讨论】:

错误是什么? 欢迎来到 ***。请使用更多详细信息更新您的问题,否则它将被关闭和/或否决。请阅读how to ask a question。 我已经更新了@biesior 的错误消息,如果可能,请提供帮助 2021-03-20 12:32:36 SMTP ERROR: Password command failed... 等等,错误日志中有一些建议和链接,你应该关注他们并检查您是否可以解决问题。还请阅读我昨天类似主题的 cmets - 也许您会发现一些额外的线索:***.com/q/66710606/1066240 但它适用于本地主机,即本地机器,但是当我在服务器上部署时,我遇到了这个问题@biesior 【参考方案1】:

您遇到此处描述的错误: 2021-03-20 12:32:36 SMTP 错误:密码命令失败:534-5.7.14

可能密码确实不正确,或者您可以访问此处:https://www.google.com/settings/security/lesssecureapps 以允许不太安全的应用程序访问您的帐户。您需要将按钮切换到 ON。

【讨论】:

如果密码不正确,那么它应该在我的本地服务器上不起作用。此外,正如我所提到的,我启用了不太安全的应用程序 还有一个错误的链接可以回答您的问题。 support.google.com/mail/answer/78754,其中之一是:确保您的邮件应用程序未设置为过于频繁地检查新电子邮件。如果您的邮件应用每 10 分钟检查一次以上的新邮件,则可能会阻止该应用访问您的帐户。【参考方案2】:

经过一周的尝试终于找到了解决方案 我关注了这个视频并找到了解决方案 https://www.youtube.com/watch?v=aBbmo1pi5B0

基本上它告诉启用recatpcha 我不知道确切的原因,但这解决了我的问题

【讨论】:

以上是关于php smtp 无法在 ubuntu 服务器中发送电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

无法使用 gmail SMTP 在 Zend 中发送电子邮件。出现致命错误:未捕获的异常“Zend_Mail_Protocol_Exception”。如何解决这个问题?

从 SMTP 服务器使用 PHP 发送电子邮件

如何使用 smtp.EmailBackend 在 Django 中发送电子邮件而无需通过邮件服务器进行身份验证

使用带有 smtp 但没有 SSL 的 JavaMail API 在 android 中发送电子邮件

如何在python程序中发邮件

在测试 php 以在 xampp 中发送电子邮件验证时遇到问题