5

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 = 'example@hotmail.fr'  
smtppass = 'mypassword'  

RECIPIENTS = 'mailto@gmail.com'
SENDER = 'example@hotmail.fr'
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.

Stephen
  • 1,072
  • 1
  • 19
  • 33

2 Answers2

2

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.

scriptmonster
  • 2,741
  • 21
  • 29
  • Your DNS comment made me explore a little further and found it to be a DHCP issue! I restarted the server fixing the DHCP issue and then assigned a static IP back to the server and it seems to be working now. I'll reward you the bounty as it lead me. Thank you! – Stephen Jul 30 '18 at 11:35
  • I'm glad to hear! Thanks. – scriptmonster Jul 31 '18 at 10:01
1

I focused my answer on the strange fact that you can get around the problem using a SOCKS5 proxy. (I believe you. There was no time to ask you for details.) You verified that your example solution by SOCKS5 works for you. Django uses the same smtplib and you can easily wrap it the same way by this code added to wsgi.py.

import smtplib
import socks    # it is the package SocksiPy or PySocks

socks.setdefaultproxy(socks.SOCKS5, '192.168.1.5', 8080)
socks.wrapmodule(smtplib)

Http(s) proxy (paragraph 5)) is not related because it does not affect SMTP and other protocols except http(s) because "SOCKS operates at a lower level than HTTP proxying".

hynekcer
  • 14,942
  • 6
  • 61
  • 99