Home > database >  Why am I getting "socket.gaierror [Errno 11001]" when trying to run my Python SMTP script?
Why am I getting "socket.gaierror [Errno 11001]" when trying to run my Python SMTP script?

Time:11-06

I have been trying to run the following code however I am getting the following error when the code runs. I am currently following a video on a course on Udemy, however, the video seems to be outdated as the option to turn on "Less secure apps" on Gmail can't be turned on anymore as Google disabled the option. To fix this, I have enabled 2-factor authentication and generated a new password for this python script. However, it is still not working and I don't know why. Please can somebody help?

import smtplib
from credentials import  *

connection = smtplib.SMTP("smtp.gmail.com", port = 587)

connection = smtplib.SMTP("smptp.gmail.com", port = 587)
connection.starttls()
connection.login(user=my_email, password=password)

connection.sendmail(from_addr=my_email, to_addrs=to_address, msg = "Testing")
connection.close()

Error as follows:

Traceback (most recent call last):
  File "C:\Users\user\Documents\Python\bootcamp 2022\day 32\Birthday Wisher (Day 32) start\Birthday Wisher (Day 32) start\main.py", line 9, in <module>
    connection = smtplib.SMTP("smptp.gmail.com" ,port =587)
  File "C:\Python39\lib\smtplib.py", line 255, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Python39\lib\smtplib.py", line 341, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:\Python39\lib\smtplib.py", line 312, in _get_socket
    return socket.create_connection((host, port), timeout,
  File "C:\Python39\lib\socket.py", line 822, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "C:\Python39\lib\socket.py", line 953, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

CodePudding user response:

getaddrinfo looks up the IP address for a domain name. The reason why this is failing is because you have a simple typo in the address for smtp.gmail.com. Just fix it and it should be fine. Like this:

import smtplib
from credentials import  *


connection = smtplib.SMTP("smtp.gmail.com", port=587)
connection.starttls()
connection.login(user=my_email, password=password)

connection.sendmail(from_addr=my_email, to_addrs=to_address, msg = "Testing")

connection.close()

Also, there is no need to redefine connection. Doing it only once will work.

  • Related