在python2.7中使用the mailer module(用pip安装在Windows上)时,如果使用非ascii字符,则会出现编码错误。你知道吗
例如,使用以下代码段:
导入邮件程序
message = mailer.Message()
message.From = "me@example.com"
message.To = "shan-x@server.com"
message.Subject = "Test"
message.Body = "Stuff with special characters like à or ç"
mailer = mailer.Mailer('my_relay-smtp')
mailer.send(message)
然后我收到以下电子邮件:
Stuff with special characters like ?? or ??
我试过这个:
message.Body = "Stuff with special characters like à or ç".decode('utf-8')
(或使用encode
)。但是我得到一个错误:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe0' in position 35: ordinal not in range(128)
答案在类消息的帮助字符串中是显式的:
所以你应该使用:
相关问题 更多 >
编程相关推荐