2

这是我现在的代码:

from email.MIMEText import MIMEText
body = "helloworld"
msg = MIMEText(body, 'plain')
msg['Subject']= subject
msg['From']   =  from_field['name'] + ' <'+from_field['email']+'>'
msg['Date'] =  datetime.datetime.now().strftime('%a, %d %b %Y %H:%M:%S %z')


#other code here for connecting to SMTP
conn.sendmail(from_field['email'],[to_email], msg.as_string()) #finally send the email

我当前的代码产生以下标题:

Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset="us-ascii" 

但是,我希望我的代码产生以下内容:

Content-Transfer-Encoding: quoted-printable 
Content-Type: text/plain; charset=iso-8859-1

我怎样才能修改我的 MIMEText 来做到这一点?

4

1 回答 1

5

指定_charset更改Content-Transfer-EncodingContent-Type

>>> import datetime
>>> from email.MIMEText import MIMEText
>>> body = "helloworld"
>>> msg = MIMEText(body, 'plain', _charset='iso-8859-1')
>>> msg['Subject'] = 'asdf'
>>> msg['From'] = 'name <username@example.com>'
>>> msg['Date'] = datetime.datetime.now().strftime('%a, %d %b %Y %H:%M:%S %z')
>>> print msg
From nobody Sun Oct 13 06:22:32 2013
Content-Type: text/plain; charset="iso-8859-1"
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Subject: asdf
From: name <username@example.com>
Date: Sun, 13 Oct 2013 06:22:30

helloworld
于 2013-10-12T21:22:49.127 回答