diff --git a/mail_external_cleaner/README.rst b/mail_external_cleaner/README.rst new file mode 100644 index 000000000..cc66e38c7 --- /dev/null +++ b/mail_external_cleaner/README.rst @@ -0,0 +1,89 @@ +.. image:: https://odoo-community.org/readme-banner-image + :target: https://odoo-community.org/get-involved?utm_source=readme + :alt: Odoo Community Association + +===================== +Mail External Cleaner +===================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:1c85e0de80c033bfd956777cbb17624817cc912e71041b8bda835cbcc1d1b0ca + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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%2Fmail-lightgray.png?logo=github + :target: https://github.com/OCA/mail/tree/16.0/mail_external_cleaner + :alt: OCA/mail +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/mail-16-0/mail-16-0-mail_external_cleaner + :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/mail&target_branch=16.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module removes all the link images on the mail and replaces it by +the source image. + +This has sense when Our Odoo instance is not open to our customers. + +Also removes portal headers and so on and keeps it only for internal +users. + +**Table of contents** + +.. contents:: + :local: + +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 +* CreuBlanca + +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/mail `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/mail_external_cleaner/__init__.py b/mail_external_cleaner/__init__.py new file mode 100644 index 000000000..0650744f6 --- /dev/null +++ b/mail_external_cleaner/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/mail_external_cleaner/__manifest__.py b/mail_external_cleaner/__manifest__.py new file mode 100644 index 000000000..ed56b0064 --- /dev/null +++ b/mail_external_cleaner/__manifest__.py @@ -0,0 +1,20 @@ +# Copyright 2025 Dixmit +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "Mail External Cleaner", + "summary": """ + Clean up email content by removing unnecessary external resources. + Makes sense for isolated environments with emails. + """, + "version": "16.0.1.0.0", + "license": "AGPL-3", + "author": "Dixmit,CreuBlanca,Odoo Community Association (OCA)", + "website": "https://github.com/OCA/mail", + "depends": [ + "mail", + "portal", + ], + "data": [], + "demo": [], +} diff --git a/mail_external_cleaner/models/__init__.py b/mail_external_cleaner/models/__init__.py new file mode 100644 index 000000000..9a3c30ed9 --- /dev/null +++ b/mail_external_cleaner/models/__init__.py @@ -0,0 +1,3 @@ +from . import mail_mail +from . import mail_thread +from . import ir_mail_server diff --git a/mail_external_cleaner/models/ir_mail_server.py b/mail_external_cleaner/models/ir_mail_server.py new file mode 100644 index 000000000..805724906 --- /dev/null +++ b/mail_external_cleaner/models/ir_mail_server.py @@ -0,0 +1,123 @@ +import base64 +import itertools +import mimetypes +import re + +from odoo import models + +_DATA_URI_RE = re.compile( + r'src=(["\'])data:(?P[^;]+);base64,(?P[A-Za-z0-9+/=\s]+)\1', + re.IGNORECASE, +) + + +def extract_inline_images_from_html(html, existing_names=()): + """ + Finds and returns: + new_html, inline_attachments, inline_names + where: + - inline_attachments: list of (filename, bytes, mime) to append to your attachments + - inline_names: set of the filenames we generated (these will become CIDs) + """ + counter = itertools.count(1) + existing = {n for n in existing_names if n} + inline_attachments = [] + inline_names = set() + + def repl(m): + mime = m.group("mime").strip().lower() + ext = mimetypes.guess_extension(mime) or ".bin" + # unique filename we’ll also use as the CID + while True: + name = f"inline-{next(counter)}{ext}" + if name not in existing: + break + b64 = re.sub(r"\s+", "", m.group("b64")) + data = base64.b64decode(b64) + inline_attachments.append((name, data, mime)) + inline_names.add(name) + return f'src="cid:{name}"' # noqa: E231 + + new_html = _DATA_URI_RE.sub(repl, html) + return new_html, inline_attachments, inline_names + + +class IrMailServer(models.Model): + _inherit = "ir.mail_server" + + def build_email( + self, + email_from, + email_to, + subject, + body, + email_cc=None, + email_bcc=None, + reply_to=False, + attachments=None, + message_id=None, + references=None, + object_id=False, + subtype="plain", + headers=None, + body_alternative=None, + subtype_alternative="plain", + ): + existing_names = [ + a[0] for a in (attachments or []) if isinstance(a, (list, tuple)) and a + ] + if subtype == "html" and body: + body, inline_atts, inline_names = extract_inline_images_from_html( + body, existing_names + ) + else: + inline_atts, inline_names = [], set() + # Append our inline attachments to your normal attachments (no function change) + attachments = (attachments or []) + inline_atts + msg = super().build_email( + email_from, + email_to, + subject, + body, + email_cc=email_cc, + email_bcc=email_bcc, + reply_to=reply_to, + attachments=attachments, + message_id=message_id, + references=references, + object_id=object_id, + subtype=subtype, + headers=headers, + body_alternative=body_alternative, + subtype_alternative=subtype_alternative, + ) + for part in msg.iter_attachments(): + fname = part.get_filename() + if fname and fname in inline_names: + # Content-ID used by + if "Content-ID" in part: + del part["Content-ID"] + part.add_header("Content-ID", f"<{fname}>") + + # Inline disposition so clients don’t show them as regular downloads + if part.get("Content-Disposition"): + part.replace_header( + "Content-Disposition", + f'inline; filename="{fname}"', # noqa: E702 + ) + else: + part.add_header( + "Content-Disposition", + f'inline; filename="{fname}"', # noqa: E702 + ) + + # Ensure base64 transfer encoding (usually already set by add_attachment) + cte = (part.get("Content-Transfer-Encoding") or "").lower() + if cte != "base64": + payload = part.get_payload(decode=True) + part.set_payload(base64.b64encode(payload).decode("ascii")) + if cte: + part.replace_header("Content-Transfer-Encoding", "base64") + else: + part.add_header("Content-Transfer-Encoding", "base64") + return msg diff --git a/mail_external_cleaner/models/mail_mail.py b/mail_external_cleaner/models/mail_mail.py new file mode 100644 index 000000000..153c9f282 --- /dev/null +++ b/mail_external_cleaner/models/mail_mail.py @@ -0,0 +1,58 @@ +# Copyright 2025 Dixmit +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +import base64 +import io +import re + +from odoo import models +from odoo.tools.mimetypes import guess_mimetype + +IMAGE_REGEX = r"(\)" # noqa: B950 + + +class MailMail(models.Model): + + _inherit = "mail.mail" + + def _send_prepare_body(self): + body = super()._send_prepare_body() + return self._embed_images(body) + + def _get_image_attachment(self, path): + if m := re.match(r"\/logo\.png\?company=(\d+)", path): + company = self.env["res.company"].browse(int(m.group(1))) + image_base64 = base64.b64decode(company.logo_web) + io.BytesIO(image_base64) + mimetype = guess_mimetype(image_base64, default="image/png") + imgext = "." + mimetype.split("/")[1] + if imgext == ".svg+xml": + imgext = ".svg" + return mimetype, company.logo_web.decode("utf-8") + if m := re.match(r"\/web\/image\/(\d+)", path): + image = self.env["ir.attachment"].browse(int(m.group(1))) + if image.exists(): + return image.mimetype, image.datas.decode("utf-8") + + def _embed_images(self, body): + base_url = ( + self.env["ir.config_parameter"] + .sudo() + .get_param("web.base.url", default="http://localhost:8069") + ) + for data in set( + re.findall( + IMAGE_REGEX, + body, + ) + ): + pre_data, url, path, post_data = data + if url == base_url: + img_data = f"{pre_data}{url}{path}{post_data}" + attachment = self._get_image_attachment(path) + if attachment: + body = body.replace( + img_data, + f"{pre_data}data:{attachment[0]};base64,{attachment[1]}{post_data}", # noqa: E231, E702, B950 + ) + return body diff --git a/mail_external_cleaner/models/mail_thread.py b/mail_external_cleaner/models/mail_thread.py new file mode 100644 index 000000000..e7060fcd1 --- /dev/null +++ b/mail_external_cleaner/models/mail_thread.py @@ -0,0 +1,19 @@ +# Copyright 2025 Dixmit +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import models + + +class MailThread(models.AbstractModel): + + _inherit = "mail.thread" + + def _notify_get_recipients_groups(self, msg_vals=None): + """ + Override to disable portal customer button access in email notifications. + """ + groups = super()._notify_get_recipients_groups(msg_vals=msg_vals) + for group, _group_func, group_vals in groups: + if group == "portal_customer": + group_vals["has_button_access"] = False + return groups diff --git a/mail_external_cleaner/readme/CONTRIBUTORS.md b/mail_external_cleaner/readme/CONTRIBUTORS.md new file mode 100644 index 000000000..4caf9c08f --- /dev/null +++ b/mail_external_cleaner/readme/CONTRIBUTORS.md @@ -0,0 +1,3 @@ +- [Dixmit](https://www.dixmit.com) + - Enric Tobella + \ No newline at end of file diff --git a/mail_external_cleaner/readme/DESCRIPTION.md b/mail_external_cleaner/readme/DESCRIPTION.md new file mode 100644 index 000000000..ab4214583 --- /dev/null +++ b/mail_external_cleaner/readme/DESCRIPTION.md @@ -0,0 +1,5 @@ +This module removes all the link images on the mail and replaces it by the source image. + +This has sense when Our Odoo instance is not open to our customers. + +Also removes portal headers and so on and keeps it only for internal users. \ No newline at end of file diff --git a/mail_external_cleaner/static/description/icon.png b/mail_external_cleaner/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/mail_external_cleaner/static/description/icon.png differ diff --git a/mail_external_cleaner/static/description/index.html b/mail_external_cleaner/static/description/index.html new file mode 100644 index 000000000..1ec12b9b0 --- /dev/null +++ b/mail_external_cleaner/static/description/index.html @@ -0,0 +1,437 @@ + + + + + +README.rst + + + +
+ + + +Odoo Community Association + +
+

