diff --git a/edi_mail_import_oca/README.rst b/edi_mail_import_oca/README.rst new file mode 100644 index 000000000..bf0c71b24 --- /dev/null +++ b/edi_mail_import_oca/README.rst @@ -0,0 +1,115 @@ +.. image:: https://odoo-community.org/readme-banner-image + :target: https://odoo-community.org/get-involved?utm_source=readme + :alt: Odoo Community Association + +=================== +Edi Mail Import Oca +=================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:aff4c256557f2507c286234eccc96a32869385fb0ab0fab7f5509a292ae01a43 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/license-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fedi--framework-lightgray.png?logo=github + :target: https://github.com/OCA/edi-framework/tree/19.0/edi_mail_import_oca + :alt: OCA/edi-framework +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/edi-framework-19-0/edi-framework-19-0-edi_mail_import_oca + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/edi-framework&target_branch=19.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module allows to process mails received as edi.exchange.records to +process them. + +**Table of contents** + +.. contents:: + :local: + +Use Cases / Context +=================== + +Usually, when we think about EDI entrances, we think about SFTP or API, +however, there is an standard way to receive files that is emails. + +This module tries to offer a way to reuse EDI and mail interface in +order to handle everything. + +Configuration +============= + +For handling this incoming data, you just need to create a Backend and +define the alias. + +The alias will be used to detect input mails. If the system detects an +email, it will take all the exchange types related to the backend and +review by exchange_filename if the pattern is filled. It will create a +new exchange record for each exchange type that fulfills: + +1. Has the same backend type id +2. Has no backend or the backend is the current one +3. Has mail ``Mail Record Policy`` is ``full`` (it will generate a JSON + file with the full email) or the policy is ``pattern`` and there is a + file that follows the pattern (it will be used as the file for the + exchange record) + +If not exchange type can be found, a UserError and a bounce email are +sent. + +Processing will be handled in the standard way. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* Dixmit + +Contributors +------------ + +- `Dixmit `__ + + - Enric Tobella + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/edi-framework `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/edi_mail_import_oca/__init__.py b/edi_mail_import_oca/__init__.py new file mode 100644 index 000000000..0650744f6 --- /dev/null +++ b/edi_mail_import_oca/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/edi_mail_import_oca/__manifest__.py b/edi_mail_import_oca/__manifest__.py new file mode 100644 index 000000000..2039da3b7 --- /dev/null +++ b/edi_mail_import_oca/__manifest__.py @@ -0,0 +1,17 @@ +# Copyright 2026 Dixmit +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "Edi Mail Import Oca", + "summary": """Process emails as EDI exchange recordsç""", + "version": "19.0.1.0.0", + "license": "AGPL-3", + "author": "Dixmit,Odoo Community Association (OCA)", + "website": "https://github.com/OCA/edi-framework", + "depends": ["edi_core_oca", "mail"], + "data": [ + "views/edi_backend.xml", + "views/edi_exchange_type.xml", + ], + "demo": [], +} diff --git a/edi_mail_import_oca/models/__init__.py b/edi_mail_import_oca/models/__init__.py new file mode 100644 index 000000000..f076fb171 --- /dev/null +++ b/edi_mail_import_oca/models/__init__.py @@ -0,0 +1,3 @@ +from . import edi_backend +from . import edi_exchange_record +from . import edi_exchange_type diff --git a/edi_mail_import_oca/models/edi_backend.py b/edi_mail_import_oca/models/edi_backend.py new file mode 100644 index 000000000..616127469 --- /dev/null +++ b/edi_mail_import_oca/models/edi_backend.py @@ -0,0 +1,31 @@ +# Copyright 2026 Dixmit +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +import ast + +from odoo import models + + +class EdiBackend(models.Model): + _name = "edi.backend" + _inherit = ["edi.backend", "mail.alias.mixin"] + + def _alias_get_creation_values(self): + values = super()._alias_get_creation_values() + values["alias_model_id"] = self.env["ir.model"]._get("edi.exchange.record").id + if self.id: + values["alias_defaults"] = defaults = ast.literal_eval( + self.alias_defaults or "{}" + ) + defaults["backend_id"] = self.id + return values + + def _mail_exchange_type_pending_input_domain(self): + """Domain for retrieving input exchange types for emails.""" + return [ + ("backend_type_id", "=", self.backend_type_id.id), + ("direction", "=", "input"), + "|", + ("backend_id", "=", False), + ("backend_id", "=", self.id), + ("mail_record_policy", "!=", False), + ] diff --git a/edi_mail_import_oca/models/edi_exchange_record.py b/edi_mail_import_oca/models/edi_exchange_record.py new file mode 100644 index 000000000..821c97a27 --- /dev/null +++ b/edi_mail_import_oca/models/edi_exchange_record.py @@ -0,0 +1,97 @@ +# Copyright 2026 Dixmit +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +import base64 +import email +import json +import logging +import re +from collections import defaultdict + +from odoo import api, models +from odoo.exceptions import UserError + +_logger = logging.getLogger(__name__) + + +class EdiExchangeRecord(models.Model): + _inherit = "edi.exchange.record" + + @api.model + def message_new(self, msg_dict, custom_values=None): + backend_id = custom_values.get("backend_id") if custom_values else None + data = defaultdict(list) + if backend_id and not custom_values.get("type_id"): + backend = self.env["edi.backend"].browse(backend_id) + types = self.env["edi.exchange.type"].search( + backend._mail_exchange_type_pending_input_domain() + ) + for exchange_type in types: + if exchange_type.mail_record_policy == "pattern": + for attachment in msg_dict.get("attachments", []): + if re.match( + exchange_type.exchange_filename_pattern, + attachment.fname, + re.IGNORECASE, + ): + data[exchange_type.id].append( + [ + attachment.fname, + self._process_email_attachment(attachment), + ] + ) + else: + new_message_dict = msg_dict.copy() + attachments = new_message_dict.pop("attachments", []) + new_message_dict["attachments"] = [] + for attachment in attachments: + new_message_dict["attachments"].append( + { + "info": attachment.info, + "data": self._process_email_attachment(attachment), + "fname": attachment.fname, + } + ) + data[exchange_type.id].append( + ["email_message.json", json.dumps(new_message_dict)] + ) + if (not custom_values or "type_id" not in custom_values) and not data: + raise UserError( + self.env._( + "No exchange type found for incoming email with subject '%s'", + msg_dict.get("subject"), + ) + ) + if data: + for exchange_type_id in data: + filename, content = data[exchange_type_id].pop() + custom_values["type_id"] = exchange_type_id + break + record = super().message_new( + msg_dict, + custom_values=custom_values, + ) + if data: + record._set_file_content(content) + record.exchange_filename = filename + record.edi_exchange_state = "input_received" + for exchange_type_id in data: + for filename, content in data[exchange_type_id]: + new_record = record.copy( + default={ + "type_id": exchange_type_id, + } + ) + new_record._set_file_content(content) + new_record.exchange_filename = filename + new_record.edi_exchange_state = "input_received" + return record + + @api.model + def _process_email_attachment(self, attachment): + """Process email attachment to be stored as file content.""" + data = attachment[1] + if isinstance(data, email.message.EmailMessage): + data = data.as_bytes() + if not isinstance(data, bytes): + data = str(data).encode("utf-8") + return base64.b64encode(data).decode("utf-8") diff --git a/edi_mail_import_oca/models/edi_exchange_type.py b/edi_mail_import_oca/models/edi_exchange_type.py new file mode 100644 index 000000000..2481b2c4d --- /dev/null +++ b/edi_mail_import_oca/models/edi_exchange_type.py @@ -0,0 +1,15 @@ +# Copyright 2026 Dixmit +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class EdiExchangeType(models.Model): + _inherit = "edi.exchange.type" + + mail_record_policy = fields.Selection( + [ + ("full", "Only archive the full mail message"), + ("pattern", "Match single filename pattern"), + ] + ) diff --git a/edi_mail_import_oca/pyproject.toml b/edi_mail_import_oca/pyproject.toml new file mode 100644 index 000000000..4231d0ccc --- /dev/null +++ b/edi_mail_import_oca/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/edi_mail_import_oca/readme/CONFIGURE.md b/edi_mail_import_oca/readme/CONFIGURE.md new file mode 100644 index 000000000..4a3ea4364 --- /dev/null +++ b/edi_mail_import_oca/readme/CONFIGURE.md @@ -0,0 +1,13 @@ +For handling this incoming data, you just need to create a Backend and define the alias. + +The alias will be used to detect input mails. +If the system detects an email, it will take all the exchange types related to the backend and review by exchange_filename if the pattern is filled. +It will create a new exchange record for each exchange type that fulfills: + +1. Has the same backend type id +2. Has no backend or the backend is the current one +3. Has mail `Mail Record Policy` is `full` (it will generate a JSON file with the full email) or the policy is `pattern` and there is a file that follows the pattern (it will be used as the file for the exchange record) + +If not exchange type can be found, a UserError and a bounce email are sent. + +Processing will be handled in the standard way. diff --git a/edi_mail_import_oca/readme/CONTEXT.md b/edi_mail_import_oca/readme/CONTEXT.md new file mode 100644 index 000000000..75b5eb86e --- /dev/null +++ b/edi_mail_import_oca/readme/CONTEXT.md @@ -0,0 +1,3 @@ +Usually, when we think about EDI entrances, we think about SFTP or API, however, there is an standard way to receive files that is emails. + +This module tries to offer a way to reuse EDI and mail interface in order to handle everything. \ No newline at end of file diff --git a/edi_mail_import_oca/readme/CONTRIBUTORS.md b/edi_mail_import_oca/readme/CONTRIBUTORS.md new file mode 100644 index 000000000..2c066ba7f --- /dev/null +++ b/edi_mail_import_oca/readme/CONTRIBUTORS.md @@ -0,0 +1,2 @@ +- [Dixmit](https://www.dixmit.com) + - Enric Tobella diff --git a/edi_mail_import_oca/readme/DESCRIPTION.md b/edi_mail_import_oca/readme/DESCRIPTION.md new file mode 100644 index 000000000..0c3f0ceba --- /dev/null +++ b/edi_mail_import_oca/readme/DESCRIPTION.md @@ -0,0 +1 @@ +This module allows to process mails received as edi.exchange.records to process them. diff --git a/edi_mail_import_oca/static/description/icon.png b/edi_mail_import_oca/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/edi_mail_import_oca/static/description/icon.png differ diff --git a/edi_mail_import_oca/static/description/index.html b/edi_mail_import_oca/static/description/index.html new file mode 100644 index 000000000..72a804140 --- /dev/null +++ b/edi_mail_import_oca/static/description/index.html @@ -0,0 +1,462 @@ + + + + + +README.rst + + + +
+ + + +Odoo Community Association + +
+

