12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- import MAPI
- import email
- from email.utils import getaddresses
- from email.header import decode_header
- from MAPI.Util import *
- from plugintemplates import *
- class RewriteGALAddressesToSMTP(IMapiDAgentPlugin):
-
- def __init__(self, logger):
- IMapiDAgentPlugin.__init__(self, logger)
- def DecodeHeaderToUTF8(self, header) -> str:
- decodedHeaderParts = decode_header(header)
- strHeader = ""
- for headerText, headerEncoding in decodedHeaderParts:
- if headerEncoding is None:
- if isinstance(headerText, str):
- strHeader += headerText
- else:
- strHeader += headerText.decode('ASCII')
- else:
- strHeader += headerText.decode(headerEncoding)
- return strHeader
- def GetKopanoRecipient(self, emailAddr: bytes, kopanoRecipients: list) -> list:
- for rcpt in kopanoRecipients:
- if SPropValue(PR_EMAIL_ADDRESS, emailAddr) in rcpt:
- return rcpt
- return None
-
- def PreDelivery(self, session, addrbook, store, folder, message):
-
- headers = message.GetProps([PR_TRANSPORT_MESSAGE_HEADERS], 0)[0].Value
- msg = email.message_from_bytes(headers)
- to_addrs = getaddresses(msg.get_all('to', []))
- cc_addrs = getaddresses(msg.get_all('cc', []))
-
- orig_to_addrs = []
- orig_cc_addrs = []
- rcptTable = message.GetRecipientTable(0)
-
- origRcpts = rcptTable.QueryRows(10, 0)
- while origRcpts and len(origRcpts) > 0:
- for origRcpt in origRcpts:
- if SPropValue(PR_RECIPIENT_TYPE, MAPI_TO) in origRcpt:
- orig_to_addrs.append(origRcpt)
- if SPropValue(PR_RECIPIENT_TYPE, MAPI_CC) in origRcpt:
- orig_cc_addrs.append(origRcpt)
- origRcpts = rcptTable.QueryRows(10, 0)
-
- names = []
- for addr in to_addrs:
- kopanoRcpt = self.GetKopanoRecipient(bytes(addr[1], encoding='utf8'), orig_to_addrs)
- if kopanoRcpt is not None:
- names.append(kopanoRcpt)
- else:
- names.append([
- SPropValue(PR_RECIPIENT_TYPE, MAPI_TO),
- SPropValue(PR_DISPLAY_NAME, bytes(self.DecodeHeaderToUTF8(addr[0]), encoding="utf8")),
- SPropValue(PR_ADDRTYPE, bytes('SMTP', encoding="utf8")),
- SPropValue(PR_EMAIL_ADDRESS, bytes(addr[1], encoding='utf8')),
- ])
- for addr in cc_addrs:
- kopanoRcpt = self.GetKopanoRecipient(bytes(addr[1], encoding='utf8'), orig_cc_addrs)
- if kopanoRcpt is not None:
- names.append(kopanoRcpt)
- else:
- names.append([
- SPropValue(PR_RECIPIENT_TYPE, MAPI_CC),
- SPropValue(PR_DISPLAY_NAME, bytes(self.DecodeHeaderToUTF8(addr[0]), encoding="utf8")),
- SPropValue(PR_ADDRTYPE, bytes('SMTP', encoding="utf8")),
- SPropValue(PR_EMAIL_ADDRESS, bytes(addr[1], encoding='utf8')),
- ])
- if len(names) > 0:
- message.ModifyRecipients(0, names)
- return MP_CONTINUE,
|