SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted in Django production?

左心房为你撑大大i 提交于 2021-01-04 07:23:07

问题


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.

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

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

My views.py view handling the mail-

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

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

回答1:


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.




回答2:


The solution that worked for me is posted here.

I used Flask but the error was exactly the same. I made sure that my credentials were correct and the environment variables were not enclosed in quotes like Eddy Ogola suggested.

The short answer from this post is to click this link while logged into the google account associated with the gmail you're sending from.

Google doesn't trust most scripts to login to your account, so they put the onus on the owner of the gmail to give "less secure apps" permission to access your gmail.



来源:https://stackoverflow.com/questions/58283561/smtpauthenticationerror-535-b5-7-8-username-and-password-not-accepted-in-dja

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!