SMTP send: "getaddrinfo failed" - python

import smtplib
def sendEmail(receiver,subject,message):
server = smtplib.SMTP('smtp.gamil.com',587)
server.ehlo()
server.starttls()
server.login("botsinhala#gmail.com",password)
server.sendmail("botsinhala#gmail.com",receiver,message)
print("succesful")
sendEmail("gadjetrathnayake#gmail.com","testing bot","Hi, This is a testing message i'm sending to you.")
above code is generating the following error. My OS is windows 10.
Traceback (most recent call last):
File "D:/MY CHANNEL/python tricks/Email bot 1/tttttttttttttttttttttttt.py", line 17, in <module>
sendEmail("andrewhawkins#gmail.com","testing bot","Hi, This is a testing message i'm sending to you.")
File "D:/MY CHANNEL/python tricks/Email bot 1/tttttttttttttttttttttttt.py", line 5, in sendEmail
server = smtplib.SMTP('smtp.gamil.com',587)
File "C:\Users\Dell Pc\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "C:\Users\Dell Pc\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\Dell Pc\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py", line 307, in _get_socket
self.source_address)
File "C:\Users\Dell Pc\AppData\Local\Programs\Python\Python37-32\lib\socket.py", line 707, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Users\Dell Pc\AppData\Local\Programs\Python\Python37-32\lib\socket.py", line 748, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
How can I fix this error

Replace smtp.gamil.com by smtp.gmail.com.
There are no A/AAAA records for smtp.gamil.com (2021-01-07T13:08+00:00).

Related

socket.gaierror: [Errno 11001] getaddrinfo failed when sending gmail email via Python

