Example #1
0
def test_rfc2231():
    filename = u'ファイル名'
    a = [Attachment(filename, fileobj=StringIO('this is some data'))]

    e = Email(['*****@*****.**'], 'subject', 'body')
    assert e.as_mime(a).get_payload()[1].get_filename() == filename

    e = Email(['*****@*****.**'], 'subject', 'body', rfc2231=True)
    assert e.as_mime(a).get_payload()[1].get_filename() == filename

    e = Email(['*****@*****.**'], 'subject', 'body', rfc2231=False)
    h, enc = decode_header(e.as_mime(a).get_payload()[1].get_filename())[0]
    assert h.decode(enc) == filename
Example #2
0
def test_content_type():
    alt = 'multipart/alternative'
    mixed = 'multipart/mixed'
    text = 'text/plain'
    html = 'text/html'
    octstr = 'application/octet-stream'
    attachments = [Attachment('filename', fileobj=StringIO('content'))]

    def t(m, root_ct, root_cs, expected_parts):
        def cmpcs(actual, expected):
            if expected is None:
                assert actual is None
            else:
                assert lookup(actual.input_charset) == lookup(expected)

        assert m.get_content_type() == root_ct
        cmpcs(m.get_charset(), root_cs)
        p = m.get_payload()
        for p, e in zip(m.get_payload(), expected_parts):
            typ, ct, cs = e
            assert isinstance(p, typ)
            assert p.get_content_type() == ct
            cmpcs(p.get_charset(), cs)

    e = Email(recipients=['*****@*****.**'], subject='subject', body='body')
    t(e.as_mime(), text, 'utf8', [])
    t(e.as_mime(attachments), mixed, None,
      [(MIMEText, text, 'utf8'), (MIMEBase, octstr, None)])

    e = Email(recipients=['*****@*****.**'], subject='subject', html_body='body')
    t(e.as_mime(), html, 'utf8', [])
    t(e.as_mime(attachments), mixed, None,
      [(MIMEText, html, 'utf8'), (MIMEBase, octstr, None)])

    e = Email(recipients=['*****@*****.**'], subject='subject', body='body', html_body='body')
    t(e.as_mime(), alt, None,
      [(MIMEText, text, 'utf8'), (MIMEText, html, 'utf8')])
    t(e.as_mime(attachments), mixed, None,
      [(MIMEBase, alt, None), (MIMEBase, octstr, None)])
    t(e.as_mime(attachments).get_payload()[0], alt, None,
      [(MIMEText, text, 'utf8'), (MIMEText, html, 'utf8')])
Example #3
0
def test_encoding():
    for body in [u'すすめ商品を見るに', u'Российская Федерация']:
        message = Email(['*****@*****.**'], 'subject', body)
        text = message.as_mime().as_string()
        assert base64.b64encode(body.encode('UTF-8')).decode('utf8') in text, u"The encoded form of '%s' is incorrect!" % body