Reputation: 693
I have tried to attached a file to the mail using python. Code:
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from smtplib import SMTPException
def send_Email():
file1="abc.txt"
message = "Test mail"
msg = MIMEMultipart()
msg.attach(MIMEText(file(file1).read()))
try:
smtpObj = smtplib.SMTP('smtp server name',port)
smtpObj.sendmail(sender, EmailId, message, msg.as_string() )
print "Successfully sent email"
except SMTPException:
print "Error: unable to send email"
Bt I have get the error: socket.gaierror: [Errno 11001] getaddrinfo failed
full error message:
File "C:\Python27\lib\smtplib.py", line 249, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python27\lib\smtplib.py", line 309, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python27\lib\smtplib.py", line 284, in _get_socket
return socket.create_connection((port, host), timeout)
File "C:\Python27\lib\socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11001] getaddrinfo failed
Upvotes: 6
Views: 99867
Reputation: 91
I got this error when I tried using flask-mail I just had to resend the message and it worked perfectly well. I don't know why I got the error the first time perhaps it might be a bug in the library...
Upvotes: 1
Reputation: 41
You need to activate IMAP/SMTP service active for your host mail.
Upvotes: -2
Reputation: 21
you might did a little mistake in settings.py file.. check your code one more time in your settings file settings.py:
EMAIL_BACKEND='django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'your_email'
EMAIL_HOST_PASSWORD = 'your_password'
EMAIL_PORT = 587
EMAIL_USE_TLS=True
Upvotes: 0
Reputation: 913
The below answer may be quite irrelevant to the question. But,some users may have a different scenario.
If a server can be reached only through VPN and if we try to reach it with VPN disconnected, this error : "gaierror: [Errno 11001] getaddrinfo failed" crops up.
Connect to VPN and then executing the code should work good.
Upvotes: 2
Reputation: 1086
There seems to be a bug in urllib3 version 1.25.9 package. This produced "socket.gaierror: [Errno 11001] getaddrinfo failed" error for me (working from behind an authenticated proxy server). Downgrading to urllib3 version 1.25.8 solved the problem.
Upvotes: 0
Reputation: 47
I prefer u guys to run the file as administrator for eg open cmd as administrator then type cd C:\into ur .py file path and then type python filename.py
it worked for me. good luck
Upvotes: -2
Reputation: 51
In my case was a host problem. Using debug mode, I spotted that in (host, port, 0, SOCK_STREAM) I got host=local and it should be host=localhost. In the run.py I defined localhost and the file hosts (c:\windows\system32\drivers\etc\hosts) was defined local. They have to be equal, otherwise you get the socket.gaieeror.
Upvotes: 0
Reputation: 1
You need to login using your credential. Try:
smtpObj = smtplib.SMTP('smtp server name',port)
smtpObj .starttls()
smtpObj .login(email, password)
smtpObj.sendmail(sender, EmailId, message, msg.as_string() )
print "Successfully sent email"
Upvotes: -2
Reputation: 2900
I know for sure that gaierror comes up when you are working from behind proxy.
Upvotes: 12
Reputation: 28370
The problem is that the DNS lookup for 'smtp server name' is failing - if this is your exact code then you can see why - if not and you have the valid qualified name for the SMTP server then you may have issues with the firewall/internet connection, etc., also port has to be set to a valid value to match your servers SMTP configuration, (usually port 25 but not absolutely always).
Upvotes: 6