PHP Mailer returns PHP Fatal error: Uncaught PHPMailer\PHPMailer\Exception: SMTP Error: Could not authenticate
PHP Mailer returns PHP Fatal error: Uncaught PHPMailer\PHPMailer\Exception: SMTP Error: Could not authenticate
我看到了很多类似的问题,就像我的一样,但从来没有一个对我有帮助。我正在尝试来自 https://github.com/PHPMailer/PHPMailer 的 PHPMailer。我正在使用 xampp 服务器对其进行测试,一切正常。但是当我在我的页面上上传整个 phpMailer 时,出现了一些问题。在我的日志文件中,我仍然收到此错误:
PHP Fatal error: Uncaught PHPMailer\PHPMailer\Exception: SMTP Error: Could not authenticate. in /web/test/vendor/phpmailer/phpmailer/src/PHPMailer.php:2089
Stack trace:
#0 web/test/vendor/phpmailer/phpmailer/src/PHPMailer.php(1900): PHPMailer\PHPMailer\PHPMailer->smtpConnect()
#1 web/test/vendor/phpmailer/phpmailer/src/PHPMailer.php(1614): PHPMailer\PHPMailer\PHPMailer->smtpSend()
#2 web/test/vendor/phpmailer/phpmailer/src/PHPMailer.php(1447): PHPMailer\PHPMailer\PHPMailer->postSend()
#3 web/test/idk.php(31): PHPMailer\PHPMailer\PHPMailer->send()
#4 {main}
thrown in web/test/vendor/phpmailer/phpmailer/src/PHPMailer.php on line 2089`
有人知道如何解决这个问题吗?我很确定我的登录凭据没问题。奇怪的是,它在 xampp 上运行良好,但在我的页面上却没有。我使用的是最新的 PHPMailer,我没有编辑任何东西,所以你可以在 PHPMailer github 上看到代码。
如果有人需要其他信息,请告诉我。
非常感谢!
编辑:这是我的 idk.php 文件(代码片段运行不正常,但我认为您可以阅读它):
<?php
declare( strict_types = 1 );
// Check if the form is submitted
if ( isset( $_POST['submit'] ) ) {
// retrieve the form data by using the element's name attributes value as key
$subject = $_REQUEST['subject'];
$body = $_REQUEST['body'];
$sender = $_REQUEST['sender'];
require __DIR__ . '/vendor/autoload.php';
$mailer = new \PHPMailer\PHPMailer\PHPMailer( true );
//$mailer->SMTPDebug = \PHPMailer\PHPMailer\SMTP::DEBUG_SERVER tento řádek povoluje client -> server a server -> client zprávy
$mailer->isSMTP();
$mailer->Host = 'smtp.gmail.com';
$mailer->SMTPAuth = true;
$mailer->Username = 'my@mail.com';
$mailer->Password = 'myPassword';
$mailer->SMTPSecure = \PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;
$mailer->Port = 587;
$mailer->setFrom( 'my@mail.com' );
$mailer->addReplyTo( 'my@mail.com' );
$mailer->addAddress( $sender );
$mailer->isHTML( true );
$mailer->Subject = $subject;
$mailer->Body = $body;
$mailer->send();
}
?>
也许我应该说我在虚拟主机上使用 .htaccess 文件。我不确定,但也许这就是我收到这些错误的原因。当我尝试访问 idk.php 文件时,出现 http 错误 500。我做了一些研究,发现这个 500 错误和 htaccess 文件之间存在某种联系。
编辑:
我关闭了不太安全的应用程序,但它仍然不起作用。我将端口更改为 465,现在此文件中第 2076 行出现错误:https://github.com/PHPMailer/PHPMailer/blob/master/src/PHPMailer.php
我看到您正在使用 Google SMTP。尝试在您的 Google 帐户中启用“不太安全的应用程序”。还要确保您已将您的电子邮件地址添加到“允许的发件人”列表中。
由于这是在您的本地机器上运行,而不是在您的服务器上运行,您可能还想将您的服务器 IP 添加到允许的连接中。
这是 gmail 问题,您需要关闭“不太安全的应用程序访问”
关闭“不太安全的应用程序访问”
https://myaccount.google.com/lesssecureapps
示例(PHPMailer v6.2.0)
<?php
/**
* This example shows settings to use when sending via Google's Gmail servers.
* This uses traditional id & password authentication - look at the gmail_xoauth.phps
* example to see how to use XOAUTH2.
* The IMAP section shows how to save this message to the 'Sent Mail' folder using IMAP commands.
*/
//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
require 'vendor/autoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer();
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// SMTP::DEBUG_OFF = off (for production use)
// SMTP::DEBUG_CLIENT = client messages
// SMTP::DEBUG_SERVER = client and server messages
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption mechanism to use - STARTTLS or SMTPS
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = 'CHANGEME';
//Password to use for SMTP authentication
$mail->Password = 'CHANGEME';
//Set who the message is to be sent from
$mail->setFrom('from@example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('replyto@example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('CHANGEME', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->msgHTML('This is a plain-text message body');
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//send the message, check for errors
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent!';
//Section 2: IMAP
//Uncomment these to save your message in the 'Sent Mail' folder.
#if (save_mail($mail)) {
# echo "Message saved!";
#}
}
取自https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps
我不确定这里是否需要前导反斜杠:
$mailer = new \PHPMailer\PHPMailer\PHPMailer( true );
或在这里:
$mailer->SMTPSecure = \PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;
我看到了很多类似的问题,就像我的一样,但从来没有一个对我有帮助。我正在尝试来自 https://github.com/PHPMailer/PHPMailer 的 PHPMailer。我正在使用 xampp 服务器对其进行测试,一切正常。但是当我在我的页面上上传整个 phpMailer 时,出现了一些问题。在我的日志文件中,我仍然收到此错误:
PHP Fatal error: Uncaught PHPMailer\PHPMailer\Exception: SMTP Error: Could not authenticate. in /web/test/vendor/phpmailer/phpmailer/src/PHPMailer.php:2089 Stack trace: #0 web/test/vendor/phpmailer/phpmailer/src/PHPMailer.php(1900): PHPMailer\PHPMailer\PHPMailer->smtpConnect() #1 web/test/vendor/phpmailer/phpmailer/src/PHPMailer.php(1614): PHPMailer\PHPMailer\PHPMailer->smtpSend() #2 web/test/vendor/phpmailer/phpmailer/src/PHPMailer.php(1447): PHPMailer\PHPMailer\PHPMailer->postSend() #3 web/test/idk.php(31): PHPMailer\PHPMailer\PHPMailer->send() #4 {main} thrown in web/test/vendor/phpmailer/phpmailer/src/PHPMailer.php on line 2089`
有人知道如何解决这个问题吗?我很确定我的登录凭据没问题。奇怪的是,它在 xampp 上运行良好,但在我的页面上却没有。我使用的是最新的 PHPMailer,我没有编辑任何东西,所以你可以在 PHPMailer github 上看到代码。 如果有人需要其他信息,请告诉我。 非常感谢!
编辑:这是我的 idk.php 文件(代码片段运行不正常,但我认为您可以阅读它):
<?php
declare( strict_types = 1 );
// Check if the form is submitted
if ( isset( $_POST['submit'] ) ) {
// retrieve the form data by using the element's name attributes value as key
$subject = $_REQUEST['subject'];
$body = $_REQUEST['body'];
$sender = $_REQUEST['sender'];
require __DIR__ . '/vendor/autoload.php';
$mailer = new \PHPMailer\PHPMailer\PHPMailer( true );
//$mailer->SMTPDebug = \PHPMailer\PHPMailer\SMTP::DEBUG_SERVER tento řádek povoluje client -> server a server -> client zprávy
$mailer->isSMTP();
$mailer->Host = 'smtp.gmail.com';
$mailer->SMTPAuth = true;
$mailer->Username = 'my@mail.com';
$mailer->Password = 'myPassword';
$mailer->SMTPSecure = \PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;
$mailer->Port = 587;
$mailer->setFrom( 'my@mail.com' );
$mailer->addReplyTo( 'my@mail.com' );
$mailer->addAddress( $sender );
$mailer->isHTML( true );
$mailer->Subject = $subject;
$mailer->Body = $body;
$mailer->send();
}
?>
也许我应该说我在虚拟主机上使用 .htaccess 文件。我不确定,但也许这就是我收到这些错误的原因。当我尝试访问 idk.php 文件时,出现 http 错误 500。我做了一些研究,发现这个 500 错误和 htaccess 文件之间存在某种联系。
编辑: 我关闭了不太安全的应用程序,但它仍然不起作用。我将端口更改为 465,现在此文件中第 2076 行出现错误:https://github.com/PHPMailer/PHPMailer/blob/master/src/PHPMailer.php
我看到您正在使用 Google SMTP。尝试在您的 Google 帐户中启用“不太安全的应用程序”。还要确保您已将您的电子邮件地址添加到“允许的发件人”列表中。
由于这是在您的本地机器上运行,而不是在您的服务器上运行,您可能还想将您的服务器 IP 添加到允许的连接中。
这是 gmail 问题,您需要关闭“不太安全的应用程序访问”
关闭“不太安全的应用程序访问”
https://myaccount.google.com/lesssecureapps
示例(PHPMailer v6.2.0)
<?php
/**
* This example shows settings to use when sending via Google's Gmail servers.
* This uses traditional id & password authentication - look at the gmail_xoauth.phps
* example to see how to use XOAUTH2.
* The IMAP section shows how to save this message to the 'Sent Mail' folder using IMAP commands.
*/
//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
require 'vendor/autoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer();
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// SMTP::DEBUG_OFF = off (for production use)
// SMTP::DEBUG_CLIENT = client messages
// SMTP::DEBUG_SERVER = client and server messages
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption mechanism to use - STARTTLS or SMTPS
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = 'CHANGEME';
//Password to use for SMTP authentication
$mail->Password = 'CHANGEME';
//Set who the message is to be sent from
$mail->setFrom('from@example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('replyto@example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('CHANGEME', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->msgHTML('This is a plain-text message body');
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//send the message, check for errors
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent!';
//Section 2: IMAP
//Uncomment these to save your message in the 'Sent Mail' folder.
#if (save_mail($mail)) {
# echo "Message saved!";
#}
}
取自https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps
我不确定这里是否需要前导反斜杠:
$mailer = new \PHPMailer\PHPMailer\PHPMailer( true );
或在这里:
$mailer->SMTPSecure = \PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;