簡體   English   中英

Python電子郵件 - 8位MIME支持

[英]Python email - 8bit MIME support

我正在編寫一個簡單的MUA應用程序,我在生成消息方面遇到了麻煩。

我想我的程序自動檢測SMTP服務器是否支持8bit MIME ,如果是,那么它將生成一條消息,其中帶有純文本的部分將在8位上編碼。 在MIME標頭中,它應該是這樣的:

Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit

主要問題是,python3.4 smtplib沒有8-bit encoder ,只有base64 ,並且quoted printable printtable

在我的情況下,它看起來:

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
msg = MIMEMultipart()
msg['From'] = 'someone@example.com'
msg['To'] = 'someone@example.com'
msg['Subject'] = 'subject'

text = MIMEText("text here".encode('utf-8'), _charset='utf-8')

msg.attach(text)

# then sending...

text.as_string()調用返回

'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: base64\n\ndGV4dCBoZXJl\n'

這個消息非常好,但我想要8-bit enconding,而不是base64

問題是我真的被判使用base64 enconding嗎?

email.encoders中只有encode_base64encode_quopri函數

utf-8的默認主體編碼是BASE64,可以在本地替換:

    from email import charset
    ch = charset.Charset('utf-8')
    ch.body_encoding = '8bit'
    text = MIMEText("")
    text.set_charset(ch)
    text.set_payload("text here")
    text.replace_header('Content-Transfer-Encoding', '8bit')
    msg.attach(text)

或全球:

    from email import charset
    charset.add_charset('utf-8', charset.SHORTEST, '8bit')

    text = MIMEText("text here".encode('utf-8'), _charset='utf-8')
    text.replace_header('Content-Transfer-Encoding', '8bit')

    msg.attach(text)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM