【问题标题】:How do I change my Content-Transfer-Encoding header in Python?如何在 Python 中更改我的 Content-Transfer-Encoding 标头?
【发布时间】:2013-10-20 18:43:03
【问题描述】:

这是我现在的代码:

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 来做到这一点?

【问题讨论】:

    标签: python email header mime


    【解决方案1】:

    指定_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
    

    【讨论】:

    • 谢谢。你知道为什么我在 msg['Date'] 末尾的 %z 打印后没有出现在标题中吗?另外,我想要(PDT),但 %Z 也没有出现。
    • @TIMEX,简单datetime.datetime.now()的返回值不包含时区信息。搜索timezonepytzdatetuil
    猜你喜欢
    • 2011-11-09
    • 2017-10-03
    • 2014-03-30
    • 1970-01-01
    • 2019-09-22
    • 2016-11-05
    • 1970-01-01
    • 2014-02-10
    相关资源
    最近更新 更多