Python编码utf错误

时间:2012-03-18 21:10:50

标签: python encoding utf-8

我正在发送电子邮件,并发送它我需要获得三个变量 - title,provider和vendor_id。这就是我得到它的方式:

# # -*- coding: utf-8 -*-
...
## subject
title = title.encode('utf-8')
subject = "%s - %s"%(provider, title)
msg['Subject'] = subject

## content
body_text = "The following has been successfully delivered:\n\nVendor ID: %s\nProvider: %s\nTitle: %s\n\nThank you."%((vendor_id, provider, title))
content = MIMEText(body_text, 'plain')

但是,当我发送电子邮件时,主题编码正确(包括标题),但body_text不是(也包括标题)。这是它的外观:

Subject: testmovieprovider - Une soirée d'enfer

Body: The following has been successfully delivered:

Vendor ID: 02352_FR
Provider: testmovieprovider
Title: Une soirée d'enfer   <------ why is this happening?

Thank you.

为什么正文中的标题编码不正确,为了让它看起来像第一个,我需要更改什么?

更新:感谢您提供反馈。这是有用的:

MIMEText(body_text, 'plain') - &gt; content = MIMEText(body_text, 'plain', 'UTF-8')

1 个答案:

答案 0 :(得分:4)

设置消息的编码:

content = MIMEText(body_text, 'plain', 'utf-8')