Django注册SMTPServerDisconnected错误

时间:2012-09-21 05:13:12

标签: django login django-registration

我使用django-registration来管理用户注册,登录等项目。但是当我注册一个帐户时,我遇到了这个问题:

SMTPServerDisconnected

为了记录,我确实在settings.py中配置了与电子邮件相关的设置:

EMAIL_HOST = 'smtp.gmail.com'                                                                                                                                           
EMAIL_PORT = 465
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'myemail@gmail.com'
EMAIL_HOST_PASSWORD = 'mypassword'

此外,我还拥有所需的所有模板。所以这个问题与此没有任何关系。

更多错误说明:

点击发送激活邮件后,它需要很长时间才能最终发出错误。

这是追溯(未完成):

SMTPServerDisconnected at /accounts/register/

Connection unexpectedly closed

last trace back:

/usr/lib/python2.7/smtplib.py in getreply

                    line = self.file.readline()

                except socket.error as e:

                    self.close()

                    raise SMTPServerDisconnected("Connection unexpectedly closed: "

                                                 + str(e))

                if line == '':

                    self.close()

                    raise SMTPServerDisconnected("Connection unexpectedly closed")

    ...

                if self.debuglevel > 0:

                    print>>stderr, 'reply:', repr(line)

                resp.append(line[4:].strip())

                code = line[:3]

                # Check that the error code is syntactically correct.

                # Don't attempt to read a continuation line if it is broken.

感谢您的任何建议。

5 个答案:

答案 0 :(得分:6)

我认为至少有一个设置不正确。 TLS [1]

需要端口587

EMAIL_PORT = 587

[1] http://support.google.com/mail/bin/answer.py?hl=en&answer=13287

答案 1 :(得分:2)

最后我想出来了。我在这里发布我的解决方案以防其他人被困在这里。问题是Gmail端口。使用587就可以了。

我在这里看到了:https://code.djangoproject.com/ticket/9575

我想知道为什么有些人可以使用465而有些人则不能。我花了几个小时才发现。希望你的家伙不要!

编辑:有关端口使用的更多讨论,请参阅h ere

To use port 465, you need to call smtplib.SMTP_SSL(). Currently, it looks like Django only uses smtplib.SMTP().

似乎587是正确的选择。

答案 2 :(得分:1)

同时,您可能需要查看https://github.com/bancek/django-smtp-ssl

答案 3 :(得分:0)

如果您对上述答案没有任何好运,您可能需要检查是否在settings.py中设置了DEFAULT_FROM_EMAIL

    # project/settings.py
    EMAIL_HOST = 'smtp.gmail.com'                                                                                                                                           
    EMAIL_PORT = 465
    EMAIL_USE_TLS = True
    EMAIL_HOST_USER = 'myemail@gmail.com'
    EMAIL_HOST_PASSWORD = 'mypassword'
    DEFAULT_FROM_EMAIL = 'myemail@gmail.com'

答案 4 :(得分:0)

Django Gmail Confi#settings.py
EMAIL_BACKEND ='django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_SSL = True    # use port 465
EMAIL_USE_TLS = False    # use port 587
EMAIL_PORT = 465 # OR 587
EMAIL_HOST_USER =  os.environ.get('USER_EMAIL')
EMAIL_HOST_PASSWORD =os.environ.get('USER_EMAIL_PASS')  
helper.py(函数)
from django.core.mail import send_mail
def send(sub=None,message=None,recipient=[]):
    # def send_mail(
                    # subject,
                    # message,
                    # from_email,
                    # recipient_list,
                    # fail_silently=False,
                    # auth_user=None,
                    # auth_password=None,
                    # connection=None,
                    # html_message=None):

    send_mail(subject='contact mail'
                       ,message='Test Mail'
                       ,from_email=EMAIL_HOST_USER
                       ,recipient_list=['mail_list']
                       ,fail_silently=False)
    return True