-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail_shed.py
executable file
·517 lines (446 loc) · 15.3 KB
/
mail_shed.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__version__ = '0.1'
import argparse
import os
#==============================================================================
# Command line argument parser
#==============================================================================
# Create parser
argparser = argparse.ArgumentParser(
description='Send emails from imap draft folder which should have been '
'sent by now.',
epilog='Options given via command line are preferred '
'over options set in config file. IMAP or SMTP specific options '
'overwrite general IMAP and SMTP settings.',
add_help=False
)
# Add optional arguments
argparser.add_argument(
'--help',
action='help',
help='show this help message and exit'
)
# General settings
group_general = argparser.add_argument_group('general settings')
group_general.add_argument(
'-s', '--separator',
help='string which separates the time from the subject'
)
group_general.add_argument(
'-d', '--drafts',
help='set the IMAP drafts folder'
)
group_general.add_argument(
'-t', '--timezone',
help='set the timezone for dates (default: UTC)'
)
group_general.add_argument(
'-c', '--config',
default='~/.mail_shed.cfg',
help='set the config file (default: %(default)s)'
)
# IMAP and SMTP settings
group_imap_smtp = argparser.add_argument_group('IMAP and SMTP settings')
group_imap_smtp.add_argument(
'-h', '--host',
help='set the IMAP and SMTP host'
)
group_imap_smtp.add_argument(
'-u', '--user',
help='set the IMAP and SMTP user'
)
group_imap_smtp.add_argument(
'-p', '--password',
help='set the IMAP and SMTP password'
)
# IMAP settings
group_imap = argparser.add_argument_group('IMAP settings')
group_imap.add_argument(
'-ih', '--imap-host',
help='set the IMAP host'
)
group_imap.add_argument(
'-iu', '--imap-user',
help='set the IMAP user'
)
group_imap.add_argument(
'-ip', '--imap-password',
help='set the IMAP password'
)
# SMTP settings
group_smtp = argparser.add_argument_group('SMTP settings')
group_smtp.add_argument(
'-sh', '--smtp-host',
help='set the SMTP host'
)
group_smtp.add_argument(
'-su', '--smtp-user',
help='set the SMTP user'
)
group_smtp.add_argument(
'-sp', '--smtp-password',
help='set the SMTP password'
)
# Logging
group_logging = argparser.add_argument_group('logging and output settings')
group_logging.add_argument(
'-l', '--log-file',
default='~/mail_shed.log',
help='set the log file (default: %(default)s)'
)
group = group_logging.add_mutually_exclusive_group()
group.add_argument(
'-v', '--verbose',
action='store_true',
default=False,
help='be verbose'
)
group.add_argument(
'-q', '--quiet',
action='store_true',
default=False,
help='don\'t output anything'
)
# Version
argparser.add_argument(
'--version',
action='version',
version='%(prog)s ' + __version__,
help='show version information and exit'
)
# Parse the arguments
args = argparser.parse_args()
# Make Path absolute
args.config = os.path.abspath(os.path.expanduser(args.config)) if args.config else None
#==============================================================================
# Logging
#==============================================================================
import logging
log_level = logging.INFO
if args.verbose:
log_level = logging.DEBUG
elif args.quiet:
log_level = logging.WARNING
logging.basicConfig(
format='%(asctime)s - %(levelname)s: %(message)s',
filename=os.path.abspath(os.path.expanduser(args.log_file)),
filemode='a', level=log_level
)
log_handler = logging.StreamHandler()
log_handler.setLevel(log_level)
log_handler.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
log = logging.getLogger()
log.addHandler(log_handler)
log.info('Started')
#==============================================================================
# Config file parser
#==============================================================================
from configparser import ConfigParser
import stat
import sys
try:
file_stat = os.stat(args.config)
except OSError as e:
if e.errno == 2:
log.critical('Config file "%s" does not exist', args.config)
sys.exit(1)
log.critical('File stat error - %s', e)
sys.exit(2)
if (stat.S_IRGRP | stat.S_IROTH) & file_stat.st_mode != 0:
log.critical('Config file "%s" is group- or world-readable. Please `chmod 400` or similar.', args.config)
sys.exit(3)
config = ConfigParser()
config.read_string('''
[imap/smtp]
[imap]
[smtp]
[general]
separator=|
drafts=\\Drafts
timezone=UTC
''')
# Read config file and WARN if it has not been loaded
if len(config.read(args.config)) == 0:
log.warn('Config file %s was not loaded', args.config)
# Overwrite the config values with the values provided via command line
for key in ('separator', 'drafts', 'timezone'):
if getattr(args, key, None) is not None:
config.set('general', key, getattr(args, key))
for key in ('ĥost', 'user', 'password'):
if getattr(args, key, None) is not None:
config.set('imap/smtp', key, getattr(args, key))
for key in ('ĥost', 'user', 'password'):
if getattr(args, 'imap_' + key, None) is not None:
config.set('imap', key, getattr(args, 'imap_' + key))
for key in ('ĥost', 'user', 'password'):
if getattr(args, 'smtp_' + key, None) is not None:
config.set('smtp', key, getattr(args, 'smtp_' + key))
# Set general host/user/password to specific if specific is not set
for connection in ('imap', 'smtp'):
for key in ('host', 'user', 'password'):
if (
config.has_option('imap/smtp', key) and
not config.has_option(connection, key)
):
config.set(connection, key, config.get('imap/smtp', key))
critical_error = False
# Exit if there are config values missing
if len(config.get('general', 'separator')) == 0:
log.critical('the separator must not be an empty string')
critical_error = True
if len(config.get('general', 'drafts')) == 0:
log.critical('the drafts folder must not be an empty string')
critical_error = True
if len(config.get('general', 'timezone')) == 0:
log.critical('the timezone must not be an empty string')
critical_error = True
if not config.has_option('imap', 'host'):
log.critical('there is not imap host set')
critical_error = True
if not config.has_option('imap', 'user'):
log.critical('there is not imap user set')
critical_error = True
if not config.has_option('imap', 'password'):
log.critical('there is not imap password set')
critical_error = True
if not config.has_option('smtp', 'host'):
log.critical('there is not smtp host set')
critical_error = True
if not config.has_option('smtp', 'user'):
log.critical('there is not smtp user set')
critical_error = True
if not config.has_option('smtp', 'password'):
log.critical('there is not smtp password set')
critical_error = True
if critical_error:
sys.exit(1)
#==============================================================================
# Modified IMAP library
#
# Original by Bruno Renié
# http://bruno.im/2010/jul/29/europython-talk-python-and-imap-protocol/
#==============================================================================
import imaplib
imaplib.Commands['XLIST'] = imaplib.Commands['LIST']
class IMAP4_SSL(imaplib.IMAP4_SSL):
def xlist(self, directory='', pattern='*'):
name = 'XLIST'
typ, data = self._simple_command(name, directory, pattern)
return self._untagged_response(typ, data, name)
#==============================================================================
# Main program imports
#==============================================================================
import smtplib
import email
import email.header
import re
from dateutil.parser import parse as parse_datetime
from datetime import datetime
import pytz
import base64
#==============================================================================
# Main program classes, functions, constants
#==============================================================================
class IMAPError(Exception):
pass
def result(response):
code, data = response
if code != 'OK':
raise IMAPError(code)
return data
FOLDER_LINE_RE = re.compile(
r'\((?P<flags>.*?)\) "(?P<delimiter>.*)" (?P<name>.*)'
)
def parse_folder_line(line):
flags, delimiter, mailbox_name = FOLDER_LINE_RE.match(line).groups()
mailbox_name = mailbox_name.strip('"')
return flags, delimiter, mailbox_name
DATE_FORMAT = '%Y-%m-%d %H:%M:%S UTC'
MAIL_DATE_FORMAT = '%a, %d %b %Y %H:%M:%S %z'
SMTP_DATE_FORMAT = '"%%b-%Y %H:%M:%S %z"'
#==============================================================================
# Main program
#==============================================================================
# Try timezone
try:
TIMEZONE = pytz.timezone(config.get('general', 'timezone'))
except pytz.exceptions.UnknownTimeZoneError:
log.critical('Timezone %s does not exist', config.get('general', 'timezone'))
sys.exit(1)
# Connect to IMAP
try:
imap = IMAP4_SSL(config.get('imap', 'host'))
log.debug('Connected to IMAP host %s', config.get('imap', 'host'))
except:
log.critical(
'Could not connect to IMAP host %s', config.get('imap', 'host')
)
sys.exit(1)
try:
imap.login(config.get('imap', 'user'), config.get('imap', 'password'))
log.debug('Logged in on IMAP host as %s', config.get('imap', 'user'))
except:
log.critical(
'Could not login on IMAP host as %s', config.get('imap', 'user')
)
sys.exit(1)
# Connect to SMTP
try:
smtp = smtplib.SMTP_SSL(config.get('smtp', 'host'))
log.debug('Connected to SMTP host %s', config.get('smtp', 'host'))
except:
log.critical(
'Could not connect to SMTP host %s', config.get('smtp', 'host')
)
sys.exit(1)
try:
smtp.login(config.get('smtp', 'user'), config.get('smtp', 'password'))
log.debug('Logged in on SMTP host as %s', config.get('smtp', 'user'))
except:
log.critical(
'Could not login on SMTP host as %s', config.get('smtp', 'user')
)
sys.exit(1)
DRAFTS = config.get('general', 'drafts')
# Get XLIST mailbox list and optionally try LIST on fail
try:
folders = result(imap.xlist())
except IMAPError as e:
if DRAFTS.startswith('\\'):
log.critical(
'Cannot fetch XLIST but needed to get drafts folder - reason %s', e
)
sys.exit(1)
try:
folders = result(imap.list())
except IMAPError as e:
log.critical(
'Cannot fetch LIST but needed to get drafts folder - reason %s', e
)
sys.exit(1)
# Go through folders to find the right mailbox
mailbox_selected = False
for folder in folders:
flags, _, name = parse_folder_line(folder)
if (DRAFTS.startswith('\\') and DRAFTS in flags.split()) or DRAFTS == name:
try:
result(imap.select(name))
mailbox_selected = True
log.debug('Selected mailbox %s', name)
DRAFTS = name
break
except IMAPError:
log.critical('Cannot select mailbox %s', name)
sys.exit(1)
if not mailbox_selected:
log.critical('The mailbox %s was not found on the server', DRAFTS)
sys.exit(1)
# Get all mails from the mailbox
try:
ids = result(imap.search(None, 'ALL'))[0].replace(' ', ',')
except IMAPError as e:
log.critical('Could not get message ids from mailbox - reason %s', e)
sys.exit(1)
try:
data = result(imap.fetch(ids, '(RFC822)'))
except IMAPError as e:
log.critical('Could not get message contents - reason %s', e)
sys.exit(1)
SEPARATOR = config.get('general', 'separator')
for line in data:
if not isinstance(line, tuple):
continue
mail = email.message_from_string(line[1])
subject = mail['subject']
# Check whether we can find the separator
if subject.find(SEPARATOR) == -1:
# Subject might be base64 encoded
decoded = email.header.decode_header(subject)[0]
if decoded[1] is not None:
subject = unicode(*decoded)
if decoded[1] is None or subject.find(SEPARATOR) == -1:
log.debug('Email with subject "%s" does not contain separator', subject)
continue
log.debug('Email with subject "%s" contains separator', subject)
date_is_final = False
date_part = subject[:subject.find(SEPARATOR)].strip()
try:
date = datetime.strptime(date_part, DATE_FORMAT)
date = pytz.UTC.localize(date)
date_is_final = True
log.debug('Date is already in UTC format: %s', date)
except ValueError:
try:
date = parse_datetime(date_part.replace('.', '-'), dayfirst=True)
try:
date = TIMEZONE.localize(date, is_dst=None)
except AmbiguousTimeError:
date = TIMEZONE.localize(date, is_dst=False)
log.warn('Ambigous date for DST - no DST chosen')
date = date.astimezone(pytz.UTC)
log.debug('Parsed UTC date: %s', date)
except ValueError:
log.warn('Cannot get datetime for subject %s', subject)
continue
msg_id = line[0].split()[0]
if date < datetime.now(pytz.UTC):
# Send the mail out
del mail['message-id']
subject = subject[subject.find(SEPARATOR) + 1:].strip()
del mail['subject']
mail['subject'] = email.header.make_header([(subject, 'utf-8')]).encode()
try:
smtp.sendmail(mail['from'], mail['to'], mail.as_string())
log.info('Sent Email "%s"', subject)
try:
imap.store(msg_id, '+FLAGS', '(\\Deleted)')
log.debug('Marked draft to be deleted')
except:
log.warn('Could not mark draft to be deleted')
except:
log.warn('Could not send Email "%s"', subject)
elif not date_is_final:
# Write UTC date into subject
del mail['message-id']
subject = date.strftime(DATE_FORMAT) + ' ' + subject[subject.find(SEPARATOR):].strip()
del mail['subject']
mail['subject'] = email.header.make_header([(subject, 'utf-8')]).encode()
# email_date = '"' + datetime.strptime(
# mail['date'][:mail['date'].rfind(' ')],
# MAIL_DATE_FORMAT
# ).strftime('%d-%b-%Y %H:%M:%S') + mail['date'][mail['date'].rfind(' '):] + '"'
del mail['date']
mail['date'] = datetime.now(TIMEZONE).strftime(MAIL_DATE_FORMAT)
email_date = datetime.now(TIMEZONE).strftime(SMTP_DATE_FORMAT)
try:
result(imap.append(DRAFTS, '(\\Seen)', email_date, mail.as_string()))
log.info('Written UTC date to Email "%s"', subject)
imap.store(msg_id, '+FLAGS', '(\\Deleted)')
log.debug('Marked draft to be deleted')
except Exception as e:
log.warn('Could now write UTC date to mail subject - %s', e)
# Delete mails from IMAP
try:
imap.expunge()
log.debug('Deleted marked emails')
except:
pass
# Disconnect from IMAP
try:
imap.close()
log.debug('Closed IMAP directory')
except:
pass
try:
imap.logout()
log.debug('Logged out from IMAP')
except Exception as e:
log.warn('Could not log out from IMAP: %s', e)
# Disconnect from SMTP
try:
smtp.quit()
log.debug('SMTP quit')
except Exception as e:
log.warn('Could quit from SMTP: %s', e)
log.info('Finished')