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.