2013-03-26 64 views
1

我喜歡在谷歌應用引擎上用python發送訂單通知。 問題是郵件正文可能包含像'öäü'這樣的特殊字符,但我沒有找到修改內容類型字符集的機會。GAE Python - 如何在郵件正文中編碼特殊字符?

是否有可能將字符集從charset =「us-ascii」更改爲「utf-8」,並仍然使用谷歌應用程序引擎郵件API或解決方法?喜歡添加一個參數Content-transfer-encoding:quoted-printable?

這裏我的方法來發送通知:

from google.appengine.ext import db 
from google.appengine.api import mail 

from email.header import Header 

def encode_mail_header(line): 
    return Header(line, 'utf-8').encode() 

msg = u"Message with some chars like öäüßéèô..." 
subject = encode_mail_header(u"Hans Müller your Ticket") 
sender = "My Service <[email protected]>" 
to = encode_mail_header(u"Hans Müller") 
to += " <[email protected]>" 

message = mail.EmailMessage(sender=sender, 
     to=to, 
     subject=subject, 
     body=msg) 
message.send() 

收到的電子郵件的代碼從我的開發服務器:

Received: from spooler by localhost (Mercury/32 v4.62); 26 Mar 2013 10:50:35 +0100 
X-Envelope-To: <[email protected]> 
Return-path: <[email protected]> 
Received: from [192.168.56.1] (127.0.0.1) by localhost (Mercury/32 v4.62) with ESMTP ID MG000011; 
    26 Mar 2013 10:50:24 +0100 
Content-Type: multipart/mixed; boundary="===============1598388400==" 
MIME-Version: 1.0 
To: =?utf-8?q?Hans_M=C3=BCller?= <[email protected]> 
From: My Service <[email protected]> 
Reply-To: 
Subject: =?utf-8?q?Hans_M=C3=BCller_your_Ticket?= 
X-UC-Weight: [# ] 51 
X-CC-Diagnostic: Not Header "Date" Exists (51) 

--===============1598388400== 
Content-Type: text/plain; charset="us-ascii" 
MIME-Version: 1.0 
Content-Transfer-Encoding: 8bit 

Message with some chars like öäüßéèô... 
--===============1598388400==-- 

感謝您的幫助

回答

1

我們用下面的代碼:

message = mail.EmailMessage()

message.subject = 「=?UTF-8 2 B 4%s嗎?=」 %base64.b64encode(U 「UAO」 .encode( 「UTF-8」))

message.html = u」的üäö「.encode('ascii','xmlcharrefreplace')

+0

我以爲避免使用html。那麼當沒有機會時......感謝您的快速回復! – user2210643 2013-03-27 11:18:40