Uncaught exception 'PHP mailer Exception' with message SMTP connect() failed

天大地大妈咪最大 提交于 2019-12-08 12:23:04

问题


I am getting this error

Fatal error: Uncaught exception 'phpmailerException' with message 'SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting' in C:\xampp\htdocs\muhasibb\epaper\PHPMailer-master\class.phpmailer.php:1465 Stack trace: #0 C:\xampp\htdocs\muhasibb\epaper\PHPMailer-master\class.phpmailer.php(1301): PHPMailer->smtpSend('Date: Tue, 17 O...', 'This is Yousaf ...') #1 C:\xampp\htdocs\muhasibb\epaper\PHPMailer-master\class.phpmailer.php(1181): PHPMailer->postSend() #2 C:\xampp\htdocs\muhasibb\epaper\1.php(33): PHPMailer->send() #3 {main} thrown in C:\xampp\htdocs\muhasibb\epaper\PHPMailer-master\class.phpmailer.php on line 1465

while i am using code

<?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 = 'yousaf.farooq906@gmail.com';                 // 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('yousaf.farooq906@gmail.com', 'Yousaf Farooq');
    $mail->addAddress('yousaf.farooq906@gmail.com', 'Joe User');     // Add a recipient
    $mail->addAddress('ellen@example.com');               // 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.';
?>

回答1:


try setting your

$mail->SMTPSecure

to

$mail->SMTPSecure = 'tls';

Also, create app password for your google account and use it in exchange for your password in $mail->Password.




回答2:


This code worked for me

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

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

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

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 = "yourmail@gmail.com";
 $mail->Password = "yourpassword";
 $mail->From="yourmail@gmail.com";
 $mail->FromName="YOUR NAME";
 $mail->Subject = $subject;
 $mail->Body = $body;
 $mail->addAddress("somemail@gmail.com");

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

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

GMAIL CONFIGURATION




回答3:


If working in localhost,

In php.ini: remove the semicolon in front of "sendmail_from = postmaster@localhost" and restart xampp.

In gmail account: in the security settings, turn on "Allow less secure apps".

And finally in PHP script: add two functions: "date_default_timezone_set('Etc/UTC')" "gethostbyname('ssl://smtp.gmail.com')" as a value for " $mail->Host"



来源:https://stackoverflow.com/questions/46785313/uncaught-exception-php-mailer-exception-with-message-smtp-connect-failed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!