-
Notifications
You must be signed in to change notification settings - Fork 1
/
send_email.py
380 lines (343 loc) · 13.9 KB
/
send_email.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#-------------------------------------------------------------------------------
# Name: send_email.py
# Purpose: Send an email to a specified address.
#
# Author: User
#
# Created: 04-11-2018
# Copyright: (c) User 2018
# Licence: <your licence>
#-------------------------------------------------------------------------------
# StdLib
import logging.handlers
import time
import os
import smtplib# For SMTP messages
import email# For SMTP messages
# Remote libraries
import yagmail
import yaml
# local
from common import *
class YAMLConfigYagmailEmail():
"""Handle reading, writing, and creating YAML config files.
For Gmail"""
def __init__(self, config_path=None):
# Set default values
self.sender_username = ''
self.sender_password = ''
self.recipient_address = ''
self.subject = ''
self.body_template = ''
if config_path:
# Ensure config dir exists.
config_dir = os.path.dirname(config_path)
if len(config_dir) > 0:# Only try to make a dir if ther is a dir to make.
if not os.path.exists(config_dir):
os.makedirs(config_dir)
# Load/create config file
if os.path.exists(config_path):
self.load(config_path)# Load config file if it exists.
else:
self.save(config_path, self.__class__())# Create an example config file if no file exists.
return
def load(self, config_path):
"""Load configuration from YAML file."""
logging.debug('Reading from config_path={0!r}'.format(config_path))
with open(config_path, 'rb') as load_f:# Read the config from file.
config = yaml.safe_load(load_f)
# Store values to class instance.
for key in config.keys():
setattr(self, key, config[key])# Sets self.key to config[key]
logging.debug('Loaded config values: {0!r}'.format(config))
return
def save(self, config_path, instance):
"""Save current configuration to YAML file."""
logging.debug('Saving to config_path = {0!r}'.format(config_path))
with open(config_path, 'wb') as save_f:# Write data to file.
yaml.dump(
data=vars(instance),
stream=save_f,
explicit_start=True,# Begin with '---'
explicit_end=True,# End with '...'
default_flow_style=False# Output as multiple lines
)
return
class YAMLConfigSmtplibEmail():
"""Handle reading, writing, and creating YAML config files.
For SMTP"""
def __init__(self, config_path=None):
# Set default values
self.smtp_server_address = ''
self.smtp_server_port = 465
self.sender_email_address = ''
self.sender_username = ''
self.sender_password = ''
self.recipient_address = ''
self.subject = ''
self.body_template = ''
if config_path:
# Ensure config dir exists.
config_dir = os.path.dirname(config_path)
if len(config_dir) > 0:# Only try to make a dir if ther is a dir to make.
if not os.path.exists(config_dir):
os.makedirs(config_dir)
# Load/create config file
if os.path.exists(config_path):
self.load(config_path)# Load config file if it exists.
else:
self.save(config_path, self.__class__())# Create an example config file if no file exists.
return
def load(self, config_path):
"""Load configuration from YAML file."""
logging.debug('Reading from config_path={0!r}'.format(config_path))
with open(config_path, 'rb') as load_f:# Read the config from file.
config = yaml.safe_load(load_f)
# Store values to class instance.
for key in config.keys():
setattr(self, key, config[key])# Sets self.key to config[key]
logging.debug('Loaded config values: {0!r}'.format(config))
return
def save(self, config_path, instance):
"""Save current configuration to YAML file."""
logging.debug('Saving to config_path = {0!r}'.format(config_path))
with open(config_path, 'wb') as save_f:# Write data to file.
yaml.dump(
data=vars(instance),
stream=save_f,
explicit_start=True,# Begin with '---'
explicit_end=True,# End with '...'
default_flow_style=False# Output as multiple lines
)
return
class YAMLConfigLoggingSmtpEmail():
"""Handle reading, writing, and creating YAML config files.
For logging-based SMTP"""
def __init__(self, config_path=None):
# Set default values
self.smtp_server_address = ''
self.smtp_server_port = 465
self.sender_email_address = ''
self.sender_username = ''
self.sender_password = ''
self.recipient_address = ''
self.subject = ''
self.body_template = ''
if config_path:
# Ensure config dir exists.
config_dir = os.path.dirname(config_path)
if len(config_dir) > 0:# Only try to make a dir if ther is a dir to make.
if not os.path.exists(config_dir):
os.makedirs(config_dir)
# Load/create config file
if os.path.exists(config_path):
self.load(config_path)# Load config file if it exists.
else:
self.save(config_path, self.__class__())# Create an example config file if no file exists.
return
def load(self, config_path):
"""Load configuration from YAML file."""
logging.debug('Reading from config_path={0!r}'.format(config_path))
with open(config_path, 'rb') as load_f:# Read the config from file.
config = yaml.safe_load(load_f)
# Store values to class instance.
for key in config.keys():
setattr(self, key, config[key])# Sets self.key to config[key]
logging.debug('Loaded config values: {0!r}'.format(config))
return
def save(self, config_path, instance):
"""Save current configuration to YAML file."""
logging.debug('Saving to config_path = {0!r}'.format(config_path))
with open(config_path, 'wb') as save_f:# Write data to file.
yaml.dump(
data=vars(instance),
stream=save_f,
explicit_start=True,# Begin with '---'
explicit_end=True,# End with '...'
default_flow_style=False# Output as multiple lines
)
return
def get_current_unix_time_int():
"""Return the current UTC+0 unix time as an int"""
# Get current time at UTC+0
# Convert to time tuple
# Convert time tuple to float seconds since epoch
# Convert float to int
current_unix_time_int = int(time.mktime(datetime.datetime.utcnow().timetuple()))
return current_unix_time_int
def format_message(message):
"""Given a string, apply formatting to it
{unixtime} - seconds-since-epoch time
"""
logging.debug('message = {0!r}'.format(message))
assert(type(message) in [str, unicode])
new_message = message
if '{unixtime}' in message:
time_value = get_current_unix_time_int()
new_message = new_message.format(unixtime=time_value)
logging.debug('new_message = {0!r}'.format(new_message))
return new_message
def send_mail_gmail(sender_username, sender_password, recipient_address, subject, body_template):
"""Send an email from gmail.
Required gmail account to be configured to allow unsecure applications."""
logging.debug(u'send_mail_gmail() locals()={0!r}'.format(locals()))# Record arguments
# Try sending an email
logging.info("Sending email from gmail to {0!r}".format(recipient_address))
# Validate values for email
# credentials
assert(type(sender_username) in [str, unicode])
assert(type(sender_password) in [str, unicode])
# message
assert(type(recipient_address) in [str, unicode])
assert(type(subject) in [str, unicode])
assert(type(body_template) in [str, unicode])
# Format message
body_text = format_message(message=body_template)
logging.debug('body_text = {0!r}'.format(body_text))
# Actually send the message
yag = yagmail.SMTP(sender_username, sender_password)
yag.send(
to=recipient_address,
subject=subject,
contents=body_text
)
logging.info("Sent email from gmail to {0!r}".format(recipient_address))
return
def send_mail_smtp(
smtp_server_address, smtp_server_port, sender_email_address,
sender_username, sender_password, recipient_address,
subject, body_template,
):
"""
Send one email using SMTP
https://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol
https://docs.python.org/2/library/email-examples.html
"""
logging.debug(u'send_mail_smtp() locals()={0!r}'.format(locals()))# Record arguments
logging.info("Sending email using SMTP to {0!r}".format(recipient_address))
# Validate values for email
# server
assert(type(smtp_server_address) in [str, unicode])# text
assert(type(smtp_server_port) in [int])# positive integer
assert(smtp_server_port >= 0)
# credentials
assert(type(sender_username) in [str, unicode])# text
assert(type(sender_password) in [str, unicode])# text
# message
assert(type(recipient_address) in [str, unicode])# text
assert(type(subject) in [str, unicode])# text
assert(type(body_template) in [str, unicode])# text
#
# Format message
body_text = format_message(message=body_template)
logging.debug('body_text = {0!r}'.format(body_text))
# Prepare message
logging.debug('Preparing message')
msg = email.mime.text.MIMEText(body_text)
msg['Subject'] = subject
msg['From'] = sender_email_address
msg['To'] = recipient_address
# Initiate connection
logging.debug('Starting connection')
s = smtplib.SMTP_SSL(
host=smtp_server_address,
port=smtp_server_port
)
logging.debug('Logging in to email server')
s.login(sender_username, sender_password)
# Send message
logging.debug('Sending message')
s.sendmail(sender_email_address, [recipient_address], msg.as_string())
# Clean up
logging.debug('Ending SMTP connection')
s.quit()
logging.info("Sent email using SMTP to {0!r}".format(recipient_address))
return
def send_mail_logging(# WIP
smtp_server_address, smtp_server_port, sender_email_address,
sender_username, sender_password, recipient_address,
subject, body_template,
):
"""Send an email over SMTP using the logging module.
https://docs.python.org/2/library/logging.handlers.html#logging.handlers.SMTPHandler
!This function does not work yet!
"""
logging.debug(u'send_mail_logging() locals()={0!r}'.format(locals()))# Record arguments
logging.warning('send_mail_logging() !This function does not work yet!')
# Format body
body_text = format_message(message=body_template)
logging.debug('body_text = {0!r}'.format(body_text))
formatter = logging.Formatter("%(asctime)s:%(message)s")
email_logger = logging.getLogger()
email_logger.setLevel(logging.DEBUG)
# Instantiate logging handler
mh = logging.handlers.SMTPHandler(
mailhost=(smtp_server_address, smtp_server_port),
fromaddr=sender_email_address,
toaddrs=recipient_address,
subject=subject,
credentials=(sender_username, sender_password),
secure=None
)
mh.setLevel(logging.CRITICAL)
mh.setFormatter(formatter)
email_logger.addHandler(mh)
# Send email
email_logger.critical(body_text)
return
def dev():
"""Development test area"""
logging.warning(u'running dev()')
# New SMTP
# SMTP
logging.info('Testing smtplib-based SMTP')
smtp_config_path = os.path.join('config', 'email_smtplib.yaml')
cfg_smtplib = YAMLConfigSmtplibEmail(config_path=smtp_config_path)
send_mail_smtp(
smtp_server_address = cfg_smtplib.smtp_server_address,
smtp_server_port = cfg_smtplib.smtp_server_port,
sender_email_address = cfg_smtplib.sender_email_address,
sender_username = cfg_smtplib.sender_username,
sender_password = cfg_smtplib.sender_password,
recipient_address = cfg_smtplib.recipient_address,
subject = cfg_smtplib.subject,
body_template = cfg_smtplib.body_template
)
# Gmail
logging.info('Testing Gmail')
gmail_config_path = os.path.join('config', 'email_gmail.yaml')
cfg_gmail = YAMLConfigYagmailEmail(config_path=gmail_config_path)
send_mail_gmail(
sender_username=cfg_gmail.sender_username,
sender_password=cfg_gmail.sender_password,
recipient_address=cfg_gmail.recipient_address,
subject=cfg_gmail.subject,
body_template=cfg_gmail.body_template
)
# Logging smtphandler
logging.info('Testing logging-based SMTP')
logging_config_path = os.path.join('config', 'email_logging.yaml')
cfg_logging = YAMLConfigLoggingSmtpEmail(config_path=logging_config_path)
send_mail_logging(
smtp_server_address = cfg_logging.smtp_server_address,
smtp_server_port = cfg_logging.smtp_server_port,
sender_email_address = cfg_logging.sender_email_address,
sender_username = cfg_logging.sender_username,
sender_password = cfg_logging.sender_password,
recipient_address = cfg_logging.recipient_address,
subject = cfg_logging.subject,
body_template = cfg_logging.body_template,
)
return
def main():
dev()
return
if __name__ == '__main__':
setup_logging(os.path.join("debug", "send_email.log.txt"), console_level=logging.DEBUG)# Setup logging
try:
main()
# Log exceptions
except Exception, e:
logging.critical("Unhandled exception!")
logging.exception(e)
logging.info("Program finished.")