Skip to main content

Use an URL to configure email backend settings in your Django Application.

Project description

CI status Python versions

This utility is based on dj-database-url by Kenneth Reitz.

It allows to utilize the 12factor inspired environments variable to configure the email backend in a Django application.

Usage

Import the package in settings.py:

import dj_email_url

Fetch your email configuration values. The default option is fetch them from EMAIL_URL environment variable:

email_config = dj_email_url.config()

Other option is parse an arbitrary email URL:

email_config = dj_email_url.parse('smtp://...')

Finally, it is necessary to assign values to settings:

EMAIL_FILE_PATH = email_config['EMAIL_FILE_PATH']
EMAIL_HOST_USER = email_config['EMAIL_HOST_USER']
EMAIL_HOST_PASSWORD = email_config['EMAIL_HOST_PASSWORD']
EMAIL_HOST = email_config['EMAIL_HOST']
EMAIL_PORT = email_config['EMAIL_PORT']
EMAIL_BACKEND = email_config['EMAIL_BACKEND']
EMAIL_USE_TLS = email_config['EMAIL_USE_TLS']
EMAIL_USE_SSL = email_config['EMAIL_USE_SSL']
EMAIL_TIMEOUT = email_config['EMAIL_TIMEOUT']

Alternatively, it is possible to use this less explicit shortcut:

vars().update(email_config)

Supported backends

Currently, dj-email-url supports:

Backend

EMAIL_URL

Description

Console

console:

Writes to stdout (development)

SMTP

smtp:

Sends using a mail transfer agent at localhost on port 25

SMTP

submission://USER:PASSWORD@smtp.sendgrid.com

Sends using SendGrid SMTP on port 587 (STARTTLS)

File

file:

Writes to a file

In-memory

memory:

Dummy

dummy:

Set from email addresses

dj-email-url also supports to optionally specify origin email addresses.

Setting

Query parameter

SERVER_EMAIL

_server_email

DEFAULT_FROM_EMAIL

_default_from_email

For example: smtp://USER:PASSWORD@smtp.example.com/?_server_email=error@example.com

Do not forget to assign values to settings:

SERVER_EMAIL = email_config.get('SERVER_EMAIL', 'root@localhost')
DEFAULT_FROM_EMAIL = email_config.get('DEFAULT_FROM_EMAIL', 'webmaster@localhost')

Other settings

There are other settings available to set from query param.

Setting

Query parameter

Comments

EMAIL_TIMEOUT

timeout

New in v1.0.5.

More info

SMTP backend

The SMTP backend is selected when the scheme in the URL if one these:

Value

Default port

Comment

smtp

25

Local mail transfer agent

submission or submit

587

SMTP with STARTTLS

Changed in version 0.1: The use of smtps is now discouraged The value smtps was used to indicate to use TLS connections, that is to set EMAIL_USE_TLS to True. Now is recommended to use submission or submit (see service name for port numbers or Uniform Resource Identifier Schemes at IANA).

On the most popular mail configuration option is to use a third party SMTP server to relay emails.

>>> url = 'submission://user@example.com:pass@smtp.example.com'
>>> url = dj_email_url.parse(url)
>>> assert url['EMAIL_PORT'] == 587
>>> assert url['EMAIL_USE_SSL'] is False
>>> assert url['EMAIL_USE_TLS'] is True

Other common option is to use a local mail transfer agent Postfix or Exim. In this case it as easy as:

>>> url = 'smtp://'
>>> url = dj_email_url.parse(url)
>>> assert url['EMAIL_HOST'] == 'localhost'
>>> assert url['EMAIL_PORT'] == 25
>>> assert url['EMAIL_USE_SSL'] is False
>>> assert url['EMAIL_USE_TLS'] is False

It is also possible to configure SMTP-over-SSL (usually on 465). This configuration is not generally recommended but might be needed for legacy systems. To apply use this configuration specify SSL using a ssl=True as a query parameter and indicate the port explicitly:

