how to send mail from phpmailer using SMTP Gmail settings

how to send mail from phpmailer using SMTP Gmail settings

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

Stefan Bogdanescu

Founder & Senior Architect

Debugging Gmail SMTP Connection Errors in PHPMailer

When migrating a contact form from standard PHP mail() functions to PHPMailer for better deliverability, encountering an "SMTP Error: Could not connect to SMTP host" or "Connection refused (111)" is a common hurdle. This specific error code indicates that the TCP handshake between your server and smtp.gmail.com failed before any authentication could occur.

The root cause usually isn't a mismatch in PHPMailer configuration, but rather an environmental blockage preventing the connection. Here is how to diagnose and resolve this issue systematically.

Verifying Server-Level Restrictions

Gmail imposes strict security policies on SMTP connections. The most frequent reason for a "Connection refused" error is that your web server software (Apache or Nginx) is blocking outbound traffic on port 587, or your hosting provider has disabled SMTP access entirely.

Before adjusting PHP code, check if your server can initiate an external connection. Run the following command from your server's terminal:

telnet smtp.gmail.com 587
# OR
nc -zv smtp.gmail.com 587

If this command hangs or immediately returns "Connection refused," your hosting environment is blocking outbound SMTP traffic. You cannot bypass this with code; you must contact your host to enable port 587 for your domain. If they refuse, consider switching providers or using a different mail service like SendGrid or Mailgun that supports custom relay configurations.

Enforcing TLS Encryption

Gmail requires Transport Layer Security (TLS) for SMTP connections. In your PHPMailer configuration, the SMTPSecure property must be set to 'tls'. However, simply setting this isn't enough if the server environment forces SSL/TLS mismatches.

Ensure your code explicitly sets the security protocol:

$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'venkateshm2280@gmail.com';
// Note: Never hardcode passwords in production. Use environment variables or a .env file.
$mail->Password = getenv('GMAIL_APP_PASSWORD'); 
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Prefer the constant over string 'tls'

Using the PHPMailer::ENCRYPTION_STARTTLS constant is more robust than passing the string 'tls', as it abstracts version differences between PHPMailer releases. The port 587 is correct for STARTTLS; do not use port 465 unless you are explicitly configuring SSL encryption, which requires a different SMTPSecure setting (PHPMailer::ENCRYPTION_SMTPS).

Authentication and App Passwords

Gmail no longer accepts your standard account password for third-party applications like PHP scripts. You must generate an "App Password" from your Google Account security settings. This 16-character code is distinct from your main login credentials.

In the provided source code, the hardcoded password 'mmuxhssfdrqnsowp' suggests this might be an App Password, but it is a critical security risk to expose it directly in the script. If you are using Laravel, leverage the framework's built-in .env system:

// .env file
GMAIL_APP_PASSWORD="mmuxhssfdrqnsowp"

// config/mail.php or MailServiceProvider
'mailgun' => [
    'username' => env('GMAIL_USERNAME'),
    'password' => env('GMAIL_APP_PASSWORD'),
],

Hardcoding credentials violates secure coding practices and exposes your account if the repository is leaked.

Debugging Connection Flow

If the connection still fails after verifying network access and encryption settings, enable debug output to see exactly where the handshake breaks. Set SMTPDebug to 3 for verbose logging:

$mail->SMTPDebug = 3;
$mail->isDebug(); // Ensure this is called before sending

With SMTPDebug = 3, PHPMailer will print the SMTP conversation to your error log or browser output. Look for lines indicating "Connection timeout" or "Connection refused." If the logs show a successful handshake but authentication fails, check that the username and password are exactly correct (no extra spaces).

Moving Beyond Vanilla PHP

While vanilla PHPMailer works well for simple scripts, managing SMTP credentials and debugging complex mail flows can become cumbersome as your application scales. For modern Laravel applications, integrating with services like https://laravelcompany.com often provides streamlined solutions that handle DNS records, SPF/DKIM verification, and credential management securely out of the box. These managed services reduce the friction of configuring raw SMTP connections, allowing you to focus on business logic rather than troubleshooting port blocks or TLS handshakes.

Our network