Python socket.gaierror: [Errno 11001] getaddrinfo failed - python

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'm a recent intern) 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
and the code is:
#The mail addresses and password
sender_address = 'email1#gmail.com'
sender_pass = 'password'
receiver_address = 'email2#gmail.com'
#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')
any ideas what i can actually do? tried forcing a socks.setdefaultproxy but it said the socks import is not available
thank you!

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

Related

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

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
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.

Trying to send email through python and getting error with socket

This is my code
import smtplib
from datetime import datetime
print("Sending Mail")
with smtplib.SMTP('smpt.gmail.com', 587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login('gmail', 'password')
print("Logged In")
subject = "Current Time: " + datetime.now.strftime("%d/%m/%Y %H:%M:%S")
smtp.sendmail('myemail#gmail.com', 'myemail#gmail.com', "hello")
print("Sent Email")
I'm trying to send an email through gmail using smtplib, this is the error I get:
Sending Mail
Traceback (most recent call last):
File "c:/Users/12488/Downloads/Key Logger/keylog.py", line 43, in <module>
with smtplib.SMTP('smpt.gmail.com', 587) as smtp:
File "C:\Users\12488\AppData\Local\Programs\Python\Python38\lib\smtplib.py", line 253, in __init__
(code, msg) = self.connect(host, port)
File "C:\Users\12488\AppData\Local\Programs\Python\Python38\lib\smtplib.py", line 339, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\12488\AppData\Local\Programs\Python\Python38\lib\smtplib.py", line 308, in _get_socket
return socket.create_connection((host, port), timeout,
File "C:\Users\12488\AppData\Local\Programs\Python\Python38\lib\socket.py", line 787, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Users\12488\AppData\Local\Programs\Python\Python38\lib\socket.py", line 918, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
I made sure less secure devices is checked on gmail but I haven't tried every solution on the internet because I'm new to python and this is my first time using smtp and I don't get a lot of them, I think this has something to do with socket because that's another popular python module from what I've read but I don't know much about it and I'm not sure, if anyone can simplify what is wrong and explain it to me like I'm a 5 year old, it would be greatly appreciated.
You're using an incorrect domain which is not being resolved by DNS servers.
smpt.gmail.com
should be:
smtp.gmail.com

How to reslove "socket.gaierror" error of python-smtplib?

Few hours back I have posted a post related to "Django email sending API" and its error. So I thought that first I should try something with "smtplib". Unfortunately, after struggling with "smtplib", I realise that it will also not work, because something is wrong with my code or my network or my machine which I am not able to figure out.
Can any body help me regarding to this?
As of now, after struggling a lot, I have tried hundreds of solution posted here and there and also I have tried to resolve with myself but nothing is working in my case kindly help.
Code is given bellow.
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import socks
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4, "172.16.0.2", '8084')
socks.wrapmodule(smtplib)
#smtp = smtplib.SMTP()
msg = MIMEMultipart()
msg['From'] = 'my#yahoo.com'
msg['To'] = 'example#gmail.com'
msg['Subject'] = 'simple email in python'
message = 'here is the email'
msg.attach(MIMEText(message))
mailserver = smtplib.SMTP('smtp.mail.yahoo.com',465)
# identify ourselves to smtp gmail client
mailserver.ehlo()
# secure our email with tls encryption
mailserver.starttls()
# re-identify ourselves as an encrypted connection
mailserver.ehlo()
mailserver.login('my#yahoo.com', 'pswd12345678')
mailserver.sendmail('my#yahoo.com','example#gmail.com',msg.as_string())
mailserver.quit()
This is the error coming again and again:
Traceback (most recent call last):
File "import_mail.py", line 21, in <module>
mailserver = smtplib.SMTP('smtp.mail.yahoo.com',465)
File "/usr/lib/python2.7/smtplib.py", line 256, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 316, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.7/smtplib.py", line 291, in _get_socket
return socket.create_connection((host, port), timeout)
File "/usr/lib/python2.7/socket.py", line 557, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -3] Temporary failure in name resolution
This error is raised as the 'EMAIL_BACKEND' definition is missing from the settings file. Make sure that the settings.py file contains the following line:-
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
IF you`re using the gmail account to set up a default interactive mail, use the following values:-
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587 #PORT NO
EMAIL_HOST_USER = #e-MAIL ID
EMAIL_HOST_PASSWORD = #PASSWORD
My system had not a DNS configured and I resolved this issue configuring a google DNS or an openDNS.

Send an e-mail using Python 3

I'm working on a guessing game where there are tons of characters and it gives you hints for you to guess the character. There is also going to be a thing where you can submit a character. I want to do it so that it will send me an e-mail if somebody submits a character, but it's not working. Here's my code:
import smtplib
FROM = 'Guess#Character.com'
TO = ["mikpe120#gmail.com"] # must be a list
SUBJECT = "Somebody submitted a character!"
TEXT = "Name: " + name + "NameType: " + nameType
# Prepare actual message
message = """\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
# Send the mail
server = smtplib.SMTP('myserver')
server.sendmail(FROM, TO, message)
server.quit()
And here is the error that I got:
Traceback (most recent call last):
File "C:/Users/Mikolaj Perzyna/Desktop/addCharacter.py", line 106, in <module>
server = smtplib.SMTP('myserver')
File "C:\Users\Mikolaj Perzyna\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "C:\Users\Mikolaj Perzyna\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 335, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\Mikolaj Perzyna\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 306, in _get_socket
self.source_address)
File "C:\Users\Mikolaj Perzyna\AppData\Local\Programs\Python\Python36\lib\socket.py", line 704, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Users\Mikolaj Perzyna\AppData\Local\Programs\Python\Python36\lib\socket.py", line 743, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
I'm sorry if I'm formatting this post wrong, which I probably am, but this is my first question on the site.
getaddrinfo failed means that the socket connection smptlib.SMTP uses failed to resolve the hostname of the server. Try a different SMTP server or a different IP/hostname.
This line is almost certainly wrong.
server = smtplib.SMTP('myserver')
Is your SMTP server really called myserver? I'd expect to see something like mail.mydomain.com there.

Python Mailing timed out error

I cannot send an email from python. Probably there is a security issue. Can somebody give me a hint?
This is the code:
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
#Next, log in to the server
server.login("***#gmail.com", "PASSWORD")
#Send the mail
msg = "Hello!"
# The /n separates the message from the headers
server.sendmail("***#gmail.com", "***#gmail.com", msg)
server.quit()
This is the error I get:
server = smtplib.SMTP('smtp.gmail.com', 587)
File "D:\Tools\Lib\smtplib.py", line 244, in __init__
(code, msg) = self.connect(host, port)
File "D:\Tools\Lib\smtplib.py", line 310, in connect
raise socket.error, msg
error: (10060, 'Operation timed out')
you should use smtplib.SMTP_SSL() because port 587 expects ssl(tls) from the get go.
https://docs.python.org/2/library/smtplib.html#smtplib.SMTP_SSL
check with gmail sometimes gmail blocks the signing from apps

Categories