5

我为我的项目使用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.

感谢您的任何建议。

4

5 回答 5

8

我认为至少有一个设置不正确。TLS [1] 需要端口 587

EMAIL_PORT = 587

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

于 2012-09-21T08:45:50.900 回答
2

最后我想通了。我在这里发布我的解决方案,以防其他人被困在这里。问题是 Gmail 端口。使用587,它工作。

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

我想知道为什么有些人可以使用 465 而有些人不能。我花了几个小时才找到答案。希望你们不会!

编辑:有关使用端口的更多讨论,请参见此处。

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

看来587是正确的选择。

于 2012-09-21T08:48:52.640 回答
1

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

于 2013-06-07T19:42:25.017 回答
1
Django Gmail 配置文件#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
于 2020-11-07T06:17:24.057 回答
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'
于 2016-02-07T21:02:40.217 回答