"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Fix \"SMTP Connect() Failed\" Error When Sending Emails via Gmail?

How to Fix \"SMTP Connect() Failed\" Error When Sending Emails via Gmail?

Published on 2024-11-06
Browse:755

How to Fix \

SMTP Connection Failure: Resolving "SMTP Connect() failed" Error

In attempting to send emails using Gmail, you may encounter an error message stating "SMTP -> ERROR: Failed to connect to server: Connection timed out (110)\nSMTP Connect() failed. Message was not sent.\nMailer error: SMTP Connect() failed." This error indicates a problem with establishing a connection to the SMTP server.

To resolve this issue, you need to modify the PHP code responsible for sending emails. Specifically, remove or comment out the line:

$mail->IsSMTP();

The IsSMTP() method is deprecated and should not be used. By removing or commenting out this line, the code will automatically use SMTP for sending emails, eliminating the connection issue and allowing successful email delivery.

Here is the updated code with the modification:

require 'class.phpmailer.php'; // path to the PHPMailer class
require 'class.smtp.php';

$mail = new PHPMailer();

$mail->Mailer = "smtp";
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 587;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "[email protected]"; // SMTP username
$mail->Password = "mypasswword"; // SMTP password 
$Mail->Priority = 1;

$mail->AddAddress("[email protected]","Name");
$mail->SetFrom($visitor_email, $name);
$mail->AddReplyTo($visitor_email,$name);

$mail->Subject  = "Message from  Contact form";
$mail->Body     = $user_message;
$mail->WordWrap = 50;  

if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
Release Statement This article is reprinted at: 1729503858 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3