2014-09-01 181 views
1

我想通過Django發送電子郵件作爲django-userena的一部分,但我無法收到電子郵件發送。在我的設置,我有:Django電子郵件不發送

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' 
EMAIL_USE_TLS = True 
EMAIL_HOST = 'smtp.gmail.com' 
EMAIL_PORT = 587 
EMAIL_HOST_USER = '[email protected]' 
EMAIL_HOST_PASSWORD = 'mypassword' 

我試圖從Django的控制檯發送一封電子郵件,其中:

from django.core.mail import EmailMessage 
email = EmailMessage('Mail Test', 'This is a test', to=['[email protected]']) 
email.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

檢查此問題:http://stackoverflow.com/questions/1950442/connection-timeout-issue-sending-email-in-django – ruddra 2014-09-02 06:22:23

回答

4

我有這個相同的問題。我正在使用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 protected]' 
EMAIL_HOST_PASSWORD = 'YOUR_PASSWORD' 

當然,如果你使用Django 1.7,那麼你可以再補充EMAIL_USE_SSL = Truesettings.py和使用默認後端。

+0

謝謝。這個排序我出=) – tr33hous 2015-07-02 11:20:14

+0

這是像這些時刻,當我希望我可以upvote兩次! – 2016-12-02 17:07:13