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

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.

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.

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.2.1.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

django_email_hosts-0.2.1-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_email_hosts-0.2.1.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.27.0

File hashes

Hashes for django_email_hosts-0.2.1.tar.gz
Algorithm Hash digest
SHA256 7352a1ce790994a212bc42fe1895c11305b8f5292062b87498327864400e3e1c
MD5 d8d76814f398c3b138f301f10b8fe909
BLAKE2b-256 fe739698582ec89c20dcf3fef3b1e97678955075f1e07e38829017de5ffba99c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_email_hosts-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 da719d83c3d9abe3e40eaef20a4dce9e6c0b072e1e770526aea9bdca22e4feb8
MD5 e0afede4e0bb066eb10f689376537c64
BLAKE2b-256 8022527a7dcac09152fd9c4c877ec1e58eacf4cc2983b118e3a3e82d5763a460

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