This commit is contained in:
2026-02-02 08:42:48 +01:00
parent 62c2080fee
commit 7b669a27a8
3 changed files with 33 additions and 8 deletions

View File

@@ -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()

View File

@@ -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

View File

@@ -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