【问题标题】:Email could not sent using Django and Python无法使用 Django 和 Python 发送电子邮件
【发布时间】:2018-01-20 18:27:34
【问题描述】:

我已经使用 Django 实现了重置密码功能。在这里,我将重置密码链接发送到已注册的电子邮件,并且没有邮件进入收件箱。我的代码如下。

settings.py:

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'user474@gmail.com'
EMAIL_HOST_PASSWORD = '*********'
EMAIL_USE_TLS = False
DEFAULT_FROM_EMAIL = 'user474@gmail.com'

注册/password_reset_form.html:

{% extends 'base.html' %}

{% block content %}
  <h3>Forgot password</h3>
  <form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Submit</button>
  </form>
{% endblock %}

注册/password_reset_email.html:

    {% autoescape off %}
    To initiate the password reset process for your {{ user.get_username }} TestSite Account,
    click the link below:

    {{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}

If clicking the link above doesn't work, please copy and paste the URL in a new browser
window instead.

Sincerely,
The Nuclear Team
{% endautoescape %}

我的密码重置表单如下所示。

这里我无法将邮件发送到给定的电子邮件 ID。

【问题讨论】:

  • 您能否向我们展示您对密码重置的看法?
  • 阅读文档即可。你的第一行代码是错误的。 docs.djangoproject.com/en/1.11/topics/email/#console-backend
  • 它不发送邮件,因为邮件打印到控制台。就像你配置的那样。
  • @Wencakisa :我附上了表格的屏幕截图。
  • @satya view 表示您的重置操作所在的 views.py 代码

标签: python django email


【解决方案1】:

需要更换:

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
#                                        ^^^^^^^^

BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
#                                   ^^^^^^

更多信息在这里:email-backend

【讨论】:

  • 我做到了,但这个SMTP AUTH extension not supported by server. 仍然出现错误。
  • 您输入的邮箱是否正确?重置时重定向完成了吗?
  • 成功了,我刚改了EMAIL_USE_TLS = True。谢谢
【解决方案2】:

你的观点应该是这样的:

from django.core.mail import EmailMessage

email = EmailMessage('title', 'body', to=[email])
email.send()

您还需要在您的 gmail 帐户上解锁验证码: https://accounts.google.com/DisplayUnlockCaptcha

【讨论】:

    猜你喜欢
    • 2013-06-07
    • 2016-06-21
    • 1970-01-01
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 2015-09-27
    相关资源
    最近更新 更多