SMTP错误:无法使用PHPMailer和Gmail帐户进行身份验证

q3qa4bjr  于 2023-02-15  发布在  PHP
关注(0)|答案(1)|浏览(203)

我正试图发送一封电子邮件使用phpmail和从我的gmail帐户。但它显示
SMTP错误:密码命令失败:535-5.7.8用户名和密码不被接受。要了解更多信息,请访问535 5.7.8 https://support.google.com/mail/?p=BadCredentials 5- 20020aca060500000b0037d8d231f8csm951314oig.51- gsmtp SMTP错误:无法验证。
不幸的是,使用“不太安全的应用程序”的选项已被Google关闭,因此我无法使用该选项。我可以尝试解决此问题的其他选项有哪些?
以下是我目前的脚本:

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = 2;                      // Enable verbose debug output
    $mail->isSMTP();                                            // Send using SMTP
    $mail->Host       = 'smtp.gmail.com';                    // Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = 'lsurahman157@gmail.com';                     // SMTP username
    $mail->Password   = 'mypasswodis!';                               // SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
    $mail->Port       = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('lsurahman157@gmail.com', 'Full Name');
    $mail->addAddress('aonik1@lsu.edu', 'Abdur Rahman Onik');     // Add a recipient

    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Subject Text';
    $mail->Body    = '<i>Mail body in HTML</i>';
    $mail->AltBody = 'This is the plain text version of the email content';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
xe55xuns

xe55xuns1#

如果您为您的帐户激活了MFA,您就可以定义应用程序密码。您可以使用该密码进行SMTP和IMAP身份验证。

相关问题