Exemplo n.º 1
0
def send_email(to, subject, body, typ_="text", debug=False):
    """ Send an email. """

    if typ_ == "text":
        msg = MIMEText(body, _charset="utf-8")

    else:
        msg = MIMEMultipart("alternative")
        msg.attach(MIMEText(strip_tags(body), "plain", _charset="utf-8"))
        msg.attach(MIMEText(body, "html", _charset="utf-8"))

    msg["To"] = ", ".join(to) if isinstance(to, list) else to
    msg["From"] = EMAIL_FROM
    msg["Subject"] = subject

    if not debug:
        s = smtplib.SMTP()
        s.connect(EMAIL_DOMAIN)
        s.ehlo()
        s.starttls()
        s.login(EMAIL_USER, base64.decodestring(EMAIL_PASSWORD))
        s.sendmail(EMAIL_FROM, to, msg.as_string())
        s.quit()

    else:
        print msg.as_string()

    return msg
    def test_should_strip_tags_should_not_barf_on_plain_text(self):
        # Given
        text = 'this is plain text'

        # When
        result = strip_tags(text)

        # Then
        self.assertEqual(text, result)

        return
    def test_should_strip_tags_from_broken_html(self):
        # Given
        text = 'this is plain text'
        html = '<a>%s' % text

        # When
        result = strip_tags(html)

        # Then
        self.assertEqual(text, result)

        return