2012-10-17 87 views
3

在我的MUA(雷鳥15.0.1)兩個郵件主題都顯示是這樣的:解碼UTF8郵件標題

Keine Mail zu "Abschlagsänderung" gefunden 

這裏是一個片段重現它:

import email 

for subject in ['Subject: Re: Keine Mail zu "=?utf-8?q?Abschlags=C3=A4nderung?=" gefunden', 
       'Subject: =?utf-8?q?Keine_Mail_zu_=22Abschlags=C3=A4nderung=22_gefunden?=']: 
    msg=email.message_from_string(subject) 
    print email.Header.decode_header(msg.get('subject')) 

輸出:

[('Re: Keine Mail zu "=?utf-8?q?Abschlags=C3=A4nderung?=" gefunden', None)] 
[('Keine Mail zu "Abschlags\xc3\xa4nderung" gefunden', 'utf-8')] 

第一個標題不能被python解析,但是thunderbird可以。它是由KMail/1.11.4創建的

如何在Python 2.7中使用元音變音解析第一個標頭?

+1

相關::[電子郵件標頭解碼UTF-8](http://stackoverflow.com/questions/7331351/python-email-header-

你可以通過更換"=?utf-8?q?=22?=解析它們decode-utf-8) –

回答

2

RFC 2047

一個「編碼的字」不能一「引用的字符串」出現。

A '引用字符串' 根據RFC 822

引用字符串= < 「> *(QTEXT /引述對)<」>;定期qtext或引用的字符。

所以我覺得Python庫是正確的,因爲

"=?utf-8?q?Abschlags=C3=A4nderung?=" 

是帶引號的字符串。以最小的引用一個更好的選擇將是

=?utf-8?q?=22Abschlags=C3=A4nderung=22?= 

具有"編碼爲=22

>>> email.Header.decode_header('=?utf-8?q?=22?= =?utf-8?q?Abschlags=C3=A4nderung?= =?utf-8?q?=22?=') 
[('"Abschlags\xc3\xa4nderung"', 'utf-8')] 
+0

非常感謝你的回答。由於它是KMail中的一個錯誤,並且這個MUA並不是非常廣泛的,所以我會離開我的代碼。 – guettli

+0

我再次遇到KMail中的這個bug。 KMail中的錯誤仍然存​​在,並且已有幾年之久:https://bugs.kde.org/show_bug.cgi?id = 69007 – guettli