Django 发送邮件
我知道有20个问题和我的差不多,但我已经尝试了一整天想让Django发送邮件。
我在尝试发送邮件时遇到了这个错误:[Errno 111] Connection refused
。
这是我在视图中创建邮件并尝试发送的地方:
try:
msg = EmailMessage(subject, message, from_email, [receiver])
msg.content_subtype = "html"
msg.send()
我的设置文件如下:
EMAIL_HOST = "localhost"
DEFAULT_FROM_EMAIL = "myemail@gmail.com"
EMAIL_PORT = 25
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
我尝试过用 python -m smtpd -n -c DebuggingServer localhost:1025
来测试发送邮件,成功了,但真正要发送的时候却不行。
当我在命令行中尝试使用send_mail时,我得到了这个错误信息:
>>> from django.core.mail import send_mail
>>> send_mail('Test', 'Test', 'myemail@gmail.com', ['myemail@gmail.com'])
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.6/dist-packages/django/core/mail/__init__.py", line 61, in send_mail
connection=connection).send()
File "/usr/local/lib/python2.6/dist-packages/django/core/mail/message.py", line 251, in send
return self.get_connection(fail_silently).send_messages([self])
File "/usr/local/lib/python2.6/dist-packages/django/core/mail/backends/smtp.py", line 79, in send_messages
new_conn_created = self.open()
File "/usr/local/lib/python2.6/dist-packages/django/core/mail/backends/smtp.py", line 42, in open
local_hostname=DNS_NAME.get_fqdn())
File "/usr/lib/python2.6/smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.6/smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.6/smtplib.py", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "/usr/lib/python2.6/socket.py", line 561, in create_connection
raise error, msg
error: [Errno 111] Connection refused
我感觉自己一直没有进展。任何帮助或建议都非常感谢。谢谢!
另外,如果你想看到其他内容,请随时评论。
8 个回答
17
@mongoose_za 给出了一个很好的答案,不过在 Django 1.4 及以上版本中,语法有点不同。
不要使用:
send_mail('test email', 'hello world', to=['test@email.com'])
而是用:
send_mail('test email', 'hello world', 'your@email.com', ['test@email.com'])
前四个参数是必须的:主题、消息内容、发件人邮箱和收件人列表。
46
首先创建一个应用专用密码
- 访问你的 谷歌账户安全页面。然后点击“两步验证”:
- 在 谷歌账户安全页面 点击
应用密码
:
然后将相应的值添加到 settings.py 文件中:
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'your-username@gmail.com'
EMAIL_HOST_PASSWORD = 'Application spectific password(for eg: smbumqjiurmqrywn)'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
你可以使用命令行来测试它:
python manage.py shell
>>> from django.core.mail import send_mail
>>> send_mail('Test', 'This is a test', 'your@email.com', ['toemail@email.com'],
fail_silently=False)
77
你是在尝试使用 Gmail 账号吗?那你可以试试这个:
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'your-username@gmail.com'
EMAIL_HOST_PASSWORD = 'your-password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
然后如果你用的是 Django 1.4 之前的版本,可以这样测试:
python manage.py shell
>>> from django.core.mail import send_mail
>>> send_mail('test email', 'hello world', to=['test@email.com'])
如果你用的是 Django 1.4,那就用这个:
python manage.py shell
>>> from django.core.mail import send_mail
>>> send_mail('test email', 'hello world', 'your@email.com', ['test@email.com'])
如果你不是在用 Gmail 账号,但还是遇到问题,那就试着把 EMAIL_HOST_USER
和 EMAIL_HOST_PASSWORD
加到你现有的设置里。如果问题依旧,可能是你的网络在限制你,比如操作系统或路由器的防火墙。
感谢 knite 提供的更新语法,给他点个赞吧,还要感谢 pranavk 告诉我 Django 1.4 的语法变化。