Swiftmailer: Connection could not be established with host smtp.gmail.com [Connection timed out #110]
When you encounter Connection timed out #110 with Gmail SMTP on a production server while your local environment works perfectly, standard troubleshooting often feels like banging your head against a wall. You've likely already enabled OpenSSL, configured Application Specific Passwords, and whitelisted the IPs. The fact that the code works locally but fails remotely usually points to network topology or TLS handshake mismatches rather than application logic errors.
The error Connection timed out #110 is an SMTP-specific response indicating that the server accepted the connection request but could not complete it within a reasonable timeframe. In many cases, this isn't actually a timeout in the traditional sense of the packet being lost; it's often a silent failure where the TLS handshake fails so quickly or silently that the SwiftMailer library interprets the lack of a valid response as a timeout.
The DNS Resolution Trap
You mentioned wondering if this is a DNS issue. It absolutely can be, but rarely because you are pointing to MX records instead of the SMTP host directly. The confusion often stems from how mail clients and libraries resolve addresses.
MX records tell your domain where incoming mail should go. They do not dictate how your application connects to outgoing services like Gmail's SMTP relay. If your application is resolving smtp.gmail.com via DNS, it will find the correct A or AAAA record for that hostname. However, if your server has split-brain DNS or specific resolver configurations that restrict external resolution, you might get a timeout trying to reach an external IP that your local machine can ping without issue.
Verify your server's ability to resolve the address independently of your application:
ping smtp.gmail.com
nslookup -type=A smtp.gmail.com
dig smtp.gmail.com
If these commands hang or return errors on your production server while working fine locally, you have a fundamental network configuration issue unrelated to SwiftMailer itself. Ensure your server's /etc/resolv.conf (Linux) or Network Settings (Windows) allows outbound queries to Google's DNS servers.
The TLS Handshake Mismatch
The most common cause for "Connection timed out" on port 465 specifically is a mismatch between the encryption method expected by SwiftMailer and what the server provides, or how the connection initiates.
Port 465 was historically used for SMTP over SSL (implicit TLS). However, Gmail has shifted its architecture. While port 465 still works for explicit SSL connections, modern PHP versions and OpenSSL configurations sometimes struggle with the specific cipher suites or certificate validation required by Google's updated certificates.
If you are using an older version of SwiftMailer bundled with a legacy Laravel installation, you might be defaulting to TLSv1.0 or TLSv1.1, which Google has deprecated for security reasons. You must explicitly enforce modern encryption protocols. In your .env file or configuration array, ensure you are not relying on defaults that might fall back to insecure ciphers.
// Example of ensuring explicit SSL settings in SwiftMailer config
'stream_context' => [
'ssl' => [
'verify_peer' => true,
'verify_peer_name' => true,
'SNI_enabled' => true, // Crucial for shared hosting environments
],
],
The verify_peer setting ensures that your server is actually talking to smtp.gmail.com and not an attacker pretending to be it. If you are running this on a shared hosting environment, SNI_enabled (Server Name Indication) is critical because the IP address alone might not be enough for the SSL handshake to succeed if the hosting provider's OpenSSL stack is misconfigured regarding SNI support.
Application Specific Passwords and Auth Mode
You noted using an Application Specific Password (App Password). This is mandatory now when 2-Factor Authentication is enabled on the Google account, which is the default for most Google Workspace accounts. However, ensure that the auth_mode in your configuration matches the capabilities of the SwiftMailer version you are running.
Older versions of SwiftMailer used login, but newer versions prefer plain or cram-md5. If you are using a very recent version of SwiftMailer with an older PHP build, switching from login to plain might resolve handshake issues where the server rejects the initial authentication attempt as a timeout.
Checking Firewall and Egress Rules
Finally, consider your production firewall rules. While whitelisting Gmail's IPs is a good step, ensure that your egress traffic isn't being rate-limited or blocked by an intermediate load balancer or cloud security group (like AWS Security Groups or Azure NSGs). Sometimes, connections to external IPs are allowed but throttled aggressively, causing the 110 timeout error.
Check if you can establish a raw TCP connection to the host using telnet or nc:
# Attempting a raw connection to port 465
timeout 10 bash -c 'echo "EHLO test" | nc smtp.gmail.com 465'
If this command hangs immediately, your firewall is blocking the outbound connection entirely. If it connects but then times out, the issue lies deeper in the TLS handshake or certificate validation discussed earlier. Addressing these network and protocol layers will resolve the Connection timed out #110 error without needing to rewrite your SwiftMailer logic.