How to send emails in Django
Answered
23
I need to send email notifications from my Django app. How do I configure and send emails?
D
Asked by
db_admin
Silver
•
415 rep
1 Answer
13
Configure email settings
# settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'your-email@gmail.com'
EMAIL_HOST_PASSWORD = 'your-app-password'
Send email
from django.core.mail import send_mail, EmailMessage
# Simple email
send_mail(
'Subject',
'Message body',
'from@example.com',
['to@example.com'],
fail_silently=False,
)
# HTML email
email = EmailMessage(
'Subject',
'HTML content here',
'from@example.com',
['to@example.com'],
)
email.content_subtype = 'html'
email.send()
O
Platinum
•
593 rep
Your Answer
You need to be logged in to answer questions.
Log In to Answer