Mail External Cleaner

+ +

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

+

This module removes all the link images on the mail and replaces it by +the source image.

+

This has sense when Our Odoo instance is not open to our customers.

+

Also removes portal headers and so on and keeps it only for internal +users.

+

Table of contents

+ +
+

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
  • +
  • CreuBlanca
  • +
+
+
+

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/mail project on GitHub.

+

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

+
+
+
+
+ + diff --git a/mail_external_cleaner/tests/__init__.py b/mail_external_cleaner/tests/__init__.py new file mode 100644 index 000000000..930a333fe --- /dev/null +++ b/mail_external_cleaner/tests/__init__.py @@ -0,0 +1 @@ +from . import test_mail_external_cleaner diff --git a/mail_external_cleaner/tests/portal_fake_model.py b/mail_external_cleaner/tests/portal_fake_model.py new file mode 100644 index 000000000..39b8e6397 --- /dev/null +++ b/mail_external_cleaner/tests/portal_fake_model.py @@ -0,0 +1,15 @@ +# Copyright 2025 Dixmit +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class PortalFakeModel(models.Model): + _name = "portal.fake.model" + _inherit = ["portal.mixin", "mail.thread", "mail.activity.mixin"] + _description = "Portal Fake Model" + + name = fields.Char() + partner_id = fields.Many2one( + comodel_name="res.partner", + ) diff --git a/mail_external_cleaner/tests/test_mail_external_cleaner.py b/mail_external_cleaner/tests/test_mail_external_cleaner.py new file mode 100644 index 000000000..0548d05eb --- /dev/null +++ b/mail_external_cleaner/tests/test_mail_external_cleaner.py @@ -0,0 +1,104 @@ +# Copyright 2025 Dixmit +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +import base64 + +from odoo_test_helper import FakeModelLoader + +from odoo.addons.mail.tests.common import MailCommon + + +class TestMailExternalCleaner(MailCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.loader = FakeModelLoader(cls.env, cls.__module__) + cls.loader.backup_registry() + cls.addClassCleanup(cls.loader.restore_registry) + from .portal_fake_model import PortalFakeModel + + cls.loader.update_registry((PortalFakeModel,)) + cls.partner = cls.env["res.partner"].create( + { + "name": "Partner Test", + "email": "partner_test@test.example.com", + "company_id": cls.env.company.id, + } + ) + cls.portal = cls.env["portal.fake.model"].create( + { + "name": "Portal Test", + "partner_id": cls.partner.id, + } + ) + cls.attachment = cls.env["ir.attachment"].create( + { + "name": "test.txt", + "datas": base64.b64encode(b"test attachment data"), + "mimetype": "text/plain", + } + ) + cls.url = ( + cls.env["ir.config_parameter"] + .sudo() + .get_param("web.base.url", default="http://localhost:8069") + ) + + def test_company_logo(self): + image_src = f"{self.url}/logo.png?company={self.env.company.id}" + with self.mock_mail_gateway(): + self.portal.message_post( + body=f'

