I was having issues with the Pi so I put a new image of Jessie on it. I reloaded a python script that has been running for a year and a half that sends a email. Now when it tries to send the email I get these errors. Does anyone know what might have changed that is causing this?


  File "GarageDoor3.py", line 111, in <module>
  File "GarageDoor3.py", line 35, in sendEmail
  File "/usr/lib/python3.4/smtplib.py", line 242, in __init__
(code, msg) = self.connect(host, port)
  File "/usr/lib/python3.4/smtplib.py", line 321, in connect
self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/lib/python3.4/smtplib.py", line 292, in _get_socket
  File "/usr/lib/python3.4/socket.py", line 491, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/usr/lib/python3.4/socket.py", line 530, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

My code is:

def sendEmail(subject):
        content=(subject + ' is opened')
        mail.sendmail('[email protected]','[email protected]',content)
  • 1
    I determined the problem to be in my dhcpcd.conf file. It was where I had set the static domain name servers, instead of domain_name_servers I had domain_name_server. Problem solved.
    – Mobalized
    Nov 16, 2016 at 12:11
  • I made my comment into an answer to make it more obvious the issue is solved. Nov 16, 2016 at 13:00

2 Answers 2


The error "Name or service not known" comes from Linux NSS. This is easy to confirm: other programs using NSS (e.g. ping smtp.gmail.com) should fail with a similar error.

Non-local network host names usually come from DNS servers, which are configured either statically, or via DHCP. Fixing DNS configuration and making sure your DNS server is working is therefore the first step to fix the issue.


I had the same error, and realized that my DNS (from my DHCP) was not working, set it to for the Google one. Now works again!

  • 1
    I didn't downvote, but I guess one reason for your downvote is that you must be confusing DHCP with DNS. The IP is a Google DNS server - Nothing to do with DHCP.
    – Brick
    Aug 14, 2017 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.