PHP SMTP Connection Error: Troubleshooting “Temporary Failure in Name Resolution” with Gmail


2 views

When your PHP script suddenly fails with "Temporary failure in name resolution" after months of working perfectly, it's particularly frustrating. Let's dive into the technical details of this specific SMTP/Gmail connection issue.

Before jumping to conclusions, verify these fundamental checks:

// Test DNS resolution from command line
nslookup smtp.gmail.com

// Alternative test using PHP
<?php
print_r(dns_get_record('smtp.gmail.com', DNS_ALL));
?>

The error suggests PHP's network stack is having trouble resolving hostnames, while system tools work fine. This typically indicates:

  • PHP using different DNS resolvers than system tools
  • SELinux/apparmor restrictions
  • PHP configuration issues

Try these solutions in order of complexity:

// 1. Force IPv4 if IPv6 is misconfigured
$smtp = new SMTP();
$smtp->connect('smtp.gmail.com', 465, 30, array('ssl' => array('verify_peer' => false)));

// 2. Explicit DNS in php.ini
[PHP]
; Add to your php.ini
dns_server = "8.8.8.8,8.8.4.4"

When basic fixes don't work, try these advanced methods:

// Create test script to isolate DNS issues
<?php
$host = 'smtp.gmail.com';
$ip = gethostbyname($host);
if ($ip === $host) {
    echo "DNS resolution failed completely\n";
    // Implement fallback IPs
    $ip = '74.125.200.109'; // Gmail SMTP IP
}

// Alternative connection method
$context = stream_context_create(array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false
    )
));
$socket = stream_socket_client(
    "tls://$ip:465",
    $errno,
    $errstr,
    30,
    STREAM_CLIENT_CONNECT,
    $context
);

For production environments, consider these robust approaches:

  • Implement a DNS caching layer (like dnsmasq)
  • Use a connection pool with fallback IPs
  • Monitor DNS resolution with a cron job
// Example connection pool implementation
class SmtpConnectionPool {
    private $servers = [
        'smtp.gmail.com' => ['74.125.200.109', '74.125.200.108'],
        'backup.smtp.gmail.com' => ['172.217.0.109']
    ];
    
    public function connect() {
        foreach ($this->servers as $host => $ips) {
            foreach ($ips as $ip) {
                try {
                    return $this->tryConnect($ip);
                } catch (Exception $e) {
                    continue;
                }
            }
        }
        throw new Exception("All connection attempts failed");
    }
}

Recently, I encountered a puzzling issue where a long-running PHP script suddenly failed to connect to Gmail's SMTP server. The error message was particularly frustrating:

The SMTP connection failed to start [tls://smtp.gmail.com:465]: 
fsockopen returned Error Number 0 and Error String 'php_network_getaddresses: 
getaddrinfo failed: Temporary failure in name resolution'

First, I verified basic network connectivity:

# ping smtp.gmail.com
PING gmail-smtp-msa.l.google.com (74.125.93.111) 56(84) bytes of data.
64 bytes from qw-in-f111.google.com (74.125.93.111): icmp_seq=1 ttl=247 time=26.7 ms

This confirmed that DNS resolution was working at the system level. My /etc/resolv.conf appeared correct:

nameserver 208.67.222.222

The key insight came when I realized that PHP might be using different DNS resolution methods than the system. Here's how I tested PHP's DNS capabilities:

<?php
$host = 'smtp.gmail.com';
$ip = gethostbyname($host);
if ($ip === $host) {
    echo "PHP DNS resolution failed!";
} else {
    echo "Resolved IP: " . $ip;
}
?>

After extensive testing, these approaches proved effective:

// Solution 1: Use IP directly (temporary workaround)
$smtpHost = '74.125.93.111'; // Resolved IP for smtp.gmail.com
// Remember to include the hostname for TLS verification
$mail->Hostname = 'smtp.gmail.com';

// Solution 2: Modify PHP's DNS configuration
ini_set('default_socket_timeout', 15);
ini_set('dns.timeout', 5);
ini_set('dns.retry', 2);

// Solution 3: Alternative DNS servers in resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4

For persistent issues, consider these deeper diagnostics:

// Test DNS resolution with different methods
print_r(dns_get_record('smtp.gmail.com', DNS_ALL));

// Check system DNS configuration
$digOutput = shell_exec('dig smtp.gmail.com');
echo "<pre>$digOutput</pre>";

To avoid future occurrences:

  • Implement connection retry logic in your PHP code
  • Monitor DNS resolution failures
  • Consider using a local DNS cache like nscd
  • Set up alternative mail delivery methods