PHPMailer, SMTP connect() failed error with Gmail

PHPMailer, SMTP connect() failed error with Gmail

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Real Culprit Behind PHPMailer and Gmail: OAuth2 and App Passwords

The error SMTP connect() failed is a common hurdle when migrating from a local development environment like XAMPP to a production host, but it rarely indicates an issue with your server's network configuration or the PHPMailer library itself. In the context of Google Workspace accounts, including Gmail, this error almost always points to one specific cause: OAuth2 and App Passwords.

When you run code locally on your machine, your browser often handles authentication seamlessly via third-party client flows (like clicking "Sign in with Google"). However, server-side scripts using PHPMailer are headless; they have no way to trigger the OAuth consent screen for a standard account password. Attempting to authenticate a standard Gmail account with just an email and password ($m->Username = "mymail@gmail.com"; $m->Password = "mypass";) on a server will fail because Google's SMTP servers reject these credentials due to the "Less secure apps" setting being disabled by default for security reasons.

To resolve this, you must generate an App Password. This is a specific 16-character password generated within your Google Account settings that bypasses the need for your primary account password while maintaining security standards. Without switching to an App Password, the SMTP connect() method will fail regardless of whether you use port 465 with SSL or port 587 with TLS.

Configuring PHPMailer for Gmail

Once you have generated an App Password in your Google Account (usually found under Security > 2-Step Verification > App passwords), you must update your PHP code to use this credential instead of your standard login password.

Here is the corrected implementation using PHPMailer:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$m = new PHPMailer(true);

try {
    // Enable SMTP debugging to see connection details
    $m->SMTPDebug = 2; 

    $m->isSMTP();
    $m->Host       = 'smtp.gmail.com';
    $m->SMTPAuth   = true;
    
    // CRITICAL: Use the App Password here, not your main password
    $m->Username   = 'mymail@gmail.com';
    $m->Password   = 'generated_app_password_here'; 
    
    // Port 465 requires SSL
    $m->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
    $m->Port       = 465;

    // Alternatively, for TLS (Port 587):
    // $m->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    // $m->Port       = 587;

    $m->isHTML(true);

    $m->Subject = 'Hello world';
    $m->Body    = 'Some content';
    $m->From    = 'mymail@gmail.com', 'Contact';
    $m->addAddress('recipient@example.com', 'Test');

    $m->send();
} catch (Exception $e) {
    echo "Message could not be sent. Mailer error: {$mailers->ErrorInfo}";
}

Why Port and Security Matter

It is tempting to keep switching between port 465/SSL and port 587/TLS until the connection succeeds, but consistency is key for maintainability.

  • Port 465 (SSL): This is the legacy standard. It wraps the entire SMTP session in SSL immediately upon connection. If your code specifies SMTPSecure = "ssl" and Port = 465, ensure you use the ENCRYPTION_SMTPS constant if using modern PHPMailer versions, or the string "ssl" for older ones.
  • Port 587 (TLS): This is the current recommended standard by many developers. It starts with a plain connection and then upgrades to TLS after authentication. If you choose this route, set $m->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS and Port = 587.

The error SMTP connect() failed specifically means the TCP handshake succeeded (you reached the server), but the SMTP handshake failed because the credentials were rejected. Google logs these attempts as "less secure app" warnings if you are using a regular password, which is why they block them entirely on production servers today.

Troubleshooting Steps

If you have switched to an App Password and still face connection issues, verify the following:

  1. Account Type: Ensure your account has 2-Step Verification enabled. Google requires this step before allowing App Password generation.
  2. App Password Validity: Verify that the generated password is exactly 16 characters long. Copying it directly from the dialog box in a single click usually works best to avoid trimming spaces.
  3. Firewall/Host Environment: While less likely, some hosting providers (like Heroku or certain shared hosts) may block outgoing connections on port 465 or 587 due to firewall rules. If you are stuck on a specific host, check their documentation for allowed outbound ports.

By switching to an App Password, you align your server-side script with Google's modern security protocols, eliminating the SMTP connect() failed error and allowing your contact form to function reliably in production.

Our network