How to send emails using Django All auth? - django

I'm trying to send emails for signup/account verification etc using django allauth, and it has worked correctly during development using the console as the email backend, however now I'm trying to send emails in using gmail, I'm getting the below error:
SMTPAuthenticationError at /accounts/password/reset/
(535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials q1-20020adfdfc1000000b0022e049586c5sm11068760wrn.28 - gsmtp')
I've tried following the steps in the article mentioned in the error message, but they haven't worked.
Not sure what I'm doing wrong, any help would be much appreciated!
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = 'example#gmail.com'
EMAIL_HOST_PASSWORD = '*****'

First of all make sure the setting is correct.
As described here ​Google no longer supports the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password. But you can enable Less secure app access in your developer console. The risks are on your own (Here is a guide to enable).
Final point: Do not hard code password in your source, read it from the environment.
UPDATE:
There are multiple guides online about how to configure Gmail for SMTP access.
For example see here

Related

Djoser not sending verification email with Google Workspace

I'm making a Django Webapp following the tutorial here. I have gotten everything to work up to a certain point. When I send the POST request to my djoser endpoint with my user information, I get back the same response he did in the video with status 201 and the name email and id fields presented back. However, I do not get a confirmation email like he does. The salient difference is that I am not using a vanilla gmail account like the demonstrator is. I have a custom domain hosted through Google Domains for which I also have a Google Workspace account. I can access this account's gmail inbox through the gmail website, and I can send and receive emails fine. However, confirmation emails will not send.
I additionally followed the steps in this guide and they still would not send. Worth noting is that when I look at the google account's log in history, my app password has no record of being used. Any insight would be helpful. Here is the section of my settings.py that contains the relevant settings:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.Emailbackend'
EMAIL_HOST = 'smtp-relay.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'admin#DOMAINNAME.net'
EMAIL_HOST_PASSWORD = '**** **** **** ****'
(I have tried the app password both in the original version with spaces google gave me, as well as without spaces.)
EMAIL_USE_TLS = True

'Authentication Unsuccessful' When Trying To Send Email Through Django

I've been trying to send emails using my Outlook email through Django but have been getting a variety of error messages, including
(535, b'5.7.3 Authentication unsuccessful [SYBPR01CA0102.ausprd01.prod.outlook.com]')
Apparently I need to allow SMTP AUTH, but cannot find how to do this as I am using Outlook in my browser and don't have it downloaded (it keeps requesting I pay for it, but I'd prefer not to as I really don't see the need for this small project, unless I need to). I did however pay for the email.
I've also read that Outlook doesn't support smpt: Outlook 365 OAuth 535 5.7.3 Authentication unsuccessful
My settings in Django are:
DEFAULT_FROM_EMAIL = '#######.###'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp-mail.outlook.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = '#######.###'
EMAIL_HOST_PASSWORD = '########'
My question is can I send Outlook emails through Django? If so, what am I doing wrong?
Thank you.
Make sure SMTP authentication is enabled for your tenant. See https://learn.microsoft.com/en-us/exchange/clients-and-mobile-in-exchange-online/authenticated-client-smtp-submission and https://learn.microsoft.com/en-us/azure/active-directory/fundamentals/concept-fundamentals-security-defaults
I had the same issue and I called Microsoft support. They instructed me to change some security properties in Microsoft Azure. Here is my answer.

Django send email with gmail SMTP only works on localhost (fail on DigitalOcean)

I'm using Django allauth to manage the my user registration. When the user create an email account, he receive a confirmation email. He cannot login to the site if the email is not confirmed. All work well on localhost (the email is sent, the confirmation works, and if the user try to connect before having confirmed the email, the connection fails with the appropriate error message).
But the problem comes when I try my app on my digitalocean server. The mail is not sent and the user can connect even if the email is not confirmed. My Django email settings are:
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'your-username#gmail.com'
EMAIL_HOST_PASSWORD = 'your-password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
Where does the problem comes from? I think it's something with my digitalocean server but I'm not sure at all.
EDIT:
I tried to allow access to my Google account using external application with this link https://accounts.google.com/b/0/DisplayUnlockCaptcha but it doesn't change anything.
I tried to change SMTP port to 465 like described here: https://www.digitalocean.com/community/tutorials/how-to-use-google-s-smtp-server, but same thing, no change.
I had the same problem where it worked on localhost but not when the app was live.
Here's the fix,
go to google's 2step-verification and set that up
go and sign up for app password here
then they ask to select an app and a device, (i chose custom for both), then press generate.
the password they give, put that in
EMAIL_HOST_PASSWORD = 'app-password'
and you should be all good to go.

Django : send mail from Gmail [duplicate]

I am new in django and developing a web application using django. I have successfully set the Signup functionality using Userena in my web application and can Register as a user with Verification Email.
I can show you my SMTP settings in my settings.py file
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'raihncse#gmail.com'
DEFAULT_FROM_EMAIL = 'raihncse#gmail.com'
SERVER_EMAIL = 'raihncse#gmail.com'
EMAIL_HOST_PASSWORD = '**************'
everything was fine before i have changed my EMAIL_HOST_PASSWORD
Infact, for some reason I have to change the previous password of that SERVER_EMAIL(raihncse#gmail.com). I have already edit the EMAIL_HOST_PASSWORD according to the new SERVER_EMAIL password.
but now, if I want to register as a new user, I am facing the following error
SMTPAuthenticationError at /accounts/signup/
(534, '5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvNq\n5.7.14 S3l1pFXENupDa_SdPphNHrnzeLPUOyf6O0l1s31w7h_UARx11P89AxPeeZ6xBi2KeQRjsw\n5.7.14 nvpxZhPVv771W9ljEDyeWnqpqv3_nakuPo36BEl3IlYj9qVujNB5cm0TYFf9LQAxRjFdda\n5.7.14 xh-y5spA9zIQONDsvRRgN3e0DXoIkgxTO3Mu75IaACi-XlvFtFfPBiQ81gUrBZ_PhZsBmh\n5.7.14 ajsSf-flNEmoSydyOTNdmwdB0__8> Please log in via your web browser and\n5.7.14 then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 40sm12125121qgi.47 - gsmtp')
A relatively recent change in Google's authentication system means you're going to have to "allow less secure app access" to your Google account, in order for this to work.
In your error, you are recommended to visit this link: https://support.google.com/mail/answer/78754
On that page:
Step #2 asks you to try Displaying an Unlock Captcha
Step #3 explains how to allow less secure app access. In summary:
Go to Allow less secure apps and choose "Allow" to let less secure apps access your Google account. We don't recommend this option because it may make it easier for someone to gain access to your account.
Recent security protocol of Google will not allow you to send messages through less secure apps.
So the fix is:
Turn ON less secure apps.
But only enabling this doesn't seem to work now. Google does not allow apps to send messages using your default gmail password. So to send messages you have to follow these two simple steps:
Turn ON two factor authentication of your gmail account. Link to turn it on.
Then create app password for your gmail account from Google. It will be a 16 digit code. Put it in settings.py or _credentials.py by: EMAIL_HOST_PASSWORD = 'sixt eend igit code' Link to know how to set up your application password for email
And BINGO! Problem will be solved.
Note: App password can only be created if you have enabled 2 factor authentication for your account.
For me, I did turn on the less secure app option even then I was getting SMTP error.This is how I solved it.
Go to https://accounts.google.com/DisplayUnlockCaptcha and allow access to your Google account by clicking on continue and then try resetting password again in Django.
This works for me:
Turn ON two factors authentication of your Gmail account.
Then create an app password for your Gmail account from Google. It will be a 16 digit code. Put it in settings.py or env.py by EMAIL_HOST_PASSWORD = 'sixteen-digit code you get from your Gmail' and remember to add them in Django config vars also
You need turn on support for less secure devices:
https://www.google.com/settings/security/lesssecureapps
If you are integrating with AWS, my suggestion is to use SES(simple Email Service). You can add and verify your email address in SES which wouldn't raise SMTPAuthenticationError.
I have followed this - https://medium.com/hackernoon/the-easiest-way-to-send-emails-with-django-using-ses-from-aws-62f3d3d33efd.
This worked for me:
I first created email_info.py in the same folder as settings.py:
from .email_info import *
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'youremail#gmail.com'
EMAIL_HOST_PASSWORD = 'your_pass'
EMAIL_PORT = 587
Then in settings.py I added:
from .email_info import *
EMAIL_USE_TLS = EMAIL_USE_TLS
EMAIL_HOST = EMAIL_HOST
EMAIL_HOST_USER = EMAIL_HOST_USER
EMAIL_HOST_PASSWORD = EMAIL_HOST_PASSWORD
EMAIL_PORT = EMAIL_PORT
And finally in views.py:
from django.conf import settings
from django.core.mail import send_mail
send_mail(subject, message, from_email, to_list, fail_silently=Tre)
Here's a YouTube video that explains this process: https://www.youtube.com/watch?v=51mmqf5a0Ss
You can also generate a dedicated email password for Django application.
1- Please check this link https://support.google.com/accounts/answer/185839?co=GENIE.Platform%3DDesktop&hl=en
2- Open the 2 Step verification setting from GOOGLE ACCOUNT--> MANAGE YOUR ACCOUNT --> SECURITY ---> 2 STEP VERIFICATION
3- Third, create a dedicated pass for Django, it will be 16 characters.
4- You can find the django email settings below
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'your_account#gmail.com'
EMAIL_HOST_PASSWORD = '16characters password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
Make sure the password in EMAIL_HOST_PASSWORD is the password that you can use to login to the gmail account in EMAIL_HOST_USER. To confirm that, go to gmail.com and login to the gmail account in EMAIL_HOST_USER using the password in EMAIL_HOST_PASSWORD. If it works in one go without any form of 2-step authentication, then you're set. Otherwise, allow less secure apps and disable display unlock captcha using the tutorials found in other answers. For me, doing both and most importantly, using the correct password worked for me.
How to get emails from Django or any library:
Notes
Note: If your account is using two step authentication it won't work.
First open an Incognito window with the account you want to send emails with.
Open Less secure apps
Then enable it
After that navigate to Unlock Captcha
Then enable it
After that fill the account email and password in the library which you are using
In Django:
EMAIL_HOST_USER is the email address. Ex: email#example.com
EMAIL_HOST_PASSWORD is the email password. Ex: smtpmessages.manager#2012
Run the program
First it will not work, To make it work open Gmail on the incognito window.
Then there will be an email from google saying that an app needs access to your google account
Open the email and click the link and select yes it was me
Then retry and it should work
Thank You!
For more info visit: Check Gmail through other email platforms
Turn ON two factors authentication of your Gmail account.
Generate App Password(You will get this option after Two factor authentication) >> select app >> select device (In my case App is Gmail and device is Windows Machine)
Copy the 16 digit password
Paste this password at EMAIL_HOST_PASSWORD = "(Paste password here)"
This is not exactly an answer to your question. But considering privacy and security of your google account, use Environment Variables in Advanced system settings. And access using "os.environ.get('Host_Email') and ('Host_Pass)" instead of putting your email and password in your code.
https://www.youtube.com/watch?v=IolxqkL7cD8
You need to use an app password to allow your app to access your google account. *You can see my answer explaining how to generate an app password.
Sign in with App Passwords:
An App Password is a 16-digit passcode that gives a less secure app or
device permission to access your Google Account. App Passwords can
only be used with accounts that have 2-Step Verification turned on.
So, your code with an app password of 16-digit passcode is something like below:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'raihncse#gmail.com'
DEFAULT_FROM_EMAIL = 'raihncse#gmail.com'
SERVER_EMAIL = 'raihncse#gmail.com'
EMAIL_HOST_PASSWORD = 'xylnudjdiwpojwzm' # Here
In addition, google hasn't allowed your app to access your google account with username(email address) and password since May 30, 2022. So now, you need username(email address) and an app password to access your google account.
Less secure apps & your Google Account:
To help keep your account secure, from May 30, 2022, ​​Google no
longer supports the use of third-party apps or devices which ask you
to sign in to your Google Account using only your username and
password.
I think you need to turn on google less secure apps. Login to your account and go to less secure apps to change your setting. It is not good but you can try your code.
If you already have allowed access to less secure apps and still having problems go to your account >> login and security >> notifications and activity in your devices, and see if there is a device that tried to access and google denied it, allow that access.
Make sure that you have selected "Turn On" option in Allow less secure apps page (https://www.google.com/settings/security/lesssecureapps).
I suggest you to refresh the page once after selecting "Turn On" radio button. The radio button selection should not move "Turn Off"
Some how it is getting "Turn Off" automatically.

Google Apps stops sending email every few days

Every few days, google apps starts rejecting my username and password with :
SMTPAuthenticationError: (535, '5.7.1 Username and Password not accepted. Learn more at\n5.7.1 http://mail.google.com/support/bin/answer.py?answer=14257 22sm439603yxe.15')
If I go to the google apps login page and try my password, it asks for a captcha. Filling it out correctly, now allows me to send email again for a few days until this happens again.
How can I stop this from happening? Has anyone else seen this when sending automated emails through google apps?
I'm sending registration emails from my website using these settings in django :
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'metaward#metaward.com'
EMAIL_HOST_PASSWORD = '*secret*'
EMAIL_USE_TLS = True
You should throttle your requests to an acceptable rate for Google Apps.
I am not sure if Google has an official statement/guidance about what is the acceptable request rate, though. If you can't find any such public statement/guidance, you should contact Google Apps support to see if you can get it through there.
The maximum limit for sending emails for a given email account per day is 3000. But it is unlikely that you are sending as many emails every day. Even the error message isn't indicating that.
While, the resolution to the actual question can only be resolved by google, the way to circumvent this problem may be by setting up different email users, and choosing one at random.
Use OAuth2:
Explained at http://www.nodemailer.com/
Gmail has came up with the concept of 'less secure' apps which is
basically anyone who uses plain password to login to Gmail, so you
might end up in a situation where one username can send (support for
'less secure' apps is enabled) but other is blocked (support for 'less
secure' apps is disabled).
To prevent having login issues you should either use XOAUTH2 or use
another provider and preferably a dedicated one like Mailgun or
SendGrid