How to fix Network is unreachable with django's send_mail?

╄→尐↘猪︶ㄣ 提交于 2019-12-11 10:55:09

问题


Failed to send mail with django's send_mail() over a 3g data connection as I tether from my phone. It takes a while and fails with a Network is unreachable error,

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/root/.virtualenvs/local_env/local/lib/python2.7/site-packages/django/core/mail/__init__.py", line 62, in send_mail
    return mail.send()
  File "/root/.virtualenvs/local_env/local/lib/python2.7/site-packages/django/core/mail/message.py", line 283, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/root/.virtualenvs/local_env/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 92, in send_messages
    new_conn_created = self.open()
  File "/root/.virtualenvs/local_env/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 50, in open
    self.connection = connection_class(self.host, self.port, **connection_params)
  File "/usr/lib/python2.7/smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python2.7/smtplib.py", line 311, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/lib/python2.7/smtplib.py", line 286, in _get_socket
    return socket.create_connection((host, port), timeout)
  File "/usr/lib/python2.7/socket.py", line 571, in create_connection
    raise err
error: [Errno 101] Network is unreachable

On the other, sending mail on a 4g connection is successful. I have a poor knowledge with networks and thus i cannot understand the cause. EDIT Doing this via localhost.


回答1:


Since you're only using your phone when doing local development, maybe it is an option to not actually send the email, but just print it to the console.

To do that, put this in your settings:

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

It is documented here.



来源:https://stackoverflow.com/questions/32478431/how-to-fix-network-is-unreachable-with-djangos-send-mail

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!