如何修复Django的send_mail无法访问网络? [英] How to fix Network is unreachable with django's send_mail?

查看:56
本文介绍了如何修复Django的send_mail无法访问网络?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从手机系绳时,无法通过3g数据连接使用django的 send_mail()发送邮件.可能需要一段时间,并且失败,并显示 Network is unreachable error

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

另一方面,通过 4g连接发送邮件成功.我对网络知识不强,因此我无法理解原因.编辑通过本地主机执行此操作.

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.

推荐答案

由于您仅在进行本地开发时才使用手机,因此可以选择不实际发送电子邮件,而只是将其打印到控制台.

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'

此处中有记载.

这篇关于如何修复Django的send_mail无法访问网络?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