send-reports: Truncate content to 50K.
[patchq.git] / send-reports.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # This script sends out the report emails.
5 #
6 # Important: When testing, edit ‘config.py’ and set ‘reply_override’
7 # to your own email address.  For production use, set it to ‘None’.
8
9 import email
10 from email.mime.text import MIMEText
11 import json
12 import pika
13 import smtplib
14
15 import config
16
17 connection = pika.BlockingConnection(pika.ConnectionParameters(
18     host = config.mq_server))
19 channel = connection.channel()
20
21 smtp = smtplib.SMTP(config.smtp_server)
22 emails = 0
23
24 def ack(method):
25     channel.basic_ack(delivery_tag = method.delivery_tag)
26
27 while True:
28     method, _, body = channel.basic_get(queue = 'patchq_reports',
29                                         no_ack = False)
30     if not method: break
31
32     to, subject, ref, content = json.loads(body)
33
34     if config.reply_override is not None:
35         to = [config.reply_override]
36
37     # If the content is over 50K then truncate it to the last 50K.
38     # XXX Upload full logs to a website.
39     if len(content) > 50000:
40         content = content[-50000:]
41
42     # Construct an email of type text/plain with the body of the
43     # message.
44     msg = MIMEText(content)
45     msg['Subject'] = subject
46     msg['From'] = email.utils.formataddr(config.from_address)
47     msg['To'] = ",".join([email.utils.formataddr(t) for t in to])
48     msg['In-Reply-To'] = ref
49     msg['References'] = ref
50
51     # Send the email.
52     smtp.sendmail(config.from_address[1],
53                   [email.utils.formataddr(t) for t in to],
54                   msg.as_string())
55     emails = emails+1
56
57     # We've sent the reply, so ack the message.
58     ack(method)
59
60 smtp.quit()
61
62 print ("Sent %d emails." % emails)