def decorate(message, hide_sender, sender): """ Notify the recipient about the sender rewrite. """ footer = [ '------------', 'Du kan ikke svare direkte på denne eposten.' ] if not hide_sender: footer.append( f'Opprinnelig avsender er {sender}, send svar til denne adressen.' ) footer.append( 'Denne eposten har uorginal avsender for å redusere risikoen for at ' 'meldingen oppfattes som spam.') else: footer.append( 'Opprinnelig avsender har valgt å skjule sin adresse.') footer = '\n'.join(footer) charset = message.get_content_charset() or 'us-ascii' content_type = message.get_content_type() wrap = True if not message.is_multipart() and content_type == 'text/plain': format_param = message.get_param('format') delsp = message.get_param('delsp') transfer_encoding = message.get('content-transfer-encoding') try: old_payload = message.get_payload(decode=True).decode(charset) del message['content-transfer-encoding'] footer_separator = '\n' payload = old_payload + footer_separator + footer for cset in (charset, 'utf-8'): try: message.set_payload(payload.encode(cset), cset) except UnicodeError: pass else: if format_param: message.set_param('format', format_param) if delsp: message.set_param('delsp', delsp) wrap = False break except (LookupError, UnicodeError): if transfer_encoding: del message['content-transfer-encoding'] message['Content-Transfer-Encoding'] = transfer_encoding elif message.get_content_type() == 'multipart/mixed': payload = message.get_payload() if not isinstance(payload, list): payload = [payload] mime_footer = MIMEText(footer.encode('utf-8'), 'plain', 'utf-8') mime_footer['Content-Disposition'] = 'inline' payload.append(mime_footer) message.set_payload(payload) wrap = False if not wrap: return inner = Message() for h, v in message.items(): if h.lower().startswith('content-'): inner[h] = v inner.set_payload(message.get_payload()) inner.set_unixfrom(message.get_unixfrom()) inner.preamble = message.preamble inner.epilogue = message.epilogue inner.set_default_type(message.get_default_type()) if hasattr(message, '__version__'): inner.__version__ = message.__version__ payload = [inner] mime_footer = MIMEText(footer.encode('utf-8'), 'plain', 'utf-8') mime_footer['Content-Disposition'] = 'inline' payload.append(mime_footer) message.set_payload(payload) del message['content-type'] del message['content-transfer-encoding'] del message['content-disposition'] message['Content-Type'] = 'multipart/mixed'
def decorate(message, hide_sender, sender): """ Notify the recipient about the sender rewrite. """ footer = [ "------------", "Du kan ikke svare direkte på denne eposten." ] if not hide_sender: footer.append( f"Opprinnelig avsender er {sender}, send svar til denne adressen." ) footer.append( "Denne eposten har uorginal avsender for å redusere risikoen for at " "meldingen oppfattes som spam.") else: footer.append( "Opprinnelig avsender har valgt å skjule sin adresse.") footer = "\n".join(footer) charset = message.get_content_charset() or "us-ascii" content_type = message.get_content_type() wrap = True if not message.is_multipart() and content_type == "text/plain": format_param = message.get_param("format") delsp = message.get_param("delsp") transfer_encoding = message.get("content-transfer-encoding") try: old_payload = message.get_payload(decode=True).decode(charset) del message["content-transfer-encoding"] footer_separator = "\n" payload = old_payload + footer_separator + footer for cset in (charset, "utf-8"): try: message.set_payload(payload.encode(cset), cset) except UnicodeError: pass else: if format_param: message.set_param("format", format_param) if delsp: message.set_param("delsp", delsp) wrap = False break except (LookupError, UnicodeError): if transfer_encoding: del message["content-transfer-encoding"] message["Content-Transfer-Encoding"] = transfer_encoding elif message.get_content_type() == "multipart/mixed": payload = message.get_payload() if not isinstance(payload, list): payload = [payload] mime_footer = MIMEText(footer.encode("utf-8"), "plain", "utf-8") mime_footer["Content-Disposition"] = "inline" payload.append(mime_footer) message.set_payload(payload) wrap = False if not wrap: return inner = Message() for h, v in message.items(): if h.lower().startswith("content-"): inner[h] = v inner.set_payload(message.get_payload()) inner.set_unixfrom(message.get_unixfrom()) inner.preamble = message.preamble inner.epilogue = message.epilogue inner.set_default_type(message.get_default_type()) if hasattr(message, "__version__"): inner.__version__ = message.__version__ payload = [inner] mime_footer = MIMEText(footer.encode("utf-8"), "plain", "utf-8") mime_footer["Content-Disposition"] = "inline" payload.append(mime_footer) message.set_payload(payload) del message["content-type"] del message["content-transfer-encoding"] del message["content-disposition"] message["Content-Type"] = "multipart/mixed"