diff --git a/msg_attachments_to_mail_message/models/ir_attachment.py b/msg_attachments_to_mail_message/models/ir_attachment.py index 4b5242b..8c96b43 100644 --- a/msg_attachments_to_mail_message/models/ir_attachment.py +++ b/msg_attachments_to_mail_message/models/ir_attachment.py @@ -13,8 +13,15 @@ from email import encoders import lxml.html import re from odoo.tools import email_normalize, email_split +from io import BytesIO _logger = logging.getLogger(__name__) +_msg_import_logger = logging.getLogger('msg.import') +handler = logging.FileHandler('/var/log/odoo/msg_import.log') +formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') +handler.setFormatter(formatter) +_msg_import_logger.addHandler(handler) +_msg_import_logger.setLevel(logging.ERROR) class IrAttachment(models.Model): @@ -38,6 +45,7 @@ class IrAttachment(models.Model): ) if not is_msg: + _msg_import_logger.error(f"Invalid file type for MSG processing: {self.name} (mimetype: {self.mimetype})") return False # Check file size @@ -51,6 +59,8 @@ class IrAttachment(models.Model): ) # Convert from base64 to MB if file_size_mb > max_size: + error_msg = f"File size error for {self.name}: File size ({file_size_mb:.2f} MB) exceeds maximum allowed size ({max_size} MB)" + _msg_import_logger.error(error_msg) self._notify_error( "File Size Error", _("The MSG file is too large. Maximum allowed size is %s MB") @@ -87,7 +97,7 @@ class IrAttachment(models.Model): """ try: # Read the MSG file - msg_file = extract_msg.Message(msg_data) + msg_file = extract_msg.Message(BytesIO(msg_data)) _logger.debug( "Converting MSG to EML - Subject: %s, From: %s, To: %s", msg_file.subject, @@ -133,10 +143,9 @@ class IrAttachment(models.Model): _logger.info('Created new contact %s under company %s for email %s', display_name, company.name, from_email) else: - _logger.warning('Email sender not found in partners and no current partner to link to: %s - Message ignored', from_email) - self._notify_error('Unknown Sender', - _('The message cannot be imported because the sender (%s) does not exist in partners and no company to link to') % from_email) - return False + _logger.warning('Email sender not found in partners and no current partner to link to: %s', from_email) + # Continue processing with the original sender + from_partner = None elif len(from_partners) > 1: if current_partner: # Chercher parmi les contacts liés à la compagnie de l'objet courant @@ -196,24 +205,26 @@ class IrAttachment(models.Model): if msg_file.body: body = msg_file.body - # Si ce n'est pas déjà du HTML mais contient des CIDs, convertir en HTML - if " - body = body.replace("\n", "
") - # Convertir les références CID en balises img - import re + # Check if the body is HTML or contains HTML-like content + is_html = "', - body, - ) - # Envelopper dans du HTML + if not is_html: + # Convert plain text to HTML + body = body.replace("\n", "
") + # Convert URLs to links + body = re.sub(r'(https?://[^\s<>"]+|www\.[^\s<>"]+)', r'\1', body) + # Convert CID references to img tags + if "[cid:" in body: + body = re.sub( + r"\[cid:([^\]]+)\]", + lambda m: f'{m.group(1)}', + body, + ) + # Wrap in HTML tags body = f"{body}" - # Clean HTML if needed - if "