>>> url = 'smtp://user@domain.com:pass@smtp.example.com:465/?ssl=True'
>>> url = dj_email_url.parse(url)
>>> assert url['EMAIL_PORT'] == 465
>>> assert url['EMAIL_USE_SSL'] is True
>>> assert url['EMAIL_USE_TLS'] is False

File backend

The file backend is the only one which needs a path. The url path is store in EMAIL_FILE_PATH key.

Change Log

Unreleased

1.0.5 - 2022-02-05

  • Added support for the timeout setting.

1.0.4 - 2022-01-16

  • Post release version to update change log.

1.0.3 - 2022-01-16

  • Added support for Python 3.10.

  • Changed continuos integration infrastructure from Travis to GitHub Actions.

  • Switched to PyPA build frontend.

1.0.2 - 2021-01-23

  • Add support for Python 3.9 (@pauloxnet)

1.0.1 - 2020-06-03

  • Included LICENSE file in tarball. Thanks to @fabaff.

1.0.0 - 2020-02-16

  • Removed support for Python versions which reached end-of-life.

  • Fixed typo. Thanks to @jeffmacdonald.

0.2.0 - 2019-04-08

  • Added support for DEFAULT_FROM_EMAIL and SERVER_EMAIL in the URL as query parameters.

0.1.0 - 2018-03-24

  • Added new schemes submission and submit to select SMTP backend on port 587 with STARTTLS. Thanks to @LEW21 to suggest to include new submit URI.

  • Discouraged the use of scheme smtps and add a user warning. Thanks to @LEW21 to alert about this confusing usage.

  • Expand which values are considered as truthy on a query string param. Now, 1, on, true, and yes, as a single character or in all case variants (lower, upper and title case) are considered as True.

0.0.10 - 2016-10-14

  • Post release version to fix release date in change log.

0.0.9 - 2016-10-14

  • Fix case when user sets ssl=False in its url (thanks bogdal)

0.0.8 - 2016-06-07

  • Allow universal wheel

0.0.7 - 2016-05-31

  • Add EMAIL_USE_SSL setting to docs and set a default value (thanks iraycd).

  • Add coverage (thanks iraycd).

0.0.6 - 2016-04-18

  • Fix error parsing URL without credentials (thanks martinmaillard).

0.0.5 - 2016-04-17

  • Allow URL encoded credentials (thanks kane-c).

0.0.4 - 2015-03-05

  • Fix README.

0.0.3 - 2015-03-05

  • Add change log.

  • Add ssl= option as a query parameter for SMTP backend.

  • Add Travis continuous integration.

0.0.2 - 2014-03-12

  • Add Python 3 support.

0.0.1 - 2013-02-12

  • Initial version.

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

dj-email-url-1.0.5.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

dj_email_url-1.0.5-py2.py3-none-any.whl (6.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file dj-email-url-1.0.5.tar.gz.

File metadata

  • Download URL: dj-email-url-1.0.5.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for dj-email-url-1.0.5.tar.gz
Algorithm Hash digest
SHA256 ef36f8a324ec57cf3be5c7a7ef44ed6900ca0208624a918ab33adc1cf6427b39
MD5 a7b0a60281d8b190cfe2999d767e2a60
BLAKE2b-256 04eb808ade3f5f1c8a5f731edf5a2cf8f8f19ba7dfb24f8f3ed3c62020ea26c1

See more details on using hashes here.

File details

Details for the file dj_email_url-1.0.5-py2.py3-none-any.whl.

File metadata

  • Download URL: dj_email_url-1.0.5-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for dj_email_url-1.0.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 64257c4f9d8139a4af8e5267229d32260e433fbf257b0cf8fc855bb0cc39ca7d
MD5 87e3a0ef43dc21de10ec9c5eb50c3fac
BLAKE2b-256 cc5f52f88123b306a48ea6679d9ea7b1d246fbbd887945baf17099f3b68c30fb

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