Skip to content

Q&A · Answered

How to send emails in Django

asked 9 months, 1 week ago 1710 views 1 answer
23

I need to send email notifications from my Django app. How do I configure and send emails?

1 Answer

One accepted
13

Accepted answer

Not confirmed since it was written, 9 months, 1 week ago

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()

Your answer

Say what you tried, not only what worked — the next person needs the reasoning. Code goes in a fenced block.

Shown beside your answer and never checked, so it cannot be a name someone already has here.

No account needed to answer. Sign in instead and it carries your name and earns reputation.