-
Notifications
You must be signed in to change notification settings - Fork 0
/
emailer.py
161 lines (131 loc) · 3.86 KB
/
emailer.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""
Helper module for sending emails via SMTP.
Use emailer.setSmtpConfig(host, port, username, password)
to configure SMTP settings.
Then emailer.enableSending()
to enable actually sending (vs just printing) of emails.
Then you can email.send(fromName, fromEmail, toList, subject, body, ...)
to send emails.
"""
# std:
import smtplib
from email.message import EmailMessage
from email.header import Header
# pip-ext:
# n/a
# pip-int:
# n/a
# loc:
import utils
IS_SENDING_ENABLED = False # <-- Disabled by default.
# ^-- Disabled => just print to console, else send via SMTP.
def enableSending(shouldEnable=True):
global IS_SENDING_ENABLED
# TODO: Check for valid `config` and `defaultSender` first.
IS_SENDING_ENABLED = shouldEnable
def checkSendingEnabled():
return IS_SENDING_ENABLED
config = { # <-- Container for SMTP-related settings.
"host": None,
"port": None,
"username": None,
"password": None,
}
def setSmtpConfig(host, port, username, password, starttls=True):
"Set SMTP related configuration settings."
config.update(
{
"host": host,
"port": int(port),
"username": username,
"password": password,
"starttls": starttls,
}
)
return True
defaultSender = {
"fromName": None,
"fromEmail": None,
}
def setDefaultSender(fromName, fromEmail):
defaultSender.update(
{
"fromName": fromName,
"fromEmail": fromEmail,
}
)
return True
def buildMessage(
fromName,
fromEmail,
toList,
subject,
body,
subtype,
ccList,
bccList,
):
"Internal helper for building EmailMessage objects."
msg = EmailMessage()
msg["From"] = "%s <%s>" % (fromName, fromEmail)
# ^-- TODO: Consider quoting via email.header.Header(). (Consider q'ing all.)
msg["To"] = ", ".join(toList)
# ^-- toList is expected to be a list of (raw) email ids. ["[email protected]", "[email protected]" ..]
if ccList:
msg["Cc"] = ", ".join(ccList)
if bccList:
msg["Bcc"] = ", ".join(bccList)
# ^-- This doesn't work in py2, but works in py3. Tested w/ SMTP broadcast.
msg["Subject"] = subject
msg.set_content(body, subtype=subtype)
return msg
def sendBuiltMessage(msg):
"Internal helper for connecting to and sending via SMTP."
with smtplib.SMTP(config["host"], config["port"]) as smtp:
smtp.ehlo()
if config["starttls"]:
smtp.starttls()
smtp.ehlo()
smtp.login(config["username"], config["password"])
smtp.send_message(msg)
return True
def printBuiltMessage(msg):
"Helper for printing/inspecting built EmailMessage objects."
print("Built message as_string: :::::::::::::::::::::")
print("----------------------------------------------")
print(msg.as_string())
print("----------------------------------------------")
return True
def send(
toList,
subject,
body,
fromName=None,
fromEmail=None,
subtype="plain",
ccList=None,
bccList=None,
):
"Main method of the module, for sending emails."
# Fill defaults:
fromName = fromName or defaultSender["fromName"]
fromEmail = fromEmail or defaultSender["fromEmail"]
assert subtype in ["plain", "html"]
ccList = ccList or []
bccList = bccList or []
if subtype == "plain":
body = utils.stripEachLine(body)
# ^-- For plain text emails, strip each line to avoid leading whitespace.
# Build message:
msg = buildMessage(
fromName, fromEmail, toList, subject, body, subtype, ccList, bccList
)
# printBuiltMessage(msg); # debug aid
# Send or print:
if IS_SENDING_ENABLED:
sendBuiltMessage(msg)
else:
printBuiltMessage(msg)
return msg
# Returned for insepction or reference.
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx