Skip to content

Django Email SMTP Config Fix

DodaTech Updated 2026-06-24 2 min read

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

  1. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  2. Misunderstanding that String is [Char] with poor performance for large text operations
  3. Using foldl instead of foldl' 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

### Why aren't my emails being sent?

Check: EMAIL_BACKEND, SMTP credentials, firewall (port 587/465), and spam folder. Run send_mail from shell with fail_silently=False.

Should I use TLS or SSL?

TLS (port 587) is standard. SSL (port 465) is legacy. Most modern providers use TLS.

What's a reasonable timeout for SMTP?

Set EMAIL_TIMEOUT = 10 (seconds) to prevent email sending from blocking the response indefinitely.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro