简体   繁体   中英

SMTP -> ERROR: Failed to connect to server: (0) in PHP malier

Following is my php email code and it works fine still today.

require_once('_lib/class.phpmailer.php');
include 'func/db_connect.php';
error_reporting(E_ALL);
ini_set('display_errors', 1);

function supervisorMail(){

    global $error;
    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->SMTPDebug = 2;
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = 'ssl';
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 465;
    $mail->Username = "*****@gmail.com";
    $mail->Password = "*****";
    $mail->SetFrom("****@gmail.com", "Employee Leave Management System");
}

But now it does not work without any changing of code and it makes following error.

SMTP -> ERROR: Failed to connect to server: (0) SMTP Error: Could not connect to SMTP host. Message could not be sent. Mailer Error: SMTP Error: Could not connect to SMTP host.

I could not able to find the solution. How can I fixed this.

try this it will be work

$mail = new PHPMailer(); 
$mail->IsSMTP(); 
$mail->SMTPDebug = 1; 
$mail->SMTPAuth = true; 
$mail->SMTPSecure = 'ssl'; 
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; 
$mail->IsHTML(true);
$mail->Username = "*******@gmail.com";
$mail->Password = "*******";

you have to change in gmail.In Account permissions section, find Access for less secure apps and enable it.

I think you should try by changing the host like this :

$mail->Host       = "ssl://smtp.gmail.com";
$mail->Port       = 465;

And also set the other paramters like this :

$mail->SetFrom('contact@example.com', 'User');

$mail->AddReplyTo("example@gmail.com', 'Name Here");

$mail->Subject    = "Contact us Email";

$mail->MsgHTML($body);

$address = "user@example.com";
$mail->AddAddress($address, "new user");

if(!$mail->Send()) {
  echo "Error in Sending email: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}

In my case it was a lack of SSL support in PHP which gave this error.

So I enabled extension=php_openssl.dll

$mail->SMTPDebug = 1; also hinted towards this solution.

提供$ mail-> SMTPDebug = 1 itz工作[1]: http ://i.stack.imgur.com/ZEpjv.png

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM