服务器SMTP PHPMailer出现问题

时间:2019-04-11 20:22:46

标签: 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错误:无法验证。

1 个答案:

答案 0 :(得分:1)

必须在一行中指定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