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

8

6

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 = ['[email protected]']
            sender = '[email protected]'
            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')
Equalize answered 8/10, 2019 at 9:25 Comment(6)
General advise: Don't use Gmail as a bulk email sending service. You will get blocked rather quickly.Shayne
Add some logging to check that os.environ.get() is setting the correct values on Heroku. Make sure you follow all the steps on support.google.com/mail/?p=BadCredentials - e.g. you don't mention the captcha suggestion in your question. You should also make sure that EMAIL_HOST_USER matches sender = '[email protected]' - Gmail won't let you send email for a different account. If it still doesn't work, then I agree with Klaus that you should look for a different email provider.Cetane
The password is correct I've checked twice. Also the displayUnlockCaptcha just guides me the way to create APP passwords nothing else, which I've done 5 times. Also Its a very small app that will send probably 7-8 emails per week that's it.Equalize
Even if it's a small app I would look for an alternative way to send emails - you say I've seen many of the other answers on stackoverflow - take the many emails as a sign that using Gmail to send Django emails is hard work!Cetane
What's the other alternative way? Can you suggest some. I tried using sendgrid but they are just too slow to respond to my new account.Equalize
Hello @Matuagkeetarp You can use The Amazon SES Free Tier · You can send 2,000 messages per day for free if you use Amazon SES from Amazon EC2. and can use Mandrill email, Mailgun etc.Fizzy
C
5

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.

Charentemaritime answered 3/12, 2020 at 23:1 Comment(0)
S
4

I had the same issue. I solved it with resetting the os environment variables, the values shouldn't be enclosed in quotes. For example, [email protected]. It should work.

Schindler answered 4/2, 2020 at 13:36 Comment(0)
Z
3

Less secure app access is no longer available.

so follow this steps:

  1. Go to your google account and Active you 2-steps verification
  2. Search app password
  3. Choose other and set a name
  4. Copy password and paste in EMAIL_HOST_PASSWORD in django setting
Zoara answered 9/6, 2023 at 10:9 Comment(1)
What a timing! This solved it for me. For a bit more context, this feature became unavailable officially yesterday (lol) Sept 30th, no wonder why my mail worker some hours ago but not the moment I tried. Quoting from google: "Starting on September 30, 2024, less secure apps, third-party apps, or devices that have you sign in with only your username and password will no longer be supported for Google Workspace accounts. "Polypody
P
1

I got the same error while working in Django enter image description here

I resolved it by just setting the google account form Less secure app access (off) to (on) enter image description here

so perform this option Change your Google account settings to allow less secure apps to access your account. (It's not recommended because it might make it easier for someone to break into your account). if you want to allow access anyway, follow these steps:

  1. Go to "Less Secure apps" section
  2. next to "access for less secure apps," select Turn on.
Pampas answered 26/5, 2021 at 4:59 Comment(0)
C
0

I had the same issue,

How I fixed it ?

Step 1: Check if the credentials is correct(email and password),

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'                                       // can also use yahoo
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'yourpassword'
DEFAULT_FROM_EMAIL = '<anything you want>'

Step 2: Check if this is turned ON (if not turn it ON)

Google Less Secure apps

Cattleman answered 12/7, 2021 at 17:17 Comment(0)
S
0

quick answer : go to lesssecureapps session of your google account

than select turn on

more detail : if all your configurations are correct , this error might be because of django trying to login to your application and gmail does not recognize him

Sb answered 19/10, 2021 at 1:35 Comment(0)
H
0

I managed to solve this problem by enabling the email I'm going to use for 2-factor verification, generating an app password.

https://support.google.com/accounts/answer/6010255?hl=en&visit_id=637896899107643254-869975220&p=less-secure-apps&rd=1#zippy=%2Cuse-an-app-password

https://support.google.com/accounts/answer/185833

Hula answered 21/7, 2022 at 13:13 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewSpectra
V
0

Firstly go to: https://myaccount.google.com/security and turn on 2-step verification, after you have turn on 2-step verification you can generate app password after you generate a password paste it in your setting at EMAIL_HOST_PASSWORD.

Vervet answered 7/12, 2022 at 13:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.