2017-01-18 13 views
0

Когда я использую the mailer module (установленный в Windows с пипсом) с python 2.7, у меня есть ошибка кодирования, если я использую символ не-ascii.Ошибка кодирования с почтовым модулем

Например, в следующем фрагменте кода:

импорт мэйлера

message = mailer.Message() 
message.From = "[email protected]" 
message.To = "[email protected]" 
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') 

(или w с encode). Но тогда я получаю сообщение об ошибке:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe0' in position 35: ordinal not in range(128) 

ответ

1

Ответ ясен в справочной строке для класса Message:

Use the charset property to send messages using other than us-ascii

Таким образом, вы должны использовать:

message = mailer.Message(charset='utf8') 
message.From = "[email protected]" 
message.To = "[email protected]" 
message.Subject = "Test" 
message.Body = "Stuff with special characters like à or ç".decode('utf-8') 

mailer = mailer.Mailer('my_relay-smtp') 
mailer.send(message) 
+0

Как я мог пропустить это? : / –

 Смежные вопросы

  • Нет связанных вопросов^_^