dj-email-url

1.0.6 · active · verified Thu Apr 16

dj-email-url is a Python utility that allows configuring email backend settings in Django applications using a URL, similar to `dj-database-url` for database configurations. It adheres to the 12-factor app principles by enabling email settings to be fetched from an environment variable. The current version is 1.0.6, and it has a steady release cadence with updates addressing Python version compatibility and new features.

Common errors

Warnings

Install

Imports

Quickstart

To quickly set up `dj-email-url`, import the package in your Django `settings.py` file. It automatically fetches the email configuration from the `EMAIL_URL` environment variable (if available) and applies the extracted settings to Django's email configuration. You can also parse an arbitrary email URL directly. The example demonstrates setting a default 'console:' URL for development and applying the extracted settings to the Django `settings` object. Remember to set the `DJANGO_EMAIL_URL` environment variable in your production environment.

import os
from django.conf import settings

# Typically, EMAIL_URL would come from an environment variable.
# Example: 'smtp://user:password@smtp.example.com:587/?use_tls=True'
os.environ['EMAIL_URL'] = os.environ.get('DJANGO_EMAIL_URL', 'console:')

# Configure settings from the EMAIL_URL environment variable
email_config = dj_email_url.config()

# Apply the configurations to Django's EMAIL settings
settings.EMAIL_BACKEND = email_config['EMAIL_BACKEND']
settings.EMAIL_HOST = email_config['EMAIL_HOST']
settings.EMAIL_PORT = email_config['EMAIL_PORT']
settings.EMAIL_HOST_USER = email_config['EMAIL_HOST_USER']
settings.EMAIL_HOST_PASSWORD = email_config['EMAIL_HOST_PASSWORD']
settings.EMAIL_USE_TLS = email_config['EMAIL_USE_TLS']
settings.EMAIL_USE_SSL = email_config['EMAIL_USE_SSL']
settings.EMAIL_TIMEOUT = email_config['EMAIL_TIMEOUT']
settings.EMAIL_FILE_PATH = email_config['EMAIL_FILE_PATH'] # Only relevant for file backend

# Optional: Set DEFAULT_FROM_EMAIL and SERVER_EMAIL from URL query params or defaults
settings.DEFAULT_FROM_EMAIL = email_config.get('DEFAULT_FROM_EMAIL', 'webmaster@localhost')
settings.SERVER_EMAIL = email_config.get('SERVER_EMAIL', 'root@localhost')

print(f"Email Backend: {settings.EMAIL_BACKEND}")
print(f"Email Host: {settings.EMAIL_HOST}")
print(f"Email Port: {settings.EMAIL_PORT}")
print(f"Default From Email: {settings.DEFAULT_FROM_EMAIL}")

view raw JSON →