-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailtool.py
executable file
·78 lines (62 loc) · 1.88 KB
/
mailtool.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
#!/usr/bin/python
"""Send email blasts to lists of addresses
Sample use:
./mailtool.py --body=samplebody.txt --tolist=samplerecipients.txt \
--subject="Testing 123"
"""
import datetime
import gflags
import smtplib
import sys
gflags.DEFINE_string('body', '', 'Body of email')
gflags.DEFINE_string('tolist', '',
'List of addresses to send to. One per line')
gflags.DEFINE_string('mailserver', 'localhost', 'Mail server to send to')
gflags.DEFINE_string('mailfrom', '[email protected]',
'Address to use as from address')
gflags.DEFINE_string('subject', '', 'Subject for the email')
FLAGS = gflags.FLAGS
def SendEmail(to, subject, body):
s = smtplib.SMTP(FLAGS.mailserver)
s.sendmail(FLAGS.mailfrom, [to],
"""From: %(from)s\r
To: %(to)s\r
Subject: %(subject)s\r
\r
%(body)s""" % {'from': FLAGS.mailfrom,
'to': to,
'subject': subject,
'body': body})
s.quit()
def main(argv):
# Parse flags
try:
argv = FLAGS(argv)
except gflags.FlagsError, e:
print 'Flags error: %s' % e
print
print FLAGS
if len(FLAGS.subject) == 0:
print 'Set a subject!'
sys.exit(1)
if len(FLAGS.tolist) == 0:
print 'Set a list of recipients!'
sys.exit(1)
if len(FLAGS.body) == 0:
print 'Set an email body!'
sys.exit(1)
f = open(FLAGS.body, 'r')
body = f.read()
f.close()
f = open(FLAGS.tolist, 'r')
addresses = f.readlines()
f.close()
count = 0
for address in addresses:
address = address.rstrip()
SendEmail(address, FLAGS.subject, body)
count += 1
print '%s: %d of %d sent' %(datetime.datetime.now(),
count, len(addresses))
if __name__ == "__main__":
main(sys.argv)