Prompt
How do I send emails from a Django app?
Latest observation
Oct 7, 2026
GeminiWeb search: on
To send emails from a Django app, configure your email settings in settings.py and use standard Django utilities like send_mail or EmailMultiAlternatives.
Step 1: Configure Email Settings (settings.py)
Option A: Local Development (Console Backend)
For local testing without actually sending emails, Django can print emails directly to your terminal:
Python
# settings.py
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
Option B: Production / SMTP (e.g., Gmail, SendGrid, Mailgun)
For real email delivery using SMTP:
Python
# settings.py
import os
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com' # or your SMTP provider host
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = os.environ.get('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD') # Use App Password for Gmail
DEFAULT_FROM_EMAIL = 'webmaster@yourdomain.com'
Step 2: Send Emails in Views or Services
Method 1: Send Simple Plain-Text Email
Use send_mail() for basic text notifications:
Python
from django.core.mail import send_mail
from django.http import HttpResponse
def send_simple_email(request):
send_mail(
subject='Welcome to Our Site!',
message='Thank you for signing up for our service.',
from_email=None, # Uses DEFAULT_FROM_EMAIL from settings
recipient_list=['user@example.com'],
fail_silently=False,
)
return HttpResponse("Email sent successfully!")
Method 2: Send HTML Emails with Templates
Render HTML templates alongside a plain-text fallback:
Python
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
def send_html_email(user):
subject = 'Welcome to Our Community'
from_email = 'no-reply@yourdomain.com'
to_email = [user.email]
# Render template contents
context = {'user_name': user.first_name}
text_content = f"Hello {user.first_name}, welcome to our platform!"
html_content = render_to_string('emails/welcome.html', context)
# Compose multi-part message
msg = EmailMultiAlternatives(subject, text_content, from_email, to_email)
msg.attach_alternative(html_content, "text/html")
msg.send()
Method 3: Send Mass Emails
Use send_mass_mail() to efficiently send multiple distinct messages over a single SMTP connection:
Python
from django.core.mail import send_mass_mail
message1 = ('Subject 1', 'Body 1', 'from@example.com', ['user1@example.com'])
message2 = ('Subject 2', 'Body 2', 'from@example.com', ['user2@example.com'])
send_mass_mail((message1, message2), fail_silently=False)
Best Practices
- Use Asynchronous Tasks: Sending emails via HTTP requests blocks the request-response cycle. Use task queues like Celery or Huey to deliver emails in the background.
- Environment Variables: Keep sensitive SMTP credentials out of version control by loading
EMAIL_HOST_USERandEMAIL_HOST_PASSWORDvia.envfiles. - Transactional Email APIs: For high-volume production setups, third-party APIs (such as Resend, Postmark, or Amazon SES) often offer higher deliverability and lower latency than direct SMTP.
Citations
- https://www.freecodecamp.org/news/how-to-send-emails-with-django/
- https://medium.com/@python-javascript-php-html-css/sending-emails-in-django-practical-guide-for-developers-692675395510
- https://docs.djangoproject.com/en/6.1/topics/email/
- https://www.youtube.com/shorts/sigfKKKoc-c
- https://plainenglish.io/software-development/how-to-send-emails-from-a-django-app