#!/usr/bin/python # -*- coding: utf-8 -*- # This script sends out the report emails. # # Important: When testing, edit ‘config.py’ and set ‘reply_override’ # to your own email address. For production use, set it to ‘None’. import email from email.mime.text import MIMEText import json import pika import smtplib import config connection = pika.BlockingConnection(pika.ConnectionParameters( host = config.mq_server)) channel = connection.channel() smtp = smtplib.SMTP(config.smtp_server) emails = 0 def ack(method): channel.basic_ack(delivery_tag = method.delivery_tag) while True: method, _, body = channel.basic_get(queue = 'patchq_reports', no_ack = False) if not method: break to, subject, ref, content = json.loads(body) if config.reply_override is not None: to = [config.reply_override] # If the content is over 50K then truncate it to the last 50K. # XXX Upload full logs to a website. if len(content) > 50000: content = content[-50000:] # Construct an email of type text/plain with the body of the # message. msg = MIMEText(content) msg['Subject'] = subject msg['From'] = email.utils.formataddr(config.from_address) msg['To'] = ",".join([email.utils.formataddr(t) for t in to]) msg['In-Reply-To'] = ref msg['References'] = ref # Send the email. smtp.sendmail(config.from_address[1], [email.utils.formataddr(t) for t in to], msg.as_string()) emails = emails+1 # We've sent the reply, so ack the message. ack(method) smtp.quit() print ("Sent %d emails." % emails)