Home >Backend Development >PHP Tutorial >SMTP Connection: Why am I Getting \'SMTP Connect() failed. Message was not sent.\'?

SMTP Connection: Why am I Getting \'SMTP Connect() failed. Message was not sent.\'?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-21 17:48:03713browse

SMTP Connection: Why am I Getting

SMTP Connection Error: "SMTP Connect() failed. Message was not sent."

The provided PHP code for sending emails using SMTP encounters an error, "SMTP -> ERROR: Failed to connect to server: Connection timed out (110)".

The issue stems from the inclusion of both $mail->IsSMTP(); and $mail->Mailer = "smtp"; lines in the code. These lines are redundant and, when used together, can lead to conflicting configurations.

Solution:

To resolve the error, follow the provided solution and comment out or remove the line:

<code class="php">$mail->IsSMTP();</code>

With this line removed, the PHPmailer class uses the $mail->Mailer setting to determine the method of sending emails, which is SMTP in this case.

Example Code:

<code class="php">// require necessary classes
require 'class.phpmailer.php';
require 'class.smtp.php';

// initialize PHPMailer
$mail = new PHPMailer();

// use SMTP
$mail->Mailer = "smtp";

// configure SMTP settings
$mail->SMTPDebug = 2;
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = "[email protected]";
$mail->Password = "mypasswword";
$mail->Priority = 1;

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

// compose email
$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>

The above is the detailed content of SMTP Connection: Why am I Getting \'SMTP Connect() failed. Message was not sent.\'?. 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