forked from mwiebe/scipy_proceedings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_mailer.py
73 lines (51 loc) · 1.67 KB
/
_mailer.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
import argparse
import smtplib
import os
import getpass
from email.mime.text import MIMEText
import sys
sys.path.insert(0, '..')
from conf import work_dir
from options import cfg2dict
from build_template import _from_template
args = None
password = None
def parse_args():
parser = argparse.ArgumentParser(description="Invite reviewers.")
parser.add_argument('--send', action='store_true')
parser.add_argument('--template', default=None)
global args
args = parser.parse_args()
args.dry_run = not args.send
if args.dry_run:
print '*** This is a dry run. Use --send to send emails.'
return args
def load_config(conf_file):
return cfg2dict(conf_file)
def get_password(sender):
global password
if not args.dry_run and not password:
password = getpass.getpass(sender + "'s password: ")
def email_addr_from(name_email):
return '"%s" <%s>' % (name_email['name'], name_email['email'])
def send_template(sender, recipient, template, template_data,
smtp_server='smtp.gmail.com', smtp_port=587):
if args.dry_run:
print 'Dry run -> not sending mail to %s' % recipient
else:
get_password(sender['login'])
print '-> %s' % recipient
template_data['email'] = recipient
message = _from_template('../mail/templates/' + template, template_data)
if args.dry_run:
print "=" * 80
print message
print "=" * 80
return
session = smtplib.SMTP(smtp_server, smtp_port)
session.ehlo()
session.starttls()
session.ehlo
session.login(sender['login'], password)
session.sendmail(sender['name'], recipient, message)
session.quit()