2013-10-12 1 views
1

Это мой код прямо сейчас:Как изменить заголовок Content-Transfer-Encoding в Python?

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, чтобы сделать это?

ответ

3

Указание _charset изменяет Content-Transfer-Encoding и Content-Type

>>> import datetime 
>>> from email.MIMEText import MIMEText 
>>> body = "helloworld" 
>>> msg = MIMEText(body, 'plain', _charset='iso-8859-1') 
>>> msg['Subject'] = 'asdf' 
>>> msg['From'] = 'name <[email protected]>' 
>>> 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 <[email protected]> 
Date: Sun, 13 Oct 2013 06:22:30 

helloworld 
+0

Спасибо. Вы знаете, почему мой% z в конце msg ['Date'] не отображается в заголовках после печати? Кроме того, я хочу (PDT), но% Z также не отображается. – TIMEX

+0

@TIMEX, возвращаемое значение простого 'datetime.datetime.now()' не содержит информации о часовом поясе. Поиск о 'часовом поясе',' pytz', 'datetuil'. – falsetru

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

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