def _process_message(self, message: RawMessage) -> Message: """ We are overriding this method only to change receiving of message text form by calling str(message.as_bytes(), charset) instead of message.as_string() as original method does. """ from campaigns.utils import convert_header_to_unicode msg = Message() if mailbox_settings['store_original_message']: msg.eml.save('%s.eml' % uuid.uuid4(), ContentFile(message.as_string()), save=False) msg.mailbox = self if 'subject' in message: msg.subject = (convert_header_to_unicode( message['subject'])[0:255]) if 'message-id' in message: msg.message_id = message['message-id'][0:255].strip() if 'from' in message: msg.from_header = convert_header_to_unicode(message['from']) if 'to' in message: msg.to_header = convert_header_to_unicode(message['to']) elif 'Delivered-To' in message: msg.to_header = convert_header_to_unicode(message['Delivered-To']) msg.save() dehydrated_message = self._get_dehydrated_message(message, msg) try: message_context = dehydrated_message.as_string() except KeyError as e: for charset in filter(None, dehydrated_message.get_charsets()): try: message_context = str(dehydrated_message.as_bytes(), charset) break except UnicodeDecodeError: continue else: raise e msg.set_body(message_context) if dehydrated_message['in-reply-to']: msg.in_reply_to = Message.objects.filter( message_id=dehydrated_message['in-reply-to'].strip()).first() msg.save() return msg
def test_message_issue_82_bis(self): """ Ensure that the email object is good before and after calling _get_dehydrated_message() """ message = self._get_email_object('email_issue_82.eml') success = True # this is the code of _process_message() msg = Message() # if STORE_ORIGINAL_MESSAGE: # msg.eml.save('%s.eml' % uuid.uuid4(), ContentFile(message), # save=False) msg.mailbox = self.mailbox if 'subject' in message: msg.subject = convert_header_to_unicode(message['subject'])[0:255] if 'message-id' in message: msg.message_id = message['message-id'][0:255] if 'from' in message: msg.from_header = convert_header_to_unicode(message['from']) if 'to' in message: msg.to_header = convert_header_to_unicode(message['to']) elif 'Delivered-To' in message: msg.to_header = convert_header_to_unicode(message['Delivered-To']) msg.save() # here the message is ok str_msg = message.as_string() message = self.mailbox._get_dehydrated_message(message, msg) try: # here as_string raises UnicodeEncodeError str_msg = message.as_string() except: success = False msg.set_body(str_msg) if message['in-reply-to']: try: msg.in_reply_to = Message.objects.filter( message_id=message['in-reply-to'] )[0] except IndexError: pass msg.save() self.assertEqual(True, success)
def test_message_issue_82_bis(self): """ Ensure that the email object is good before and after calling _get_dehydrated_message() """ message = self._get_email_object('email_issue_82.eml') success = True # this is the code of _process_message() msg = Message() # if STORE_ORIGINAL_MESSAGE: # msg.eml.save('%s.eml' % uuid.uuid4(), ContentFile(message), save=False) msg.mailbox = self.mailbox if 'subject' in message: msg.subject = convert_header_to_unicode(message['subject'])[0:255] if 'message-id' in message: msg.message_id = message['message-id'][0:255] if 'from' in message: msg.from_header = convert_header_to_unicode(message['from']) if 'to' in message: msg.to_header = convert_header_to_unicode(message['to']) elif 'Delivered-To' in message: msg.to_header = convert_header_to_unicode(message['Delivered-To']) msg.save() #here the message is ok str_msg = message.as_string() message = self.mailbox._get_dehydrated_message(message, msg) try: # here as_string raises UnicodeEncodeError str_msg = message.as_string() except: success = False msg.set_body(str_msg) if message['in-reply-to']: try: msg.in_reply_to = Message.objects.filter( message_id=message['in-reply-to'] )[0] except IndexError: pass msg.save() self.assertEqual(True, success)