Encoding mail subject (SMTP) in Python with non-ASCII characters

前端 未结 2 527
甜味超标
甜味超标 2020-12-29 06:30

I am using Python module MimeWriter to construct a message and smtplib to send a mail constructed message is:

file msg.txt:
--------------------         


        
2条回答
  •  庸人自扰
    2020-12-29 06:38

    From http://docs.python.org/library/email.header.html

    from email.message import Message
    from email.header import Header
    msg = Message()
    msg['Subject'] = Header('主題', 'utf-8')
    print msg.as_string()
    

    Subject: =?utf-8?b?5Li76aGM?=

    more simple:

    from email.header import Header
    print Header('主題', 'utf-8').encode()
    

    =?utf-8?b?5Li76aGM?=

提交回复
热议问题