Categories
Python Answers

How to send email via Python Django?

Spread the love

To send email via Python Django, we can use the EmailMesage class.

For instance, we write

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'youremail@gmail.com'
EMAIL_HOST_PASSWORD = 'email_password'
EMAIL_PORT = 587

to add our email settings in settings.py.

Then we write

from django.core.mail import EmailMessage

email = EmailMessage('Subject', 'Body', to=['your@email.com'])
email.send()

to create an EmailMessage object with the subject, body and to emails.

And then we call send 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 *