Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
677 views
in Technique[技术] by (71.8m points)

python - Encoding of headers in MIMEText

I'm using MIMEText to create an email from scratch in Python 3.2, and I have trouble creating messages with non-ascii characters in the subject.

For example

from email.mime.text import MIMEText
body = "Some text"
subject = "? My Subject"                   # first char is non-ascii
msg = MIMEText(body,'plain','utf-8')
msg['Subject'] = subject                   # <<< Problem probably here
text = msg.as_string()

The last line gives me the error

UnicodeEncodeError: 'ascii' codec can't encode character 'xbb' in position 0: ordinal not in range(128)

How do I tell MIMEText that the subject is not ascii ? subject.encode('utf-8') doesn't help at all, and anyway I've seen people using unicode strings with no problems in other answers (see for example Python - How to send utf-8 e-mail?)

Edit: I'd like to add that the same code doesn't give any error in Python 2.7 (thought that doesn't mean that the result is correct).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I found the solution. Email headers containing non ascii characters need to be encoded as per RFC 2047. In Python this means using email.header.Header instead of a regular string for header content (see http://docs.python.org/2/library/email.header.html). The right way to write the above example is then

from email.mime.text import MIMEText
from email.header import Header
body = "Some text"
subject = "? My Subject"                   
msg = MIMEText(body,'plain','utf-8')
msg['Subject'] = Header(subject,'utf-8')
text = msg.as_string()

The subject string will be encoded in the email as

=?utf-8?q?=C2=BB_My_Subject?=

The fact the in python 2.x the previous code was working for me is probably related to the mail client being able to interpret the wrongly encoded header.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...