I've got a script in PHP that's been running fine for months. It recently stopped working.

This script connects to gmail to send out an email to my customers.

Recently, I started getting this error when I run the script:

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'

Keep in mind, this was with zero code changes.

I've looked at my resolv.conf and it appears to be ok:


I can ping gmail.com:

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

I can connect via lynx to google and other sites with no problem.

I've logged into my gmail account with no problems (no captcha there either).

I am at wits end. Anyone have any ideas?


  • Is the script failing all the time? Or just occasionally?
    – MikeyB
    May 31, 2009 at 3:29
  • Fails every time. May 31, 2009 at 3:37
  • I encountered similar issue with my daemon written in C which performs periodic TCP reconnections. At some point getaddrinfo() suddenly started to return the error. When I looked at the server, no DNS requests were being sent, and adding the required entry into /etc/hosts did not help. Sure, restart helps but as Xerxes rightfully noted, this is not the real solution. Initially I thought that the problem was caused by missing freeaddrinfo() call but I failed to reproduce it with a test application. Anyway, I've added proper cleanup calls to the daemon and going to monitor it closely.
    – Linulin
    Dec 21, 2009 at 11:54

6 Answers 6


PHP is having trouble accessing either /etc/hosts or /etc/resolv.conf: there's a long standing issue in PHP related to this specific error. The fix is to try restarting Apache or whatever is invoking PHP, or to make sure /etc/hosts and /etc/resolv.conf are readable by what's invoking PHP.

  • 1
    Restart did the trick! May 31, 2009 at 4:10
  • 4
    Yes, but how long before the next restart is due? I would look further and find the root cause of the problem.
    – khosrow
    May 31, 2009 at 5:06
  • Wow I was having the same issue, but apparently service httpd restart is the solution... but I still doesn't understand what causes this...
    – J_Wesker
    Aug 9, 2010 at 1:32

I just experienced the same error and

service httpd restart

did the trick...

% dig @ smtp.gmail.com +short

Now, try using Xdebug to see where the problem is exactly....

$ip = gethostbyname('smtp.gmail.com');

Anything good in the logs?


Add debugging code before that line to make sure that the script can resolve it correctly.


I have had this fault today, however it occurred after a specific event which makes me think that i may have found the cause.

Due to some network equipment trouble, i actually rebooted by server and when it came back up, there was an incorrect network connection in the cabling, effectively meaning no DNS server was available.

in the meantime this php function was called and after this it never worked again until i restarted httpd.

I think the bug may relate to a network failure (where the DNS is not only not available, but unreachable, ie wrong subnet) and when the network subsequently recovers this function does not look for the DNS again.

This thead was a few years ago, maybe this bug is now fixed in PHP?


I was having the following issue while upgrading magento 2 to the latest version, so I suspect it is the issue with the php configuration.

The "https://repo.magento.com/packages.json" file could not be downloaded: php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution 
failed to open stream: php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution

Resolved it by restarting the xampp through command.

sudo /opt/lampp/lampp/stop
sudo /opt/lampp/lampp/start

Then tried the composer update command again and it worked like a charm.

Not the answer you're looking for? Browse other questions tagged .