diff --git a/modules/document_incoming/document.py b/modules/document_incoming/document.py index 9e68d3a..f2a7dbc 100755 --- a/modules/document_incoming/document.py +++ b/modules/document_incoming/document.py @@ -17,6 +17,8 @@ from trytond.transaction import Transaction from trytond.wizard import Button, StateTransition, StateView, Wizard from .exceptions import DocumentIncomingSplitError +import logging +logger = logging.getLogger(__name__) if config.getboolean('document_incoming', 'filestore', default=True): file_id = 'file_id' @@ -182,11 +184,17 @@ class Incoming(DeactivableMixin, Workflow, ModelSQL, ModelView): @classmethod def from_inbound_email(cls, email_, rule): message = email_.as_dict() - active = not message.get('attachments') - active = True + attachments = message.get('attachments') + active = False data = message.get('text', message.get('html')) + logger.info("DATA_FROM_INBOUND_MAIL:%s",data) if isinstance(data, str): data = data.encode() + body = message.get('text') or message.get('html') or '' + if isinstance(body, str): + body_bytes = body.encode('utf-8') + else: + body_bytes = body document = cls( active=active, name=message.get('subject', 'No Subject'), @@ -196,13 +204,23 @@ class Incoming(DeactivableMixin, Workflow, ModelSQL, ModelView): source='inbound_email', ) children = [] - for attachment in message.get('attachments', []): + if attachments: + for attachment in attachments: + child = cls( + name=attachment['filename'] or 'data.bin', + company=rule.document_incoming_company, + data=attachment['data'], + type=rule.document_incoming_type, + source='inbound_email') + children.append(child) + else: child = cls( - name=attachment['filename'] or 'data.bin', + name='mail.txt', company=rule.document_incoming_company, - data=attachment['data'], + data=body_bytes, type=rule.document_incoming_type, - source='inbound_email') + source='inbound_email', + ) children.append(child) document.children = children document.save() diff --git a/modules/document_incoming/inbound_email.py b/modules/document_incoming/inbound_email.py index 9c96175..60ade78 100755 --- a/modules/document_incoming/inbound_email.py +++ b/modules/document_incoming/inbound_email.py @@ -4,7 +4,8 @@ from trytond.model import fields from trytond.pool import Pool, PoolMeta from trytond.pyson import Eval - +import logging +logger = logging.getLogger(__name__) class Rule(metaclass=PoolMeta): __name__ = 'inbound.email.rule' @@ -64,6 +65,8 @@ class Rule(metaclass=PoolMeta): return data = email_.as_dict() + body = data.get('text') or data.get('html') + logger.info("BODY:%s",body) attachments = data.get('attachments') or [] # 👉 S'il y avait des pièces jointes, le module standard a déjà créé les documents @@ -73,7 +76,7 @@ class Rule(metaclass=PoolMeta): return # 🚨 Aucune pièce jointe → on crée un document depuis le corps du mail - body = data.get('text') or data.get('html') + if not body: return # rien à créer diff --git a/modules/inbound_email/inbound_email.py b/modules/inbound_email/inbound_email.py index 6600971..ae44e5a 100755 --- a/modules/inbound_email/inbound_email.py +++ b/modules/inbound_email/inbound_email.py @@ -17,6 +17,9 @@ from trytond.pyson import Eval from trytond.transaction import Transaction from trytond.url import http_host +import logging +logger = logging.getLogger(__name__) + if config.getboolean('inbound_email', 'filestore', default=True): file_id = 'data_id' store_prefix = config.get('inbound_email', 'store_prefix', default=None) @@ -74,6 +77,7 @@ class Inbox(ModelSQL, ModelView): assert email_.inbox == self for rule in self.rules: if rule.match(email_.as_dict()): + logger.info("RULE_MATCHED:%s",rule) email_.rule = rule rule.run(email_) return