通过 PHPMailer 发送电子邮件

Send email via PHPMailer

我是这样组织代码的:

<?php

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

require '/home/g/grablmz2/psywomenandmen.ru/public_html/ads/libs/PHPMailer/Exception.php';
require '/home/g/grablmz2/psywomenandmen.ru/public_html/ads/libs/PHPMailer/PHPMailer.php';
require '/home/g/grablmz2/psywomenandmen.ru/public_html/ads/libs/PHPMailer/SMTP.php';


$mail = new PHPMailer(true);

try {
    echo 'start';
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // 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   = 'hidden';                     // SMTP username
    $mail->Password   = 'hidden';                               // SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
    $mail->Port       = 587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

    //Recipients
    $mail->setFrom('hidden', 'Mailer');
    $mail->addAddress('hidden', 'Michael');

   // Content
    $mail->Subject = 'Test';
    $mail->Body    = 'Test';

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

Gmail 中的“不太安全的应用程序”已开启。通过复制并粘贴到浏览器来双重检查登录名和密码。

异常:

start2020-12-11 16:36:00 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP c16sm931756lfb.236 - gsmtp
2020-12-11 16:36:00 CLIENT -> SERVER: EHLO psywomenandmen.ru
2020-12-11 16:36:00 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [5.101.156.177]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2020-12-11 16:36:00 CLIENT -> SERVER: STARTTLS
2020-12-11 16:36:00 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2020-12-11 16:36:00 CLIENT -> SERVER: EHLO psywomenandmen.ru
2020-12-11 16:36:00 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [5.101.156.177]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2020-12-11 16:36:00 CLIENT -> SERVER: AUTH LOGIN
2020-12-11 16:36:00 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2020-12-11 16:36:00 CLIENT -> SERVER: [credentials hidden]
2020-12-11 16:36:00 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2020-12-11 16:36:00 CLIENT -> SERVER: [credentials hidden]
2020-12-11 16:36:01 SERVER -> CLIENT: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbv534-5.7.14 bXAEJKkJeqWCiVzTTlBWq9rxftBEamfSnaWIVJ0_dKDfgIuJ6a7XbSNz62WFwSuLLEQAO534-5.7.14 vcrBWQgPPNpki5WeHQlOd9wE2RNXirLYC1_og20m_xsWH4wka8pkDZJz0Iv-LySW>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 c16sm931756lfb.236 - gsmtp
2020-12-11 16:36:01 SMTP ERROR: Password command failed: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbv534-5.7.14 bXAEJKkJeqWCiVzTTlBWq9rxftBEamfSnaWIVJ0_dKDfgIuJ6a7XbSNz62WFwSuLLEQAO534-5.7.14 vcrBWQgPPNpki5WeHQlOd9wE2RNXirLYC1_og20m_xsWH4wka8pkDZJz0Iv-LySW>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 c16sm931756lfb.236 - gsmtp
SMTP Error: Could not authenticate.
2020-12-11 16:36:01 CLIENT -> SERVER: QUIT
2020-12-11 16:36:01 SERVER -> CLIENT: 221 2.0.0 closing connection c16sm931756lfb.236 - gsmtp
SMTP Error: Could not authenticate.
Message could not be sent. Mailer Error: SMTP Error: Could not authenticate.

我是不是在端口和其他选项上做错了什么?

不,您在端口和选项方面没有做错,它们都很好。你做错的是没有阅读错误信息

<https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbv
534-5.7.14 bXAEJKkJeqWCiVzTTlBWq9rxftBEamfSnaWIVJ0_dKDfgIuJ6a7XbSNz62WFwSuLLEQAO
534-5.7.14 vcrBWQgPPNpki5WeHQlOd9wE2RNXirLYC1_og20m_xsWH4wka8pkDZJz0Iv-LySW>
534-5.7.14 Please log in via your web browser and then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/answer/78754 c16sm931756lfb.236 - gsmtp

这提供了一个 link 页面,告诉您更多信息,还有一个简单的说明:“请通过您的网络浏览器登录,然后重试”。这意味着您应该通过浏览器登录 gmail,然后再尝试 运行 您的脚本。

对于 gmail 的解锁验证码页面,您可能还需要执行其他步骤,但您可以通过阅读包含 full details of this exact issue.

的 PHPMailer 故障排除指南了解相关信息。