Edi Mail Import Oca

+ +

Beta License: AGPL-3 OCA/edi-framework Translate me on Weblate Try me on Runboat

+

This module allows to process mails received as edi.exchange.records to +process them.

+

Table of contents

+ +
+

Use Cases / Context

+

Usually, when we think about EDI entrances, we think about SFTP or API, +however, there is an standard way to receive files that is emails.

+

This module tries to offer a way to reuse EDI and mail interface in +order to handle everything.

+
+
+

Configuration

+

For handling this incoming data, you just need to create a Backend and +define the alias.

+

The alias will be used to detect input mails. If the system detects an +email, it will take all the exchange types related to the backend and +review by exchange_filename if the pattern is filled. It will create a +new exchange record for each exchange type that fulfills:

+
    +
  1. Has the same backend type id
  2. +
  3. Has no backend or the backend is the current one
  4. +
  5. Has mail Mail Record Policy is full (it will generate a JSON +file with the full email) or the policy is pattern and there is a +file that follows the pattern (it will be used as the file for the +exchange record)
  6. +
+

If not exchange type can be found, a UserError and a bounce email are +sent.

+

Processing will be handled in the standard way.

+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Dixmit
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/edi-framework project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+
+ + diff --git a/edi_mail_import_oca/tests/__init__.py b/edi_mail_import_oca/tests/__init__.py new file mode 100644 index 000000000..720a332c1 --- /dev/null +++ b/edi_mail_import_oca/tests/__init__.py @@ -0,0 +1,2 @@ +from . import test_mail_import_oca +from . import test_mail_import_oca_no_file diff --git a/edi_mail_import_oca/tests/test_mail_import_oca.py b/edi_mail_import_oca/tests/test_mail_import_oca.py new file mode 100644 index 000000000..b5ed1c255 --- /dev/null +++ b/edi_mail_import_oca/tests/test_mail_import_oca.py @@ -0,0 +1,123 @@ +# Copyright 2026 Dixmit +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +import json + +from odoo.addons.mail.tests.common import MailCommon +from odoo.addons.test_mail.data.test_mail_data import MAIL_EML_ATTACHMENT + + +class TestEmailParsing(MailCommon): + """Test email parsing and import via mail gateway""" + + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.backend_type = cls.env["edi.backend.type"].create( + { + "name": "Mail Import OCA Test Backend Type", + "code": "mail_import_oca_test_backend_type", + } + ) + cls.backend = cls.env["edi.backend"].create( + { + "name": "Mail Import OCA Test Backend", + "backend_type_id": cls.backend_type.id, + "alias_name": "edi-input", + } + ) + cls.exchange_type_01 = cls.env["edi.exchange.type"].create( + { + "name": "Test Exchange Type", + "code": "test_exchange_type_01", + "mail_record_policy": "full", + "direction": "input", + "backend_type_id": cls.backend_type.id, + "backend_id": cls.backend.id, + } + ) + cls.exchange_type_02 = cls.env["edi.exchange.type"].create( + { + "name": "Test Exchange Type", + "code": "test_exchange_type_02", + "mail_record_policy": "pattern", + "exchange_filename_pattern": ".*eml", + "direction": "input", + "backend_type_id": cls.backend_type.id, + "backend_id": cls.backend.id, + } + ) + cls.exchange_type_03 = cls.env["edi.exchange.type"].create( + { + "name": "Test Exchange Type", + "code": "test_exchange_type_03", + "mail_record_policy": "pattern", + "exchange_filename_pattern": ".*jpg", + "direction": "input", + "backend_type_id": cls.backend_type.id, + "backend_id": cls.backend.id, + } + ) + cls.exchange_type_04 = cls.env["edi.exchange.type"].create( + { + "name": "Test Exchange Type", + "code": "test_exchange_type_04", + "exchange_filename_pattern": ".*eml", + "direction": "input", + "backend_type_id": cls.backend_type.id, + "backend_id": cls.backend.id, + } + ) + cls.exchange_types = ( + cls.exchange_type_01 | cls.exchange_type_02 | cls.exchange_type_03 + ) + + def test_import_full(self): + self.assertTrue(self.backend.alias_email) + self.assertFalse( + self.env["edi.exchange.record"].search( + [ + ("type_id", "in", self.exchange_types.ids), + ] + ) + ) + mail = self.format( + MAIL_EML_ATTACHMENT, + to=self.backend.alias_email, + subject="purchase test mail", + target_model="account.move", + msg_id="", + ) + self.env["mail.thread"].message_process("mail.thread", mail) + record = self.env["edi.exchange.record"].search( + [ + ("type_id", "=", self.exchange_type_01.id), + ] + ) + self.assertTrue(record) + self.assertEqual(record.edi_exchange_state, "input_received") + file_content = record._get_file_content() + self.assertTrue(file_content) + data = json.loads(file_content) + self.assertIn("body", data) + record = self.env["edi.exchange.record"].search( + [ + ("type_id", "=", self.exchange_type_02.id), + ] + ) + self.assertTrue(record) + self.assertEqual(record.edi_exchange_state, "input_received") + self.assertEqual(record.exchange_filename, "original_msg.eml") + self.assertFalse( + self.env["edi.exchange.record"].search( + [ + ("type_id", "=", self.exchange_type_03.id), + ] + ) + ) + self.assertFalse( + self.env["edi.exchange.record"].search( + [ + ("type_id", "=", self.exchange_type_04.id), + ] + ) + ) diff --git a/edi_mail_import_oca/tests/test_mail_import_oca_no_file.py b/edi_mail_import_oca/tests/test_mail_import_oca_no_file.py new file mode 100644 index 000000000..2192bee22 --- /dev/null +++ b/edi_mail_import_oca/tests/test_mail_import_oca_no_file.py @@ -0,0 +1,65 @@ +# Copyright 2026 Dixmit +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import api +from odoo.exceptions import UserError +from odoo.tools import mute_logger + +from odoo.addons.mail.tests.common import MailCommon +from odoo.addons.test_mail.data.test_mail_data import MAIL_EML_ATTACHMENT + + +class TestEmailParsing(MailCommon): + """Test email parsing and import via mail gateway""" + + def setUp(self): + super().setUp() + self.registry_enter_test_mode() + # now we make a test cursor for self.cr + self.cr = self.registry.cursor() + self.addCleanup(self.cr.close) + self.env = api.Environment(self.cr, api.SUPERUSER_ID, {}) + self.backend_type = self.env["edi.backend.type"].create( + { + "name": "Mail Import OCA Test Backend Type", + "code": "mail_import_oca_test_backend_type", + } + ) + self.backend = self.env["edi.backend"].create( + { + "name": "Mail Import OCA Test Backend", + "backend_type_id": self.backend_type.id, + "alias_name": "edi-input", + } + ) + self.exchange_type = self.env["edi.exchange.type"].create( + { + "name": "Test Exchange Type", + "code": "test_exchange_type", + "direction": "input", + "mail_record_policy": "pattern", + "exchange_filename_pattern": ".*xml", + "backend_type_id": self.backend_type.id, + "backend_id": self.backend.id, + } + ) + + @mute_logger("odoo.addons.edi_mail_import_oca.models.edi_exchange_record") + def test_import_no_file_found(self): + self.assertTrue(self.backend.alias_email) + self.assertFalse( + self.env["edi.exchange.record"].search( + [ + ("type_id", "=", self.exchange_type.id), + ] + ) + ) + mail = self.format( + MAIL_EML_ATTACHMENT, + to=self.backend.alias_email, + subject="purchase test mail", + target_model="account.move", + msg_id="", + ) + with self.assertRaises(UserError): + self.env["mail.thread"].message_process("mail.thread", mail) diff --git a/edi_mail_import_oca/views/edi_backend.xml b/edi_mail_import_oca/views/edi_backend.xml new file mode 100644 index 000000000..07f505ebd --- /dev/null +++ b/edi_mail_import_oca/views/edi_backend.xml @@ -0,0 +1,63 @@ + + + + + edi.backend + + + + + + +
+ + + +
+ + +
+ + + +
+
+
+
+
+
diff --git a/edi_mail_import_oca/views/edi_exchange_type.xml b/edi_mail_import_oca/views/edi_exchange_type.xml new file mode 100644 index 000000000..8459c0ea0 --- /dev/null +++ b/edi_mail_import_oca/views/edi_exchange_type.xml @@ -0,0 +1,14 @@ + + + + + edi.exchange.type + + + + + + + +