Categories
Python Answers

How to send mail using Gmail SMTP with Python Django?

Spread the love

Sometimes, we want to send mail using Gmail SMTP with Python Django.

In this article, we’ll look at how to send mail using Gmail SMTP with Python Django.

How to send mail using Gmail SMTP with Python Django?

To send mail using Gmail SMTP with Python Django, we can add the email settings in the settings.

Then we use the EmailMessage class to send the email.

For instance, we write

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'test@gmail.com'
EMAIL_HOST_PASSWORD = 'test'
EMAIL_PORT = 587

to add the email settings in settings.py.

Then in our view, we write

from django.core.mail import EmailMessage

email = EmailMessage('title', 'body', to=[email])
email.send()

to create a new EmailMessage instance with the title, body, and the to email addresses.

Then we call send to send the email.

Conclusion

To send mail using Gmail SMTP with Python Django, we can add the email settings in the settings.

Then we use the EmailMessage class to send the email.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *