-
Notifications
You must be signed in to change notification settings - Fork 15
/
dj_email_url.py
122 lines (91 loc) · 3.12 KB
/
dj_email_url.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# SPDX-FileCopyrightText: 2013-2022 Miguel Gonzalez <migonzalvar@gmail.com>
#
# SPDX-License-Identifier: BSD-2-Clause
# -*- coding: utf-8 -*-
import os
import warnings
import urllib.parse
DEFAULT_ENV = 'EMAIL_URL'
SCHEMES = {
'smtp': 'django.core.mail.backends.smtp.EmailBackend',
'submission': 'django.core.mail.backends.smtp.EmailBackend',
'submit': 'django.core.mail.backends.smtp.EmailBackend',
'smtps': 'django.core.mail.backends.smtp.EmailBackend', # pend deprecation
'console': 'django.core.mail.backends.console.EmailBackend',
'file': 'django.core.mail.backends.filebased.EmailBackend',
'memory': 'django.core.mail.backends.locmem.EmailBackend',
'dummy': 'django.core.mail.backends.dummy.EmailBackend'
}
TRUTHY = (
'1',
'y', 'Y', 'yes', 'Yes', 'YES',
't', 'T', 'true', 'True', 'TRUE',
'on', 'On', 'ON',
)
def unquote(value):
return urllib.parse.unquote(value) if value else value
def config(env=DEFAULT_ENV, default=None):
"""Returns a dictionary with EMAIL_* settings from EMAIL_URL."""
conf = {}
s = os.environ.get(env, default)
if s:
conf = parse(s)
return conf
def parse(url):
"""Parses an email URL."""
conf = {}
url = urllib.parse.urlparse(url)
qs = urllib.parse.parse_qs(url.query)
# Remove query strings
path = url.path[1:]
path = path.split('?', 2)[0]
# Update with environment configuration
conf.update({
'EMAIL_FILE_PATH': path,
'EMAIL_HOST_USER': unquote(url.username),
'EMAIL_HOST_PASSWORD': unquote(url.password),
'EMAIL_HOST': url.hostname,
'EMAIL_PORT': url.port,
'EMAIL_USE_SSL': False,
'EMAIL_USE_TLS': False,
'EMAIL_TIMEOUT': None,
})
if url.scheme in SCHEMES:
conf['EMAIL_BACKEND'] = SCHEMES[url.scheme]
# Set defaults for `smtp`
if url.scheme == 'smtp':
if not conf['EMAIL_HOST']:
conf['EMAIL_HOST'] = 'localhost'
if not conf['EMAIL_PORT']:
conf['EMAIL_PORT'] = 25
# Set defaults for `smtps`
if url.scheme == 'smtps':
warnings.warn(
"`smtps` scheme will be deprecated in a future version,"
" use `submission` instead",
UserWarning,
)
conf['EMAIL_USE_TLS'] = True
# Set defaults for `submission`/`submit`
if url.scheme in ('submission', 'submit'):
conf['EMAIL_USE_TLS'] = True
if not conf['EMAIL_PORT']:
conf['EMAIL_PORT'] = 587
# Query args overwrite defaults
if 'ssl' in qs and qs['ssl']:
if qs['ssl'][0] in TRUTHY:
conf['EMAIL_USE_SSL'] = True
conf['EMAIL_USE_TLS'] = False
elif 'tls' in qs and qs['tls']:
if qs['tls'][0] in TRUTHY:
conf['EMAIL_USE_SSL'] = False
conf['EMAIL_USE_TLS'] = True
# Timeout
if 'timeout' in qs:
conf['EMAIL_TIMEOUT'] = int(qs['timeout'][0])
# From addresses
if '_server_email' in qs:
conf['SERVER_EMAIL'] = qs['_server_email'][0]
if '_default_from_email' in qs:
conf['DEFAULT_FROM_EMAIL'] = qs['_default_from_email'][0]
return conf