77👍
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.
8👍
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.
- [Django]-Access tuple in django template
- [Django]-How to get URL of current page, including parameters, in a template?
- [Django]-Apache or Nginx to serve Django applications?
5👍
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.
- [Django]-Django 1.7 throws django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet
- [Django]-Django DB Settings 'Improperly Configured' Error
- [Django]-Django – use reverse url mapping in settings
3👍
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
- [Django]-How to update fields in a model without creating a new record in django?
- [Django]-How do I install psycopg2 for Python 3.x?
- [Django]-Django Management Command Argument
2👍
You need turn on support for less secure devices:
https://www.google.com/settings/security/lesssecureapps
- [Django]-Setting Django up to use MySQL
- [Django]-How to server HTTP/2 Protocol with django
- [Django]-AUTH_USER_MODEL refers to model .. that has not been installed and created AbstractUser models not able to login
2👍
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.
- [Django]-Return results from multiple models with Django REST Framework
- [Django]-Automatically create an admin user when running Django's ./manage.py syncdb
- [Django]-What is the Simplest Possible Payment Gateway to Implement? (using Django)
1👍
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
- [Django]-Update to Django 1.8 – AttributeError: django.test.TestCase has no attribute 'cls_atomics'
- [Django]-Django: When to use QuerySet none()
- [Django]-Django: no such table: django_session
1👍
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
- [Django]-How do I invalidate @cached_property in django
- [Django]-How do I use Django groups and permissions?
- [Django]-"Failed building wheel for psycopg2" – MacOSX using virtualenv and pip
1👍
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.
- [Django]-MySQL ERROR 2026 – SSL connection error – Ubuntu 20.04
- [Django]-How do you get PyPy, Django and PostgreSQL to work together?
- [Django]-Django form: what is the best way to modify posted data before validating?
0👍
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
- [Django]-Django – iterate number in for loop of a template
- [Django]-Django count RawQuerySet
- [Django]-Get path of virtual environment in pipenv
0👍
- 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)"
- [Django]-Why is mod_wsgi not able to write data? IOError: failed to write data
- [Django]-Get Model instance from Form without saving
- [Django]-How to upload multiple images to a blog post in django
0👍
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.
- [Django]-Using a Django custom model method property in order_by()
- [Django]-Heroku, postgreSQL, django, comments, tastypie: No operator matches the given name and argument type(s). You might need to add explicit type casts
- [Django]-Is there a filter for divide for Django Template?
0👍
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.
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.
- [Django]-OneToOneField() vs ForeignKey() in Django
- [Django]-How to test Django's UpdateView?
- [Django]-Django Rest Framework: Disable field update after object is created
-1👍
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.
- [Django]-Link in django admin to foreign key object
- [Django]-Get object by field other than primary key
- [Django]-Django REST Framework – Separate permissions per methods
-1👍
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.
- [Django]-Django Migrations Add Field with Default as Function of Model
- [Django]-Allowing only super user login
- [Django]-Django simple_tag and setting context variables
-2👍
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.
- [Django]-How to allow only one radio button to be checked?
- [Django]-How can I create custom page for django admin?
- [Django]-Best practice for Django project working directory structure