Skip to main content

Support for several SMTP configurations in a single site

Project description

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

django-email-hosts uses the excellent dj-email-url library under the hood. Each SMTP connection is configured using a dj-email-url DSN.

The keys of the EMAIL_HOSTS dictionary are defined by you and there’s no deeper meaning to them.

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.

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=use_backend("sendgrid"),
).send()

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_email_hosts-0.1.1.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

django_email_hosts-0.1.1-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file django_email_hosts-0.1.1.tar.gz.

File metadata

  • Download URL: django_email_hosts-0.1.1.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.6.0 pkginfo/1.8.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.10

File hashes

Hashes for django_email_hosts-0.1.1.tar.gz
Algorithm Hash digest
SHA256 81027acd965c11e6fcb6a3739ec9e86b8356a0a30013760efef0f9e3dbeb38bb
MD5 6b4f3c5cdd45875026e197b7ff52c6cf
BLAKE2b-256 84afb7d9a2013f2c75af5db8acc464feb9d9f0688e8f48bc1221a479ef48eb1a

See more details on using hashes here.

File details

Details for the file django_email_hosts-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: django_email_hosts-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.6.0 pkginfo/1.8.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.10

File hashes

Hashes for django_email_hosts-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1f14df027db4150b873370df1ff388cecc4e29fe044daef72b30c92b159c3c5a
MD5 75994d3fe8e2db2e3d3b48c004111dad
BLAKE2b-256 5bbaa0237064f9e5fa3d641b041ae04f0b56e239583d006b05f965fef09330b3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page