Home  >  Article  >  Backend Development  >  Why is my PHPmailer SMTP connect() failing with Gmail and how can I fix it?

Why is my PHPmailer SMTP connect() failing with Gmail and how can I fix it?

Barbara Streisand
Barbara StreisandOriginal
2024-10-27 04:26:03814browse

Why is my PHPmailer SMTP connect() failing with Gmail and how can I fix it?

SMTP connect() failed PHPmailer - PHP

This issue arises when you encounter an error while attempting to send an email using PHPmailer, specifically "Mailer Error: SMTP connect() failed." The underlying cause is often related to authentication settings and compatibility with your email provider.

In this instance, the solution involves enabling less secure apps for your Google account. Google recently implemented XOAUTH2 authentication, which requires you to explicitly allow access to third-party applications.

To resolve this:

  • Visit https://www.google.com/settings/security/lesssecureapps
  • Check the "Turn on" option
  • This step authorizes PHPmailer to connect to the Google SMTP server using the XOAUTH2 mechanism.

Additionally, ensure that you are using the correct SMTP settings:

  • SMTP Server: smtp.gmail.com
  • Port: 587
  • TLS: Enabled
  • Authentication: Yes
  • Username: Your Google account email address
  • Password: Your Google account password

Here's an updated code sample with these settings:

<code class="php">require_once 'C:\xampp\htdocs\email\vendor\autoload.php';

define ('GUSER','[email&#160;protected]');
define ('GPWD','your password');

function smtpmailer($to, $from, $from_name, $subject, $body) { 
    global $error;
    $mail = new PHPMailer();  // create a new object
    $mail->IsSMTP(); // enable SMTP
    $mail->SMTPDebug = 2;  // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true;  // authentication enabled
    $mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail
    $mail->SMTPAutoTLS = false;
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 587;

    $mail->Username = GUSER;  
    $mail->Password = GPWD;           
    $mail->SetFrom($from, $from_name);
    $mail->Subject = $subject;
    $mail->Body = $body;
    $mail->AddAddress($to);
    if(!$mail->Send()) {
        $error = 'Mail error: '.$mail->ErrorInfo; 
        return false;
    } else {
        $error = 'Message sent!';
        return true;
    }
}</code>

By enabling less secure apps and using the correct SMTP settings, you should be able to successfully send emails using PHPmailer with Gmail's SMTP server.

The above is the detailed content of Why is my PHPmailer SMTP connect() failing with Gmail and how can I fix it?. 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