服务器 SMTP PHPMailer 的问题

problems with server SMTP PHPMailer

我的 phpmailer 有问题,我有正确的 SMPT 服务器数据以及用户名和密码,但它仍然标记我的凭据错误,这是我的代码

  <?php 
include ("class.phpmailer.php");
include ("../vendor/phpmailer/phpmailer/PHPMailer.php");
include ("../vendor/phpmailer/phpmailer/src/SMTP");
 // Import PHPMailer classes into the global namespace
    // These must be at the top of your script, not inside a function
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;

    require '../vendor/phpmailer/phpmailer/src/Exception.php';
    require '../vendor/phpmailer/phpmailer/src/PHPMailer.php';
    require '../vendor/phpmailer/phpmailer/src/SMTP.php';
    require '../vendor/autoload.php';

    $name = trim(stripslashes($_POST['contactName']));
    $email = trim(stripslashes($_POST['contactEmail']));
    $subject = trim(stripslashes($_POST['contactSubject']));
    $contact_message = trim(stripslashes($_POST['contactMessage']));


    // Load Composer's autoloader


    // Instantiation and passing `true` 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   = 'admin@ucarolina.mx';                     // SMTP username
     $mail->Password   = '******';                             // SMTP password
     $mail->SMTPSecure = 'TSL';                                  // Enable TLS encryption, `ssl` also accepted
     $mail->Port       = 587;                                    // TCP port to connect to


        //Recipients
        $mail->setFrom('luribe@qvoz.com');
        $mail->addAddress($email, $name);     // Add a recipient


        // Content
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = $subject;
        $mail->Body    = $contact_message;
        $mail->AltBody = $contact_message;

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


?>

这是标记我的错误 SMTP 错误:无法验证。 无法发送消息。邮件程序错误:SMTP 错误:无法验证。

有一行必须指定SMTPSecure。您有该行,但 tls 被错误输入为 TSL。让我们改变一下:

改变

$mail->SMTPSecure = 'TSL'; 

$mail->SMTPSecure = 'tls'; 

此外,您应该像这样在 $mail->send(); 周围添加一个 if/then:

if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

如果邮件未发送,您的 if 语句将捕获并报告错误。请参阅 PHPMailer 示例中此类错误处理的示例:https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps