为什么在尝试发送电子邮件时使用 phpmailer 会出现此错误?
Why do I get this error with phpmailer when trying to send an email?
错误:
2021-03-19 15:17:50 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP t24sm3903607qto.23 - gsmtp
2021-03-19 15:17:50 CLIENT -> SERVER: EHLO localhost
2021-03-19 15:17:50 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [24.233.165.10]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2021-03-19 15:17:50 CLIENT -> SERVER: STARTTLS
2021-03-19 15:17:50 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2021-03-19 15:17:50 CLIENT -> SERVER: EHLO localhost
2021-03-19 15:17:50 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [24.233.165.10]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2021-03-19 15:17:50 CLIENT -> SERVER: AUTH LOGIN
2021-03-19 15:17:50 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2021-03-19 15:17:50 CLIENT -> SERVER: [credentials hidden]
2021-03-19 15:17:50 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2021-03-19 15:17:50 CLIENT -> SERVER: [credentials hidden]
2021-03-19 15:17:51 SERVER -> CLIENT: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials t24sm3903607qto.23 - gsmtp
2021-03-19 15:17:51 SMTP ERROR: Password command failed: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials t24sm3903607qto.23 - gsmtp
SMTP Error: Could not authenticate.
2021-03-19 15:17:51 CLIENT -> SERVER: QUIT
2021-03-19 15:17:51 SERVER -> CLIENT: 221 2.0.0 closing connection t24sm3903607qto.23 - gsmtp
SMTP Error: Could not authenticate.
Message could not be sent. Mailer Error: SMTP Error: Could not authenticate.
我的代码:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/PHPMailer/src/Exception.php';
require 'PHPMailer/PHPMailer/src/PHPMailer.php';
require 'PHPMailer/PHPMailer/src/SMTP.php';
// Instantiation and passing [ICODE]true[/ICODE] enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'myemail@gmail.com'; // SMTP username
$mail->Password = '*************'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, [ICODE]ssl[/ICODE] also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('myemail.com', 'Mailer');
$mail->addAddress('myemail@gmail.com', 'Recipients'); // Add a recipient
//$mail->addAddress('');
$mail->addReplyTo('myemai@gmail.com', 'Mailer');
//$mail->addCC('');
//$mail->addBCC('');
// Attachments
//$mail->addAttachment(''); // Add attachments
//$mail->addAttachment('');
// Content
$mail->isHTML(true); // Set email format to HTML
$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->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
我启用了不太安全的应用程序。
我启用了 IMAP。
关闭两步验证。
我从 github 下载了最新版本。
电子邮件是我的电子邮件,确实有效。
我应该怎么办?
我正在 xampp 上从我的本地主机尝试此操作。
我的密码是正确的。为什么会这样?
是这个错字:
$mail->Host = 'smpt.gmail.com';
应该是:
$mail->Host = 'smtp.gmail.com';
错误信息本身说找不到主机名,这完全是意料之中的,因为它是错误的。
您多次编辑问题,每次都更改其含义,以后应该避免这种情况,因为它会产生大量的评论。相反,下次请阅读错误消息并在询问之前修复它们。你做错了什么(按出现顺序)
- 不要只为将来使用没有值的赋值方法,如
$mail->addAddress('');
、$mail->addCC('')
等,这会导致错误。
- 使用正确的 SMTP 服务器地址。
- 使用正确的凭据。当然,登录名和密码 区分大小写。
- 如果凭据错误问题仍然存在,请在您的 Gmail 帐户上打开 ON 'Less secure applications'。可以在安全选项卡中完成,如屏幕截图所示
错误:
2021-03-19 15:17:50 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP t24sm3903607qto.23 - gsmtp
2021-03-19 15:17:50 CLIENT -> SERVER: EHLO localhost
2021-03-19 15:17:50 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [24.233.165.10]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2021-03-19 15:17:50 CLIENT -> SERVER: STARTTLS
2021-03-19 15:17:50 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2021-03-19 15:17:50 CLIENT -> SERVER: EHLO localhost
2021-03-19 15:17:50 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [24.233.165.10]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2021-03-19 15:17:50 CLIENT -> SERVER: AUTH LOGIN
2021-03-19 15:17:50 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2021-03-19 15:17:50 CLIENT -> SERVER: [credentials hidden]
2021-03-19 15:17:50 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2021-03-19 15:17:50 CLIENT -> SERVER: [credentials hidden]
2021-03-19 15:17:51 SERVER -> CLIENT: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials t24sm3903607qto.23 - gsmtp
2021-03-19 15:17:51 SMTP ERROR: Password command failed: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials t24sm3903607qto.23 - gsmtp
SMTP Error: Could not authenticate.
2021-03-19 15:17:51 CLIENT -> SERVER: QUIT
2021-03-19 15:17:51 SERVER -> CLIENT: 221 2.0.0 closing connection t24sm3903607qto.23 - gsmtp
SMTP Error: Could not authenticate.
Message could not be sent. Mailer Error: SMTP Error: Could not authenticate.
我的代码:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/PHPMailer/src/Exception.php';
require 'PHPMailer/PHPMailer/src/PHPMailer.php';
require 'PHPMailer/PHPMailer/src/SMTP.php';
// Instantiation and passing [ICODE]true[/ICODE] enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'myemail@gmail.com'; // SMTP username
$mail->Password = '*************'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, [ICODE]ssl[/ICODE] also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('myemail.com', 'Mailer');
$mail->addAddress('myemail@gmail.com', 'Recipients'); // Add a recipient
//$mail->addAddress('');
$mail->addReplyTo('myemai@gmail.com', 'Mailer');
//$mail->addCC('');
//$mail->addBCC('');
// Attachments
//$mail->addAttachment(''); // Add attachments
//$mail->addAttachment('');
// Content
$mail->isHTML(true); // Set email format to HTML
$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->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
我启用了不太安全的应用程序。 我启用了 IMAP。 关闭两步验证。 我从 github 下载了最新版本。 电子邮件是我的电子邮件,确实有效。 我应该怎么办? 我正在 xampp 上从我的本地主机尝试此操作。 我的密码是正确的。为什么会这样?
是这个错字:
$mail->Host = 'smpt.gmail.com';
应该是:
$mail->Host = 'smtp.gmail.com';
错误信息本身说找不到主机名,这完全是意料之中的,因为它是错误的。
您多次编辑问题,每次都更改其含义,以后应该避免这种情况,因为它会产生大量的评论。相反,下次请阅读错误消息并在询问之前修复它们。你做错了什么(按出现顺序)
- 不要只为将来使用没有值的赋值方法,如
$mail->addAddress('');
、$mail->addCC('')
等,这会导致错误。 - 使用正确的 SMTP 服务器地址。
- 使用正确的凭据。当然,登录名和密码 区分大小写。
- 如果凭据错误问题仍然存在,请在您的 Gmail 帐户上打开 ON 'Less secure applications'。可以在安全选项卡中完成,如屏幕截图所示