Skip to main content

django-email-hosts – Support for several SMTP configurations in a single site

CI Status

Why?

Some projects may want to sent emails over multiple SMTP relays or smarthosts. Examples are sites running on multiple domains, e.g. info@example.com and info@example.org. It may be possible to use the same SMTP credentials for each sender address but if that isn’t possible django-email-hosts may be a good solution for the problem.

Usage

  • Install: pip install django-email-hosts

  • Configure: Add the EMAIL_HOSTS setting

  • Use: Always explicitly use the SMTP connection returned by email_hosts.backends.get_connection

EMAIL_HOSTS

The keys of the EMAIL_HOSTS dictionary are defined by you and there’s no deeper meaning to them. The values are DSNs inspired by dj-email-url DSNs.

An example configuration (which is possibly nonsensical) looks like this:

EMAIL_HOSTS = {
    "sendgrid": "submission://USER:PASSWORD@smtp.sendgrid.com?_default_from_email=info@example.com",
    "mailgun": "submission://USER:PASSWORD@smtp.mailgun.com?_default_from_email=info@example.org",
}

This configuration creates two SMTP backends, one using sendgrid and one using mailgun. The _default_from_email is completely optional. If the email message’s from_email isn’t set (resp. is equal to the DEFAULT_FROM_EMAIL setting) it automatically defaults to the per-backend value.

Django 6.1 and MAILERS

Django 6.1 introduced the MAILERS setting for configuring several email backends. email_hosts.mailers converts the EMAIL_HOSTS dictionary into MAILERS entries which support the per-backend _default_from_email:

from email_hosts import mailers

MAILERS = {
    "default": {"BACKEND": "django.core.mail.backends.smtp.EmailBackend", ...},
    **mailers(EMAIL_HOSTS),
}

Then, send emails using Django’s own API, e.g. EmailMessage(...).send(using="sendgrid"). get_connection below and the sendtestemailhosts management command use MAILERS automatically when the setting is defined.

email_hosts.backends.get_connection

The get_connection function expects a single key for the EMAIL_HOSTS setting above. Sending a single email using an explicit connection may look as follows, using the settings from above:

from django.core.mail import EmailMessage
from email_hosts.backends import get_connection

EmailMessage(
    "Hello",
    "World",
    to=["recipient@example.com"],
    connection=get_connection("sendgrid"),
).send()

get_connection currently silently returns the default email backend if the key doesn’t exist in the EMAIL_HOSTS dictionary.

sendtestemailhosts management command

The sendtestemailhosts management command allows you to test your email configurations by sending test emails through a specific backend.

Usage:

python manage.py sendtestemailhosts <backend> <email> [<email> ...]

The command takes a backend key from your EMAIL_HOSTS configuration as the first argument, followed by one or more recipient email addresses.

Example using the configuration above:

python manage.py sendtestemailhosts sendgrid test@example.com
python manage.py sendtestemailhosts mailgun admin@example.org recipient@example.com

The test email will include the hostname and timestamp in the subject line, and a simple confirmation message in the body.

Metadata

Release files for django-email-hosts 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-email-hosts 0.3.0
File Size Uploaded Provenance
django_email_hosts-0.3.0.tar.gz 10.7 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for django-email-hosts 0.3.0
File Interpreter ABI Platform
django_email_hosts-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 16.6 kB

Release files / django_email_hosts-0.3.0.tar.gz

Download URL django_email_hosts-0.3.0.tar.gz
Size 10.7 kB
Tags Source
SHA-256 checksum
How to use checksums
5e4060d691123870bd388e0f904bae8b1296d23b9e35bf49c21bda554dbcc8a3
BLAKE2b-256 checksum
How to use checksums
227bc7df95f16c0eee558ce4a6ce654bca430984dceb6aa84e705b825f1c31ac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 1, 2026.

Transparency log

Release files / django_email_hosts-0.3.0-py3-none-any.whl

Download URL django_email_hosts-0.3.0-py3-none-any.whl
Size 5.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
25e951ee364b9ad196954cfda36abbc2b5840f1cc26bd687b443b207117674cd
BLAKE2b-256 checksum
How to use checksums
2f36fc1e66a9c29c98720febc0bb0fb499e639f60db85c09c5f4a3171018e9da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 1, 2026.

Transparency log

Security for django-email-hosts 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for django_email_hosts-0.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 1, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page