2013-02-18 11 views
10

Python unterstützt eine recht funktionale MIME-Library namens email.mime.MIMEText als zitierte Ausdrucke codieren

Ich möchte erreichen, dass ein MIME-Part, der einfachen UTF-8-Text enthält, als zitierte Ausdrucke und nicht als base64 codiert wird. Obwohl alle functionallity in der Bibliothek zur Verfügung steht, habe ich es nicht, es zu benutzen:

Beispiel:

import email.mime.text, email.encoders 
m=email.mime.text.MIMEText(u'This is the text containing ünicöde', _charset='utf-8') 
m.as_string() 
# => Leads to a base64-encoded message, as base64 is the default. 

email.encoders.encode_quopri(m) 
m.as_string() 
# => Leads to a strange message 

Der letzte Befehl an eine seltsame Nachricht führt:

Content-Type: text/plain; charset="utf-8" 
MIME-Version: 1.0 
Content-Transfer-Encoding: base64 
Content-Transfer-Encoding: quoted-printable 

GhpcyBpcyB0aGUgdGV4dCBjb250YWluaW5nIMO8bmljw7ZkZQ=3D=3D 

Dies ist natürlich nicht Als zitierte Ausdrucke codiert, ist der doppelte Header transfer-encoding seltsam (wenn nicht illegal).

Wie kann ich meinen Text als zitierte Ausdrucke in der Mime-Nachricht codiert bekommen?

+0

Siehe auch http://stackoverflow.com/a/9509718/874188 - die Frage ist Python 3, aber ich habe es auch in Python 2 verwendet. – tripleee

Antwort

9

Okay, ich habe eine Lösung, die sehr hacky ist, aber zumindest führt es in eine Richtung: MIMEText setzt Base64 voraus und ich weiß nicht, wie ich das ändern soll. Aus diesem Grund verwende ich MIMENonMultipart:

import email.mime, email.mime.nonmultipart, email.charset 
m=email.mime.nonmultipart.MIMENonMultipart('text', 'plain', charset='utf-8') 

#Construct a new charset which uses Quoted Printables (base64 is default) 
cs=email.charset.Charset('utf-8') 
cs.body_encoding = email.charset.QP 

#Now set the content using the new charset 
m.set_payload(u'This is the text containing ünicöde', charset=cs) 

Nun scheint die Nachricht richtig codiert werden:

Content-Type: text/plain; charset="utf-8" 
MIME-Version: 1.0 
Content-Transfer-Encoding: quoted-printable 

This is the text containing =C3=BCnic=C3=B6de 

Man kann sogar eine neue Klasse konstruieren, die die Komplexität verbirgt:

class MIMEUTF8QPText(email.mime.nonmultipart.MIMENonMultipart): 
    def __init__(self, payload): 
    email.mime.nonmultipart.MIMENonMultipart.__init__(self, 'text', 'plain', 
                 charset='utf-8') 

    utf8qp=email.charset.Charset('utf-8') 
    utf8qp.body_encoding=email.charset.QP 

    self.set_payload(payload, charset=utf8qp) 

Und Verwenden Sie es wie folgt:

m = MIMEUTF8QPText(u'This is the text containing ünicöde') 
m.as_string() 
5

von issue 1525919 angepasst und getestet auf Python 2.7:

from email.Message import Message 
from email.Charset import Charset, QP 

text = "\xc3\xa1 = \xc3\xa9" 
msg = Message() 

charset = Charset('utf-8') 
charset.header_encoding = QP 
charset.body_encoding = QP 

msg.set_charset(charset) 
msg.set_payload(msg._charset.body_encode(text)) 

print msg.as_string() 

geben Ihnen:

MIME-Version: 1.0 
Content-Type: text/plain; charset="utf-8" 
Content-Transfer-Encoding: quoted-printable 

=C3=A1 =3D =C3=A9 

Auch this response aus einem Python-Committer sehen.

+0

Ich vermisste zuerst, dass die Eingabe in 'body_encode' bereits utf-8-codiert sein muss, und dass sie die utf-8-Codierung für Sie nicht tut. Das hier notieren, falls es anderen den Schmerz des gleichen Missverständnisses erspart. –

Verwandte Themen