Checking My Image

', + subject="Test Email with External Image", + partner_ids=self.partner.ids, + attachments=[(self.attachment.name, self.attachment.datas)], + ) + self.assertEqual(len(self._mails), 1) + self.assertNotIn(self.url, self._mails[0]["body_alternative"]) + + def test_image_attachment(self): + image = self.env["ir.attachment"].create( + { + "name": "test_image.png", + "datas": base64.b64encode(b"test image data"), + "mimetype": "image/png", + } + ) + image_src = f"{self.url}/web/image/{image.id}" + with self.mock_mail_gateway(): + self.portal.message_post( + body=f'

Checking My Image

', + subject="Test Email with External Image", + partner_ids=self.partner.ids, + attachments=[(self.attachment.name, self.attachment.datas)], + ) + self.assertEqual(len(self._mails), 1) + self.assertNotIn(self.url, self._mails[0]["body_alternative"]) + + def test_other_images(self): + """Should not replace as we don't know how to handle them.""" + image_src = f"{self.url}/image.png" + with self.mock_mail_gateway(): + self.portal.message_post( + body=f'

Checking My Image

', + subject="Test Email with External Image", + partner_ids=self.partner.ids, + attachments=[(self.attachment.name, self.attachment.datas)], + ) + self.assertEqual(len(self._mails), 1) + self.assertIn(self.url, self._mails[0]["body_alternative"]) + + def test_outside_images(self): + """Should not replace them.""" + image_src = "http://localhostlocalhost:8069/image.png" + with self.mock_mail_gateway(): + self.portal.message_post( + body=f'

Checking My Image

', + subject="Test Email with External Image", + partner_ids=self.partner.ids, + attachments=[(self.attachment.name, self.attachment.datas)], + ) + self.assertEqual(len(self._mails), 1) + self.assertIn( + "http://localhostlocalhost:8069", self._mails[0]["body_alternative"] + ) diff --git a/setup/.setuptools-odoo-make-default-ignore b/setup/.setuptools-odoo-make-default-ignore new file mode 100644 index 000000000..207e61533 --- /dev/null +++ b/setup/.setuptools-odoo-make-default-ignore @@ -0,0 +1,2 @@ +# addons listed in this file are ignored by +# setuptools-odoo-make-default (one addon per line) diff --git a/setup/README b/setup/README new file mode 100644 index 000000000..a63d633e8 --- /dev/null +++ b/setup/README @@ -0,0 +1,2 @@ +To learn more about this directory, please visit +https://pypi.python.org/pypi/setuptools-odoo diff --git a/setup/mail_external_cleaner/odoo/addons/mail_external_cleaner b/setup/mail_external_cleaner/odoo/addons/mail_external_cleaner new file mode 120000 index 000000000..a78f52619 --- /dev/null +++ b/setup/mail_external_cleaner/odoo/addons/mail_external_cleaner @@ -0,0 +1 @@ +../../../../mail_external_cleaner \ No newline at end of file diff --git a/setup/mail_external_cleaner/setup.py b/setup/mail_external_cleaner/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/mail_external_cleaner/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/test-requirements.txt b/test-requirements.txt new file mode 100644 index 000000000..66bc2cbae --- /dev/null +++ b/test-requirements.txt @@ -0,0 +1 @@ +odoo_test_helper