Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Emails through Django - WinError 10060 A connection attempt failed and GetAddrInfo Error

I've been running an instance of Django on Windows R2 2012 for over a year and I've come to a road block. Yesterday something happened, I don't know what it could be. The same two errors keep popping up at different times though when trying to send an email:

[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

and

socket.gaierror: [Errno 11001] getaddrinfo failed

Users are able to connect to the IP address of the server and the port Django is running on 192.168.1.5:8000, but they cannot send emails anymore. Though a percentage do go through as described here, but very few.

Things I've tried

1) This solution

import socket
socket.getaddrinfo('localhost', 8000)

Since I'm doing python manage.py runserver 192.168.1.5:8000, I added that IP and nothing.

2) I went into the Firewall settings and made sure that the ports were all good. The SMTP one that is declared in the setting.py file in my Django project and 25. All of them, inbound and out.

3) I tried sending things on my local machine and it does work. I used other programs that do not use Django to send emails and they do process on all other machines except the Server. So I know it's not my email server.

4) I changed the email config to use my Gmail account and it does process on all other machines except for the server. So it has to be the environment.

5) Editing http_proxy environment variables

The problem, in my case, was that some install at some point defined an environment variable http_proxy on my machine when I had no proxy. Removing the http_proxy environment variable fixed the problem.

As described here

and in my Django project in the wsgi.y file:

os.environ['http_proxy'] = "http://192.168.1.5:8080"
os.environ['https_proxy'] = "http://192.168.1.5:8080"

6) Given this answer here (can someone please explain how I would do it to a django email function), I've also tried this method of wrapping it from solutions here

import smtplib
import socks

#socks.setdefaultproxy(TYPE, ADDR, PORT)
socks.setdefaultproxy(socks.SOCKS5, '192.168.1.5', 8080)
socks.wrapmodule(smtplib)

smtpserver = 'smtp.live.com'
AUTHREQUIRED = 1 
smtpuser = '[email protected]'  
smtppass = 'mypassword'  

RECIPIENTS = '[email protected]'
SENDER = '[email protected]'
mssg = "test message"
s = mssg   

server = smtplib.SMTP(smtpserver,587)
server.ehlo()
server.starttls() 
server.ehlo()
server.login(smtpuser,smtppass)
server.set_debuglevel(1)
server.sendmail(SENDER, [RECIPIENTS], s)
server.quit()

Though I wouldn't like to use such a method as I'd prefer using Django's built in email service.

like image 882
Stephen Avatar asked Oct 17 '22 15:10

Stephen


1 Answers

Since you have not changed the code and errors you shared shows that it's a network related problem.

It's most probably a DNS issue. In your settings.py you have specified the EMAIL_HOST, which is i believe a hostname. You need to check you server's DNS server.

You are mentioning about checking your firewall settings but what you are doing wrong is not checking the actual connection. To address the problem you can use couple of command line utilities like telnet or nslookup. You can check if you can resolve a hostname:

nslookup smptp.mail_host.com

This command will fail most probably.

I would like to point what you did wrong in your steps:

1) You have tried to get your services getaddrinfo in which you needed to put your smtp servers hostname, which would result with the same error. Sockets are the very primitive part of the connection on the application layer, you don't really need to dig in to that.

2) Checking firewall settings is OK.

3) This is a good step which shows that there is a problem with your servers network connection.

4) That is another evidence :)

5) You have got it wrong, if you have a proxy server on your network to connect external networks, than you use this settings. But you have configured it wrong. You should not set your projects url as proxy server.

6) This is another deep level coding. You should not use such low level script, which will cause you numerious problems, which would have been handled in high level modules.

like image 112
scriptmonster Avatar answered Oct 20 '22 00:10

scriptmonster