I'm on Windows and using Python 3.9.10 and I have trouble sending an email via google smtp.
My code is :
import smtplib
from email.mime.text import MIMEText
def send_email(host, port, subject, msg, sender, recipients, password):
msg = MIMEText(msg)
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = ', '.join(recipients)
smtp_server = smtplib.SMTP_SSL(host, port)
smtp_server.login(sender, password)
smtp_server.sendmail(sender, recipients, msg.as_string())
smtp_server.quit()
def main():
host = "smtp.gmail.com"
port = 465
user = <my email>
pwd = <google app password>
subject = "Test email"
msg = "Hello world"
sender = user
recipients = [user]
send_email(host, port, subject, msg, sender, recipients, pwd)
if __name__ == '__main__':
main()
What I don't understand is I keep getting the error socket.gaierror: [Errno 11001] getaddrinfo failed:
Traceback (most recent call last):
File "%project_location%\send_email_google.py", line 37, in <module>
main()
File "%project_location%\send_email_google.py", line 33, in main
send_email(host, port, subject, msg, sender, recipients, pwd)
File "%project_location%\send_email_google.py", line 13, in send_email
smtp_server = smtplib.SMTP_SSL(host, port)
File "%Python%\lib\smtplib.py", line 1050, in __init__
SMTP.__init__(self, host, port, local_hostname, timeout,
File "%Python%\lib\smtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
File "%Python%\lib\smtplib.py", line 341, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "%Python%\lib\smtplib.py", line 1056, in _get_socket
new_socket = super()._get_socket(host, port, timeout)
File "%Python%\lib\smtplib.py", line 312, in _get_socket
return socket.create_connection((host, port), timeout,
File "%Python%\lib\socket.py", line 823, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "%Python%\lib\socket.py", line 954, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
Process finished with exit code 1
I'm not using a VPN, proxies or anything.
I have checked, I have the correct host and the correct port for Gmail. I'm using the google app password thing. I just don't get why it won't work.
Thanks!
I fixed it
I had a typo and my host was not "smtp.gmail.com" but was "'smtp.gmail.com'"

Script to Send Automated Emails via Outlook

I am attempting to set up a script that will fire off a series of emails automatically.
Below I have the code I am using to test this process.
import smtplib, ssl
username = 'me#company.com' # input('Enter username: ')
password = 'mypassword' # input('Enter password: ')
port = 443
smtp_server = "outlook.office365.com"
sender_email = username
receiver_email = "me#company.com"
message = "Hi there! This message is sent from Python."
context = ssl.create_default_context()
# s = smtplib.SMTP_SSL('outlook.office365.com')
# s.set_debuglevel(1)
with smtplib.SMTP(smtp_server, port) as server:
server.starttls(context=context)
server.login(username, password)
server.sendmail(sender_email, receiver_email, message)
When I run it, I get the following error:
Traceback (most recent call last):
File "/Users/gx8k/repos/namespace_auditer/./send_mail.py", line 15, in <module>
s = smtplib.SMTP_SSL('outlook.office365.com')
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 1050, in __init__
SMTP.__init__(self, host, port, local_hostname, timeout,
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 341, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 1056, in _get_socket
new_socket = super()._get_socket(host, port, timeout)
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 312, in _get_socket
return socket.create_connection((host, port), timeout,
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 844, in create_connection
raise err
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 832, in create_connection
sock.connect(sa)
TimeoutError: [Errno 60] Operation timed out
I want to send the email from Outlook specifically.
Checking the 'Accounts' settings I see my server is running on port 443 and has a domain of https://outlook.office365.com/subdomain/Exchange.asmx
I'm only using the outlook.office365.com portion of it since that's all that is shown in most examples I see. However I have tried using the full domain as well as just https://outlook.office365.com/. Nothing seems to work.
I have been using the info found here to guide me.
Originally I was using the domain smtp.office365.com but changed it after finding outlook.office365.com in my accounts settings. It fails with the same error anyway.
Traceback (most recent call last):
File "/Users/gx8k/repos/namespace_auditer/./send_mail.py", line 15, in <module>
s = smtplib.SMTP_SSL('smtp.office365.com')
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 1050, in __init__
SMTP.__init__(self, host, port, local_hostname, timeout,
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 341, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 1056, in _get_socket
new_socket = super()._get_socket(host, port, timeout)
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 312, in _get_socket
return socket.create_connection((host, port), timeout,
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 844, in create_connection
raise err
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 832, in create_connection
sock.connect(sa)
TimeoutError: [Errno 60] Operation timed out
I also tried with port 587 rather than 443. (443 is what I find in my outlook settings) - same error either way.
Update: I think the answer to my question is that mac's don't use SMTP...now I have to figure out how to do this in IMAP.
What protocol does Outlook for Mac use?
By default, Outlook is set to IMAP protocol. If you choose to set your account on IMAP, you may use the following settings then click Add Account.
Additional Update:
I ended up scratching this approach altogether. I ended up using an internal API service to send notifications out to all relevant parties.
For Office 365 accounts, the SMTP server name is smtp.office365.com, post 587, TLS on.

Python Email Server not responding

Input
import smtplib
conn = smtplib.SMTP('imap.gmail.com',587)
conn.ehlo()
conn.starttls()
conn.login('loginmail#gmail.com', 'password')
conn.sendmail('sender1#gmail.com','sender2#gmail.com','Subject: What you like? \n\n Reply Reply Reply')
conn.quit()
Output and Error
Traceback (most recent call last):
File "E:\python\openCV\em.py", line 4, in <module>
conn = smtplib.SMTP('imap.gmail.com',587)
File "C:\Users\loges\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 251, in _init_
(code, msg) = self.connect(host, port)
File "C:\Users\loges\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\loges\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 307, in _get_socket
self.source_address)
File "C:\Users\loges\AppData\Local\Programs\Python\Python36\lib\socket.py", line 724, in create_connection
raise err
File "C:\Users\loges\AppData\Local\Programs\Python\Python36\lib\socket.py", line 713, in create_connection
sock.connect(sa)
TimeoautError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
Please help me to solve this issue. It looks like server issue. I have included the login mail and password, 2 sender emails and a subject. I want to send a zip file. The program is showing error and I have provided the error above
You are connecting to the wrong server. Should be:
conn = smtplib.SMTP('smtp.gmail.com', 587)

socket.gaierror: [Errno 11004] getaddrinfo failed Python

Im currently having a problem with a Python program I made. The program is supposed to connect to google translate using the http.client module. My program works if im just connection to google.com, but it gives me an error when I try to connect to translate.google.com.
import urllib.request,re,http.client
while True:
print('')
question = input(">: ").replace(" ","%20").replace("?",'%3F')
x = "/#auto/es/%s"%(question)
conn = http.client.HTTPSConnection("www.translate.google.com",80)
http.client.HTTPConnection.connect(conn)
conn.request("GET",x)
response = conn.getresponse()
data = str(response.read())
conn.close()
print("Done")
Traceback (most recent call last):
File "P:\CS5\translater.py", line 9, in <module>
http.client.HTTPConnection.connect(conn)
File "C:\Program Files\Python35-32\lib\http\client.py", line 826, in connect
(self.host,self.port), self.timeout, self.source_address)
File "C:\Program Files\Python35-32\lib\socket.py", line 693, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Program Files\Python35-32\lib\socket.py", line 732, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11004] getaddrinfo failed

gaierror while using send_mail in django behind proxy

I was trying to send an email from my Django app using gmail. I added these email settings to settings.py
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'myemail#gmail.com'
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
But it throws this name resolution error:
Traceback (most recent call last):
File "/usr/lib/python3.4/code.py", line 90, in runcode
exec(code, self.locals)
File "<console>", line 1, in <module>
File "/usr/local/lib/python3.4/dist-packages/Django-1.8.7-py3.4.egg/django/core/mail/__init__.py", line 62, in send_mail
return mail.send()
File "/usr/local/lib/python3.4/dist-packages/Django-1.8.7-py3.4.egg/django/core/mail/message.py", line 303, in send
return self.get_connection(fail_silently).send_messages([self])
File "/usr/local/lib/python3.4/dist-packages/Django-1.8.7-py3.4.egg/django/core/mail/backends/smtp.py", line 100, in send_messages
new_conn_created = self.open()
File "/usr/local/lib/python3.4/dist-packages/Django-1.8.7-py3.4.egg/django/core/mail/backends/smtp.py", line 58, in open
self.connection = connection_class(self.host, self.port, **connection_params)
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
self.source_address)
File "/usr/lib/python3.4/socket.py", line 494, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/usr/lib/python3.4/socket.py", line 533, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
Note: I am working behind a proxy.
In addition, I tried to connect to google.com through socket:
import socket
socket.gethostbyname('www.google.com')
It throws:
[Errno 11001] getaddrinfo failed
enable pop3 in gmail.com
create specific password for django application
(http://support.google.com/accounts/bin/answer.py?hl=en&answer=185833)
Solutions here

Categories