本文介绍了消息SMTP connect()的未捕获异常'PHP mailer Exception'失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误

我在使用代码时

<?php
                            // Passing `true` enables exceptions

    //Server settings

    require 'PHPMailer-master/PHPMailerAutoload.php';
    $mail = new PHPMailer(true);                                // Enable verbose debug output
    $mail->isSMTP();
    $mail->SMTPDebug = 2;                                     // Set mailer to use SMTP
    $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = '[email protected]';                 // SMTP username
    $mail->Password = '********';                           // SMTP password
    $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;
    $mail->Mailer = "smtp";                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('[email protected]', 'Yousaf Farooq');
    $mail->addAddress('[email protected]', 'Joe User');     // Add a recipient
    $mail->addAddress('[email protected]');               // Name is optional


    //Attachments
        // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Yousaf Farooq';
    $mail->Body    = 'This is Yousaf Farooq';


    if($mail->send())
    echo 'Message has been sent';
    else
    echo 'Message could not be sent.';
?>

推荐答案

此代码对我有用

$mail = new PHPMailer(true); // create a new object

我评论了这一行 //$ mail-> isSMTP(); //启用SMTP

I commented this line //$mail->isSMTP(); // enable SMTP

 $mail->SMTPDebug = 4;
 $mail->SMTPAuth = true; // authentication enabled

您必须使用tls 587端口

You have to use tls whith port 587

 $mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for Gmail
 $mail->Host = "smtp.gmail.com";
 $mail->Port = 587;
 $mail->Mailer = "smtp";
 $mail->isHTML(true);
 $mail->Username = "[email protected]";
 $mail->Password = "yourpassword";
 $mail->From="[email protected]";
 $mail->FromName="YOUR NAME";
 $mail->Subject = $subject;
 $mail->Body = $body;
 $mail->addAddress("[email protected]");

 if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
 } else {
   echo "Email Sent";
 }

另外,当您使用gmail时,请转到具有帐户访问权限的应用并设置允许不太安全的应用:打开"

Also when you are using gmail you go to Apps with account access and set Allow less secure apps: ON

邮件配置

这篇关于消息SMTP connect()的未捕获异常'PHP mailer Exception'失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 07:16