首页 > 解决方案 > 如何配置 Cookiecutter Django 以使用 Gmail SMTP

问题描述

谁能帮我配置我的 django cookiecutter 生产设置以使用 Gmail SMTP。

我已经使用 docker 部署了我的应用程序。Cookiecutter 让您可以将您的应用程序配置为任何邮件提供商。我选择了 Mailgun,但是我没有经过我的域提供商验证的电子邮件地址。所以,我无法将任何用户注册到我的应用程序,因为(https://cookiecutter-django.readthedocs.io/en/latest/deployment-with-docker.html#configuring-the-stack)这里说你可以'吨:)

我试图将默认电子邮件验证设置从“强制”更改为“无”。但是,它仍然抛出了 500。在下面,我添加了 all-auth 设置。我必须决定要么购买一个电子邮件地址,要么配置我的应用程序以使用 Gmail Smtp,或者摆脱这个电子邮件验证过程。

设置/base.py

ACCOUNT_ALLOW_REGISTRATION = env.bool("DJANGO_ACCOUNT_ALLOW_REGISTRATION", True)
# https://django-allauth.readthedocs.io/en/latest/configuration.html
ACCOUNT_AUTHENTICATION_METHOD = "username"
# https://django-allauth.readthedocs.io/en/latest/configuration.html
ACCOUNT_EMAIL_REQUIRED = True
# https://django-allauth.readthedocs.io/en/latest/configuration.html
ACCOUNT_EMAIL_VERIFICATION = "none"



# https://docs.djangoproject.com/en/dev/ref/settings/#email-backend
EMAIL_BACKEND = env(
    "DJANGO_EMAIL_BACKEND", default="django.core.mail.backends.smtp.EmailBackend"
)

任何摆脱这 500 个的方法都很棒。

标签: python-3.xdjangocookiecutter-django

解决方案


您不配置电子邮件主机和来自发件人的电子邮件。尝试这个

EMAIL_BACKEND = env(
    "DJANGO_EMAIL_BACKEND", default="django.core.mail.backends.smtp.EmailBackend"
)
EMAIL_HOST = "smtp.gmail.com"
EMAIL_PORT = 587
EMAIL_HOST_USER = "your_email@gmail.com"
EMAIL_HOST_PASSWORD = "email_password"
DEFAULT_FROM_EMAIL = "email_of_sender@gmail.com" # same with EMAIL_HOST_USER
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
# https://docs.djangoproject.com/en/dev/ref/settings/#email-timeout
EMAIL_TIMEOUT = None

推荐阅读