Package: dj-email-url
This utility is based on dj-database-url by Kenneth Reitz.It allows to utilize the12factorinspired environments variable to
configure the email backend in a Django application.UsageImport the package insettings.py:importdj_email_urlFetch your email configuration values. The default option is fetch them fromEMAIL_URLenvironment variable:email_config=dj_email_url.config()Other option is parse an arbitrary email URL:email_config=dj_email_url.parse('smtp://...')Finally, it isnecessaryto 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 backendsCurrently,dj-email-urlsupports:BackendEMAIL_URLDescriptionConsoleconsole:Writes to stdout (development)SMTPsmtp:Sends using a mail transfer agent at localhost on port 25SMTPsubmission://USER:PASSWORD@smtp.sendgrid.comSends usingSendGridSMTP on port 587 (STARTTLS)Filefile:Writes to a fileIn-memorymemory:Dummydummy:WarningUsing special characters on passwordsTo use characters that have a special meaning in an URL (think of&)
you should usepercent encoding.
For example,m&mwould becomem%26m.Because the percent character itself (%) serves as the indicator for
percent-encoded octets, it must be percent-encoded as%25.>>>fromurllib.parseimportquote_plus>>>importdj_email_url>>>quote_plus("!@#$%^&*")'%21%40%23%24%25%5E%26%2A'>>>dj_email_url.parse("smtp://user:%21%40%23%24%25%5E%26%2A@localhost")["EMAIL_HOST_PASSWORD"]'!@#$%^&*'Set from email addressesdj-email-urlalso supports to optionally specify origin email addresses.SettingQuery parameterSERVER_EMAIL_server_emailDEFAULT_FROM_EMAIL_default_from_emailFor example:smtp://USER:PASSWORD@smtp.example.com/?_server_email=error@example.comDo 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 settingsThere are other settings available to set from query param.SettingQuery parameterCommentsEMAIL_TIMEOUTtimeoutNew in v1.0.5.More infoSMTP backendTheSMTP backendis selected when the scheme in the URL if one these:ValueDefault portCommentsmtp25Local mail transfer agentsubmissionorsubmit587SMTP with STARTTLSChanged in version 0.1:The use ofsmtpsis nowdiscouragedThe valuesmtpswas used to indicate to use TLS connections,
that is to setEMAIL_USE_TLStoTrue.
Now is recommended to usesubmissionorsubmit(seeservice name for port numbersorUniform Resource Identifier Schemesat IANA).On the most popular mail configuration option is
to use athird party SMTP server to relay emails.>>>url='submission://user@example.com:pass@smtp.example.com'>>>url=dj_email_url.parse(url)>>>asserturl['EMAIL_PORT']==587>>>asserturl['EMAIL_USE_SSL']isFalse>>>asserturl['EMAIL_USE_TLS']isTrueOther common option is to use alocal mail transfer agentPostfix or Exim.
In this case it as easy as:>>>url='smtp://'>>>url=dj_email_url.parse(url)>>>asserturl['EMAIL_HOST']=='localhost'>>>asserturl['EMAIL_PORT']==25>>>asserturl['EMAIL_USE_SSL']isFalse>>>asserturl['EMAIL_USE_TLS']isFalseIt is also possible to configureSMTP-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 assl=Trueas 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)>>>asserturl['EMAIL_PORT']==465>>>asserturl['EMAIL_USE_SSL']isTrue>>>asserturl['EMAIL_USE_TLS']isFalseFile backendThe file backend is the only one which needs a path. The url path is store
inEMAIL_FILE_PATHkey.LicenseThis work is licensed under several licences.All original source code is licensed under BSD-2-Clause.All documentation is licensed under CC-BY-4.0.Some configuration and data files are licensed under CC0-1.0.For more accurate information, check the individual files.You can check the compliance withREUSE helper tool.Change LogUnreleased1.0.6- 2022-09-24Remove unnecessary code (thanks @matthiask).Improve license metadata. No changes on license itself.1.0.5- 2022-02-05Added support for the timeout setting.1.0.4- 2022-01-16Post release version to update change log.1.0.3- 2022-01-16Added support for Python 3.10.Changed continuos integration infrastructure from Travis to GitHub Actions.Switched to PyPA build frontend.1.0.2- 2021-01-23Add support for Python 3.9 (@pauloxnet)1.0.1- 2020-06-03Included LICENSE file in tarball. Thanks to @fabaff.1.0.0- 2020-02-16Removed support for Python versions which reached end-of-life.Fixed typo. Thanks to @jeffmacdonald.0.2.0- 2019-04-08Added support forDEFAULT_FROM_EMAILandSERVER_EMAILin the URL as
query parameters.0.1.0- 2018-03-24Added new schemessubmissionandsubmitto select SMTP backend on port 587 with STARTTLS.
Thanks to @LEW21 to suggest to include newsubmitURI.Discouraged the use of schemesmtpsand 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, andyes, as a single character or in all case variants
(lower, upper and title case) are considered asTrue.0.0.10- 2016-10-14Post release version to fix release date in change log.0.0.9- 2016-10-14Fix case when user sets ssl=False in its url (thanks bogdal)0.0.8- 2016-06-07Allow universal wheel0.0.7- 2016-05-31Add EMAIL_USE_SSL setting to docs and set a default value (thanks iraycd).Add coverage (thanks iraycd).0.0.6- 2016-04-18Fix error parsing URL without credentials (thanks martinmaillard).0.0.5- 2016-04-17Allow URL encoded credentials (thanks kane-c).0.0.4- 2015-03-05Fix README.0.0.3- 2015-03-05Add change log.Addssl=option as a query parameter for SMTP backend.Add Travis continuous integration.0.0.2- 2014-03-12Add Python 3 support.0.0.1- 2013-02-12Initial version.