Ejemplo n.º 1
0
def test_message_as_bytes():
    msg = Message(sender="*****@*****.**", recipients=["*****@*****.**"])
    msg.body = "normal ascii text"
    assert bytes(msg) == msg.as_bytes()
Ejemplo n.º 2
0
def test_empty_subject_header():
    mail = Mail(**test_mail_options)
    msg = Message(sender="*****@*****.**", recipients=["*****@*****.**"])
    msg.body = "normal ascii text"
    mail.send(msg)
    assert 'Subject:' not in msg.as_string()
Ejemplo n.º 3
0
def test_message_default_sender():
    msg = Message(recipients=["*****@*****.**"])
    msg.body = "normal ascii text"
    mail = Mail(**test_mail_options)
    mail.send(msg)
    assert msg.sender == '*****@*****.**'
Ejemplo n.º 4
0
def test_message_charset():
    msg = Message(sender="*****@*****.**",
                  subject="subject",
                  recipients=["*****@*****.**"],
                  charset='us-ascii')

    # ascii body
    msg.body = "normal ascii text"
    assert 'Content-Type: text/plain; charset="us-ascii"' in msg.as_string()

    # ascii html
    msg = Message(sender="*****@*****.**",
                  subject="subject",
                  recipients=["*****@*****.**"],
                  charset='us-ascii')
    msg.body = None
    msg.html = "<html><h1>hello</h1></html>"
    assert 'Content-Type: text/html; charset="us-ascii"' in msg.as_string()

    # unicode body
    msg = Message(sender="*****@*****.**",
                  subject="subject",
                  recipients=["*****@*****.**"])
    msg.body = "ünicöde ←→ ✓"
    assert 'Content-Type: text/plain; charset="utf-8"' in msg.as_string()

    # unicode body and unicode html
    msg = Message(sender="*****@*****.**",
                  subject="subject",
                  recipients=["*****@*****.**"])
    msg.html = "ünicöde ←→ ✓"
    assert 'Content-Type: text/plain; charset="utf-8"' in msg.as_string()
    assert 'Content-Type: text/html; charset="utf-8"' in msg.as_string()

    # unicode body and attachments
    msg = Message(sender="*****@*****.**",
                  subject="subject",
                  recipients=["*****@*****.**"])
    msg.html = None
    msg.attach(data=b"foobar",
               content_type='text/csv',
               headers={'X-Extra-Header': 'Yes'})
    assert 'Content-Type: text/plain; charset="utf-8"' in msg.as_string()
    assert 'X-Extra-Header' in msg.as_string()

    # unicode sender as tuple
    msg = Message(sender=("送信者", "*****@*****.**"),
                  subject="表題",
                  recipients=["*****@*****.**"],
                  reply_to="返信先 <*****@*****.**>",
                  charset='shift_jis')  # japanese
    msg.body = '内容'
    assert 'From: =?iso-2022-jp?' in msg.as_string()
    assert 'From: =?utf-8?' not in msg.as_string()
    assert 'Subject: =?iso-2022-jp?' in msg.as_string()
    assert 'Subject: =?utf-8?' not in msg.as_string()
    assert 'Reply-To: =?iso-2022-jp?' in msg.as_string()
    assert 'Reply-To: =?utf-8?' not in msg.as_string()
    assert 'Content-Type: text/plain; charset="iso-2022-jp"' in msg.as_string()

    # unicode subject sjis
    msg = Message(sender="*****@*****.**",
                  subject="表題",
                  recipients=["*****@*****.**"],
                  charset='shift_jis')  # japanese
    msg.body = '内容'
    assert 'Subject: =?iso-2022-jp?' in msg.as_string()
    assert 'Content-Type: text/plain; charset="iso-2022-jp"', msg.as_string()

    # unicode subject utf-8
    msg = Message(sender="*****@*****.**",
                  subject="subject",
                  recipients=["*****@*****.**"],
                  charset='utf-8')
    msg.body = '内容'
    assert 'Subject: subject' in msg.as_string()
    assert 'Content-Type: text/plain; charset="utf-8"' in msg.as_string()

    # ascii subject
    msg = Message(sender="*****@*****.**",
                  subject="subject",
                  recipients=["*****@*****.**"],
                  charset='us-ascii')
    msg.body = "normal ascii text"
    assert 'Subject: =?us-ascii?' not in msg.as_string()
    assert 'Content-Type: text/plain; charset="us-ascii"' in msg.as_string()

    # default charset
    msg = Message(sender="*****@*****.**",
                  subject="subject",
                  recipients=["*****@*****.**"])
    msg.body = "normal ascii text"
    assert 'Subject: =?' not in msg.as_string()
    assert 'Content-Type: text/plain; charset="utf-8"' in msg.as_string()