Source

ec2control / ec2control / emailer.py

Full commit
import smtplib
from email.mime.text import MIMEText
import re
from mako.template import Template

class ParseMail(object):
    def __init__(self, email):
        self.email = email
        self._parse(email)

    def _parse(self, data):
        lines = data.split("\n")
        orig_from = lines[0]
        if orig_from.startswith("From "):
            orig_from = re.split(r"\s", orig_from)[1]
        else:
            orig_from = None
        subject = None
        from_ = None
        for line in lines:
            if subject is None and line.startswith("Subject: "):
                tokens = re.split(r"\s+", line)
                if len(tokens) == 1:
                    command = None
                    arguments = []
                else:
                    command = tokens[1]
                    arguments = tokens[2:]
            elif from_ is None and line.startswith("From: "):
                from_ = re.split(r"\s", line, 1)[1]
                m = re.match(r".*<(.*?)>\s*$", from_)
                if m:
                    from_ = m.group(1)

        self.effective_from = orig_from or from_
        self.command = command
        self.arguments = arguments

class Response(object):
    def __init__(self, config, parsed_struct):
        self.config = config
        self.parsed_struct = parsed_struct

    def send_started(self, instance):
        self._render(status="started", instance=instance)

    def send_command_error(self, exception, instance):
        self._render(status="command_error", exception=exception, instance=instance)

    def send_error(self, exception, instance):
        self._render(status="error", exception=exception, instance=instance)

    def _render(self, **kw):
        t = Template(filename=self.config['response_template'])
        body = t.render(**kw)
        subject = t.get_def("subject").render(**kw)
        self._send(subject, body)

    def _send(self, subject, message):
        msg = MIMEText(message)

        to_addr = self.parsed_struct.effective_from
        cc_addr = self.config['cc']

        msg['Subject'] = subject
        msg['From'] = self.config['from_addr']
        msg['To'] = to_addr
        msg['Cc'] = cc_addr
        msg = msg.as_string()
        smtp = smtplib.SMTP(self.config['smtp_host'], int(self.config['smtp_port']))

        for addr in set([to] + cc.split(",")):
            smtp.sendmail(from_addr, addr, msg)