Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions mail_external_cleaner/README.rst
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/OCA/mail/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 <https://github.com/OCA/mail/issues/new?body=module:%20mail_external_cleaner%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

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

Credits
=======

Authors
-------

* Dixmit
* CreuBlanca

Contributors
------------

- `Dixmit <https://www.dixmit.com>`__

- 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 <https://github.com/OCA/mail/tree/16.0/mail_external_cleaner>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions mail_external_cleaner/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
20 changes: 20 additions & 0 deletions mail_external_cleaner/__manifest__.py
Original file line number Diff line number Diff line change
@@ -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": [],
}
3 changes: 3 additions & 0 deletions mail_external_cleaner/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import mail_mail
from . import mail_thread
from . import ir_mail_server
123 changes: 123 additions & 0 deletions mail_external_cleaner/models/ir_mail_server.py
Original file line number Diff line number Diff line change
@@ -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<mime>[^;]+);base64,(?P<b64>[A-Za-z0-9+/=\s]+)\1',
re.IGNORECASE,
)


def extract_inline_images_from_html(html, existing_names=()):
"""
Finds <img src="data:...base64,..."> 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 <img src="cid:...">
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
58 changes: 58 additions & 0 deletions mail_external_cleaner/models/mail_mail.py
Original file line number Diff line number Diff line change
@@ -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"(\<img [\w\b =\-\":;,.%]*src=\")(https?:\/\/[\w.:]+)((?:\/[\w@:%.+&~#=\/-]+)?(?:\?\S+)?)(\"[\w\b =\-\":;,.%]+\/?\>)" # 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
19 changes: 19 additions & 0 deletions mail_external_cleaner/models/mail_thread.py
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions mail_external_cleaner/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- [Dixmit](https://www.dixmit.com)
- Enric Tobella

5 changes: 5 additions & 0 deletions mail_external_cleaner/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -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.
Binary file added mail_external_cleaner/static/description/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading