Coding Article Detail

post featured image

How to set up Gmail as the email backend for a Django project (with code)

This coding article explains the basics of how to set up Gmail as the email backend for a Django project, you will need to have a Gmail account and know the login credentials for that account. You will also need to have Django installed and have a Django project set up.

Here are the steps to set up Gmail as the email backend for your Django project:

1. Install the django-email-backend package by running the following command:

pip install django-email-backend

2. In your Django settings file (typically settings.py), add the following lines to configure the email backend:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'your-gmail-username@gmail.com'
EMAIL_HOST_PASSWORD = 'your-gmail-password'

3. Make sure that you have enabled "Less secure app access" in your Gmail account settings. To do this, go to your Google Account settings, click on "Security" and then scroll down to "Third-party apps with account access." Make sure that "Allow less secure apps" is turned on.

4. Test the email backend by sending a test email from your Django application. You can do this by using Django's built-in send_mail function, like this:

from django.core.mail import send_mail

send_mail(
'Subject here',
'Here is the message.',
'from@example.com',
['to@example.com'],
fail_silently=False,
)

If everything is set up correctly, this should send a test email to the specified address using your Gmail account as the email backend.

By joseguerra on Fri 30 Dec 2022

Last updated 1 year, 10 months ago
Category: Django
0 1

Back to Coding Articles

Comments: 1

johndoe on Tue 03 Jan 2023 wrote:

Thanks for the article!

Would you like to leave a comment?. Already have an account? Then please Sign In

If you don't have an account please Sign Up to create one.