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



我试图发送忘记密码的邮件。代码在本地机器上运行良好,我可以接收电子邮件。但当我在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服务器587465中的端口也打开了。如果在开放港口等方面有任何错误,请告诉我。。如有任何帮助,将不胜感激

编辑:错误看起来像这个

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我不知道确切的原因,但这解决了我的问题

相关内容

最新更新