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

php smtp cannot sent email in ubuntu server

我正在尝试发送忘记密码的邮件。代码在本地机器上运行良好,我可以接收电子邮件。但是当我在 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 也没有激活 如果这可能是个问题请告诉我

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

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

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

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