23
I need to send email notifications from my Django app. How do I configure and send emails?
Loading...
Q&A · Answered
I need to send email notifications from my Django app. How do I configure and send emails?
Accepted answer
# 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'
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()Say what you tried, not only what worked — the next person needs the reasoning. Code goes in a fenced block.