我正在尝试通过Django发送电子邮件作为django-userena的一部分,但我根本无法收到电子邮件发送。在我的设置中,我有:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'myuser@gmail.com'
EMAIL_HOST_PASSWORD = 'mypassword'
我尝试使用以下命令从Django控制台发送电子邮件:
from django.core.mail import EmailMessage
email = EmailMessage('Mail Test', 'This is a test', to=['otheruser@gmail.com'])
email.send()
它挂在send命令上,实际上并不发送电子邮件。如果我停止命令,我会得到这个追溯:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/myuser/Copy/Projects/Programming/myproject/venv/local/lib/python2.7/site-packages/django/core/mail/message.py", line 274, in send
return self.get_connection(fail_silently).send_messages([self])
File "/home/myuser/Copy/Projects/Programming/myproject/venv/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 87, in send_messages
new_conn_created = self.open()
File "/home/myuser/Copy/Projects/Programming/myproject/venv/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 48, in open
local_hostname=DNS_NAME.get_fqdn())
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 312, in connect
(code, msg) = self.getreply()
File "/usr/lib/python2.7/smtplib.py", line 356, in getreply
line = self.file.readline()
File "/usr/lib/python2.7/socket.py", line 447, in readline
data = self._sock.recv(self._rbufsize)
有关为何没有这样做的任何帮助?
答案 0 :(得分:5)
我有同样的问题。我正在使用Django 1.6。事实证明我需要使用SSL通过Gmail发送电子邮件。所以我使用了这个方便的包:https://github.com/bancek/django-smtp-ssl
$ pip install django-smtp-ssl
然后settings.py
应该有:
EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 465
EMAIL_HOST_USER = 'email@gmail.com'
EMAIL_HOST_PASSWORD = 'YOUR_PASSWORD'
当然,如果您使用的是Django 1.7,那么您只需将EMAIL_USE_SSL = True
添加到settings.py
并使用默认的后端。