2015-02-07 48 views

回答

0

尝试解码来自俄罗斯的字符集,以ASCII:

msg.body = request.form['firstname'].decode('cp855') 
+0

我想'857'芯片组是更好 – GLHF 2015-02-07 01:29:07

+0

解码解码成Unicode,不ASCII。 – 2015-02-07 11:11:33

+0

AttributeError:'str'对象没有属性'decode'(С) – user2950593 2015-02-07 12:16:50

0

我觉得这是蟒蛇scrypt的编码问题。我提出以下解决方案:

# -*- coding: cp1251 -*- 
import smtplib 
from email.mime.text import MIMEText 
fromaddr = '[email protected]' 
toaddrs = '[email protected]' 
msg = MIMEText('Текст на русском', 'plain') 
username = '[email protected]' 
password = 'pwd' 
server = smtplib.SMTP('smtp.gmail.com:587') 
server.ehlo() 
server.starttls() 
server.login(username,password) 
server.sendmail(fromaddr, toaddrs, msg.as_string()) 
server.quit() 

我检查了我的Gmail帐户,并得到正确的俄罗斯字符。

PS 我的环境是P3.4 + VS2012

+1

在Python 3.4上,我仍然最好使用默认的UTF-8而不是cp1251来进行源代码编码。 – 2015-02-07 10:37:12

+0

如果我将cp1251更改为UTF-8,scrypt遇到俄罗斯符号后会生成异常。所以我认为cp1251是解决方案的义务。 – 2015-02-07 10:46:08

+1

当然,为了工作,你的文件也需要用UTF-8编码保存。 – 2015-02-07 10:49:51