From 2e1aaf6feb879307d7caafb6686fef093a0177a0 Mon Sep 17 00:00:00 2001 From: h44z Date: Fri, 16 Jun 2017 10:44:33 +0000 Subject: [PATCH] Fixed recipient name decoding Use decode_headers to decode the recipient name (otherwise it will display something like =?utf-8?Q?Max_Muster?=) --- RewriteGALAddressesToSMTP.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/RewriteGALAddressesToSMTP.py b/RewriteGALAddressesToSMTP.py index 8ea1279..66c74b9 100644 --- a/RewriteGALAddressesToSMTP.py +++ b/RewriteGALAddressesToSMTP.py @@ -6,6 +6,7 @@ import MAPI import email from email.utils import getaddresses +from email.header import decode_header from MAPI.Util import * from plugintemplates import * @@ -15,6 +16,14 @@ class RewriteGALAddressesToSMTP(IMapiDAgentPlugin): def __init__(self, logger): IMapiDAgentPlugin.__init__(self, logger) + def DecodeHeaderToUTF8(self, header): + decodedHeader = decode_header(header) + headerText, headerEncoding = decodedHeader[0] + if headerEncoding is None: + return unicode(headerText) + else: + return unicode(headerText, headerEncoding, 'ignore') + def PreDelivery(self, session, addrbook, store, folder, message): headers = message.GetProps([PR_TRANSPORT_MESSAGE_HEADERS], 0)[0].Value msg = email.message_from_string(headers) @@ -25,7 +34,7 @@ class RewriteGALAddressesToSMTP(IMapiDAgentPlugin): for addr in to_addrs: names.append([ SPropValue(PR_RECIPIENT_TYPE, MAPI_TO), - SPropValue(PR_DISPLAY_NAME_W, unicode(addr[0])), + SPropValue(PR_DISPLAY_NAME_W, self.DecodeHeaderToUTF8(addr[0])), SPropValue(PR_ADDRTYPE, 'SMTP'), SPropValue(PR_EMAIL_ADDRESS, unicode(addr[1])), ]) @@ -33,7 +42,7 @@ class RewriteGALAddressesToSMTP(IMapiDAgentPlugin): for addr in cc_addrs: names.append([ SPropValue(PR_RECIPIENT_TYPE, MAPI_CC), - SPropValue(PR_DISPLAY_NAME_W, unicode(addr[0])), + SPropValue(PR_DISPLAY_NAME_W, self.DecodeHeaderToUTF8(addr[0])), SPropValue(PR_ADDRTYPE, 'SMTP'), SPropValue(PR_EMAIL_ADDRESS, unicode(addr[1])), ])