Home  >  Article  >  Backend Development  >  How to Resolve SMTP Connect() Timeouts with PHPMailer?

How to Resolve SMTP Connect() Timeouts with PHPMailer?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-21 17:50:03976browse

How to Resolve SMTP Connect() Timeouts with PHPMailer?

SMTP Connect() Error: Troubleshooting Connection Timeouts

When attempting to send emails using PHPMailer, users may encounter the following error: "SMTP -> ERROR: Failed to connect to server: Connection timed out (110)SMTP Connect() failed. Message was not sent.Mailer error: SMTP Connect() failed." This error often stems from connection difficulties between the server and the PHP script.

Upon examining the provided PHP code, it was noticed that the line "$mail->IsSMTP();" was present. According to the documentation for PHPMailer, this method is deprecated and is no longer necessary. Removing or commenting out this line solves the connection timeout issue:

<code class="php">// Remove or comment out the following line:
// $mail->IsSMTP();</code>

By removing or commenting out the "IsSMTP()" method, the connection to the SMTP server can be established successfully. The full code below demonstrates the corrected PHPMailer configuration:

<code class="php">require 'class.phpmailer.php'; // path to the PHPMailer class
require 'class.smtp.php';

$mail = new PHPMailer();

$mail->Mailer = "smtp";
$mail->SMTPDebug = 2;
$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.';
}</code>

By implementing these changes, the error "SMTP Connect() failed" should be resolved, allowing emails to be sent successfully via PHPMailer using SMTP authentication.

The above is the detailed content of How to Resolve SMTP Connect() Timeouts with PHPMailer?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn