SMTPAuthenticationError:(535,b'5.7.8在Django生产中不接受用户名和密码吗? [英] SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted in Django production?

查看:78
本文介绍了SMTPAuthenticationError:(535,b'5.7.8在Django生产中不接受用户名和密码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Heroku上部署了Django应用.在其中一部分中,我将使用SMTP Gmail设置向用户发送电子邮件.当我在本地运行项目但未在Heroku上已部署的项目上运行时,电子邮件已成功发送.

I have a Django app deployed on Heroku. In one of the sections I'm sending email to the user using SMTP Gmail settings. The emails are sent successfully when I run project locally but not on my deployed project on Heroku.

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

I've seen many of the other answers on Stackoverflow but none of them resolves my issue. I've enabled the 2FA on my Google account and generated an APP password and using that password in my settings file. Turning on allow_less_secure_app option isn't suggested by other developers

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

My settings.py file email settings-

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 视图处理邮件-

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日志中遇到的错误是-

The error I'm getting in Heroku logs is-

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')

推荐答案

我遇到了同样的问题.我通过重置os环境变量解决了该问题,不应将这些值括在引号中.例如,MAIL_USERNAME = blah @ gmail.com.应该可以.

I had the same issue. I solved it with resetting the os environment variables, the values shouldn't be enclosed in quotes. For example, MAIL_USERNAME=blah@gmail.com. It should work.

这篇关于SMTPAuthenticationError:(535,b'5.7.8在Django生产中不接受用户名和密码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