-
Notifications
You must be signed in to change notification settings - Fork 1
/
email-stats
executable file
·53 lines (39 loc) · 1.71 KB
/
email-stats
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
#!/usr/bin/python
from __future__ import (absolute_import, division, print_function)
import argparse
import datetime
from email import charset
from email.header import Header
from email.mime.text import MIMEText
import smtplib
import SPI
from jinja2 import Environment, FileSystemLoader
config = SPI.Config()
charset.add_charset('utf-8', charset.SHORTEST, charset.QP)
def send_stats(db, args):
env = Environment(loader=FileSystemLoader(config.get('BASE_DIR') +
'/members.git/templates/'))
template = env.get_template('stats.txt')
stats = db.get_stats()
date = '{:%F %T}'.format(datetime.datetime.today())
msg = MIMEText(template.render(stats=stats, date=date), 'plain', 'utf-8')
msg['Subject'] = Header('SPI membership statistics')
msg['From'] = 'SPI Membership Committee <[email protected]>'
msg['To'] = args.email
msg['Bcc'] = '[email protected]'
if not args.dryrun:
smtp = smtplib.SMTP(config.get('SMTP_SERVER'))
smtp.sendmail('[email protected]', args.email.split(","), msg.as_string())
smtp.quit()
else:
print(msg)
parser = argparse.ArgumentParser(description='Email membership statistics')
parser.add_argument('--email', dest='email',
help="Email address to send message",
action='store', default='[email protected], [email protected]')
parser.add_argument('--dry-run', dest='dryrun',
help="Just show what would happen, don't take any action",
action='store_const', const=True, default=False)
args = parser.parse_args()
db = SPI.MemberDB(config.get('DB_TYPE'), config.get('DB_NAME'))
send_stats(db, args)