python - SMTPAuthenticationError : (535, b'5.7.8 Django 生产中不接受用户名和密码?

标签 python django email smtp

我在 Heroku 上部署了一个 Django 应用程序。在其中一节中,我使用 SMTP Gmail 设置向用户发送电子邮件。当我在本地运行项目时,电子邮件发送成功,但在 Heroku 上部署的项目上却发送失败。

我在 Stackoverflow 上看到了许多其他答案,但没有一个能解决我的问题。 我已在我的 Google 帐户上启用了 2FA,并生成了应用密码并在我的设置文件中使用该密码。其他开发者不建议打开allow_less_secure_app选项

我的settings.py文件电子邮件设置-

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = os.environ.get('EMAIL_USER2')
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_PASS2')

我的views.py View 处理邮件-

def index(request) 
    if request.method == 'POST':
        form = MyForm(request.POST)
        if form.is_valid():
            message = form.cleaned_data['message']
            email = form.cleaned_data['email']
            subject = "You got a message"
            thoughts = "{} by {}".format(message,email)
            recipients = ['xyz@gmail.com']
            sender = 'abc@gmail.com'
            send_mail(subject, thoughts, sender ,recipients,fail_silently=False)
            return HttpResponse()
    else:
        form = MyForm()
    return render(request,'my_webapp/index.html',{'form':form})

我在 Heroku 日志中遇到的错误是 -

raise SMTPAuthenticationError(code, resp)
2019-10-07T18:22:12.174365+00:00 app[web.1]: smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials w2sm9789664qtc.59 - gsmtp')

最佳答案

对我有用的解决方案已发布 here .

我使用了Flask,但错误是完全相同的。我确保我的凭据正确,并且环境变量没有像 Eddy Ogola 建议的那样用引号引起来。

这篇文章的简短回答是点击 this link登录与您发送邮件的 Gmail 关联的 Google 帐户。

Google 不信任大多数脚本来登录您的帐户,因此他们让 Gmail 所有者有责任授予“安全性较低的应用”访问您 Gmail 的权限。

关于python - SMTPAuthenticationError : (535, b'5.7.8 Django 生产中不接受用户名和密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58283561/

相关文章:

python - 一种通过比较特定值从多维数组中删除重复项的方法

python - Virtualenv 与 Eclipse(伽利略)

php - Laravel 无法使用 gmail 发送电子邮件

python - 如何在 BeautifulSoup 中使用元素的样式定义(例如填充、字体大小等)来抓取元素

Python生成器, 'coroutine'中的非吞咽异常

python - 将默认参数传递给基于类的 View

python - Django:如何根据选择的其他表单字段选项更改具有不同查询集的表单字段选择选项?

cloudflare - 使用 Cloudflare 子域设置 Mailgun

java - 如何使用 javamail 将我网站的联系表单链接到我的邮件

python 2.7 存在于字典中