I'm trying to make a mail sender in Python, the script works on my personal laptop, but when I run it on my work laptop, I think the proxy gets in the way of getting a connection to the Gmail SMTP server
The error is as follows:
File "D:\ocm-hours-report-automation\mail-manager\src\python\mail-sender.py", line 44, in <module>
session = smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port
File "C:\Users\myuser\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
File "C:\Users\myuser\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 341, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\myuser\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 312, in _get_socket
return socket.create_connection((host, port), timeout,
File "C:\Users\myuser\AppData\Local\Programs\Python\Python39\lib\socket.py", line 822, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Users\myuser\AppData\Local\Programs\Python\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
The code is:
#The mail addresses and password
sender_address = '[email protected]'
sender_pass = 'password'
receiver_address = '[email protected]'
#Setup the MIME
message = MIMEMultipart()
message['From'] = sender_address
message['To'] = receiver_address
message['Subject'] = 'A test mail sent by Python. It has an attachment.' #The subject line
#The body and the attachments for the mail
message.attach(MIMEText(mail_content, 'plain'))
#Create SMTP session for sending the mail
session = smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port
session.starttls() #enable security
session.login(sender_address, sender_pass) #login with mail_id and password
text = message.as_string()
session.sendmail(sender_address, receiver_address, text)
session.quit()
print('Mail Sent')
Does someone have any idea what I can actually do? I tried forcing a socks.setdefaultproxy but it said the socks import is not available.
Your suspicion is correct. socket.gaierror: [Errno 11001] getaddrinfo failed indicates the application cannot resolve the IP address of the host. There's various ways to get around this depending on the operating system of the machine running the code such as manually mapping in /etc/hosts. But, even if the IP address is resolved, that doesn't mean the proxy will allow a connection to it.
In my case, I was getting this error because I'd lost my internet connection. I think I should share it here so that someone will not have to waste time
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With