Django Email SMTP Config Fix
In this tutorial, you'll learn about Django Email SMTP Config Fix. We cover key concepts, practical examples, and best practices.
The Problem
Your Django app doesn't send emails. Password reset links, welcome emails, and notifications fail silently or with connection errors.
Quick Fix
Wrong — no email backend configured
# settings.py — EMAIL_BACKEND not set, defaults to console
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
Output: Emails are printed to the console, not sent. In production, this means no emails reach users.
Correct — SMTP configuration
# 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 = 'noreply@dodatech.com'
EMAIL_HOST_PASSWORD = 'your-app-password'
DEFAULT_FROM_EMAIL = 'DodaTech <noreply@dodatech.com>'
Output: Emails sent via SMTP. Users receive password resets, notifications, and confirmations.
Using environment variables
import os
EMAIL_HOST = os.environ.get('EMAIL_HOST', 'smtp.gmail.com')
EMAIL_PORT = int(os.environ.get('EMAIL_PORT', 587))
EMAIL_USE_TLS = os.environ.get('EMAIL_USE_TLS', 'True') == 'True'
EMAIL_HOST_USER = os.environ.get('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD')
DEFAULT_FROM_EMAIL = os.environ.get('DEFAULT_FROM_EMAIL', 'noreply@dodatech.com')
Testing email configuration
from django.core.mail import send_mail
# Test from Django shell
send_mail(
'Test Subject',
'Test message body.',
'noreply@dodatech.com',
['admin"@dodatech".com'],
fail_silently=False,
)
Sendgrid / Mailgun / Amazon SES
# SendGrid
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'apikey'
EMAIL_HOST_PASSWORD = 'SG.xxxxx'
# Mailgun
EMAIL_HOST = 'smtp.mailgun.org'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'postmaster@mg.dodatech.com'
EMAIL_HOST_PASSWORD = 'your-mailgun-password'
File backend for development
import os
from pathlib import Path
if os.environ.get('DJANGO_ENV') == 'development':
EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend'
EMAIL_FILE_PATH = BASE_DIR / 'sent_emails'
Output: Emails written to sent_emails/ directory as files. Open them in a text editor to verify content.
Prevention
- Never use console backend in production.
- Store SMTP credentials in environment variables, not in settings.py.
- Test email sending early in the development cycle.
Common Mistakes with email smtp config
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists
These mistakes appear frequently in real-world DJANGO code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro