from contextlib import contextmanager
import logging
import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

from settings import Settings


@contextmanager
def smtp_server():
    s = smtplib.SMTP(Settings.SMTP_HOST, Settings.SMTP_PORT)
    s.starttls()
    s.login(Settings.SMTP_USERNAME, Settings.SMTP_PASSWORD)
    yield s
    s.quit()


def send_email(s, sender, msg_subject, html_content):
    msg = MIMEMultipart()
    recipients = [Settings.EMAIL_RECIPIENT]
    msg['Subject'] = msg_subject
    msg['From'] = sender
    msg['To'] = ', '.join(recipients)

    part_html = MIMEText(html_content, 'html', 'utf-8')
    msg.attach(part_html)

    try:
        if not Settings.DRY_RUN:
            s.sendmail(msg['From'], recipients, msg.as_string())
        logging.info('sent email to "%s" about "%s"' % (recipients, msg_subject))

    except Exception as e:
        logging.error('An error occurred while sending email: %s - %s' % (e.__class__, e))
        logging.debug(u'email:\n%s' % html_content)
        raise

@@ -441,28 +441,111 @@
     
-msg = MIMEMultipart(
+if Settings.EMAIL_RECIPIENT is None:%0A        raise Exception('EMAIL_RECIPIENT env variable must be set'
 )%0A  
@@ -585,16 +585,43 @@
 CIPIENT%5D
+%0A%0A    msg = MIMEMultipart()
 %0A    msg

Raise exception is EMAIL_RECIPIENT not set