-
-
Notifications
You must be signed in to change notification settings - Fork 623
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[16.0][MIG] mail_embed_image module #1499
Draft
StephaneMangin
wants to merge
19
commits into
OCA:16.0
Choose a base branch
from
camptocamp:cid_content_change_and_data_embeddings
base: 16.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
30707b9
[ADD] mail_embed_image module
99f9171
[FIX] solves SMTP line limit error [maximum allowed line length is 998
gfcapalbo 8a93cfc
[REF] refactor and improvements
gfcapalbo 04ccb02
[FIX]
gfcapalbo fb8453b
[FIX]
gfcapalbo 34ffe82
[add] debug feedback
gfcapalbo 830a6a6
f
gfcapalbo 52ec4d4
[FIX] exacly reproduce super's default values
hbrunn 2317908
[IMP] depend on base
hbrunn 05c44bb
[FIX] run tests at_install
hbrunn 2985c8c
[FIX] improve error handling
hbrunn 44ca23c
[FIX] make tests more resilient against different testing envs
hbrunn ba8dd69
[UPD] Update mail_embed_image.pot
oca-travis bfb6936
[UPD] README.rst
OCA-git-bot 0335a0f
Apply dotfiles
TDu 1663aee
mail_embed_image 10.0.1.0.1
OCA-git-bot 3a7114e
[UPD] README.rst
OCA-git-bot ff2514d
[16.0][MIG] migrating to 16.0
f81e4c2
[16.0][IMP] mail_embed_image: improve embedding type:
StephaneMangin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
================ | ||
Mail Embed Image | ||
================ | ||
|
||
.. | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!! This file is generated by oca-gen-addon-readme !! | ||
!! changes will be overwritten. !! | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!! source digest: sha256:a365995cc3558fa6f105e5354c6a4317efd6453f04a5647e0acdff4c5adb3c12 | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
|
||
.. |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/licence-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%2Fsocial-lightgray.png?logo=github | ||
:target: https://github.com/OCA/social/tree/16.0/mail_embed_image | ||
:alt: OCA/social | ||
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png | ||
:target: https://translation.odoo-community.org/projects/social-16-0/social-16-0-mail_embed_image | ||
: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/social&target_branch=16.0 | ||
:alt: Try me on Runboat | ||
|
||
|badge1| |badge2| |badge3| |badge4| |badge5| | ||
|
||
This module finds images attached to outgoing emails and replaces their urls | ||
with cids. This will avoid rendering issues with some email clients. | ||
|
||
It also provides 2 options to embed internal URL images in a mail body: | ||
- CIDs: add fileparts as CIDs | ||
- Data URLs: add images as data URLs | ||
|
||
This option is configurable in an company settings variables. | ||
|
||
**Table of contents** | ||
|
||
.. contents:: | ||
:local: | ||
|
||
Bug Tracker | ||
=========== | ||
|
||
Bugs are tracked on `GitHub Issues <https://github.com/OCA/social/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/social/issues/new?body=module:%20mail_embed_image%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 | ||
~~~~~~~ | ||
|
||
* Therp BV | ||
|
||
Contributors | ||
~~~~~~~~~~~~ | ||
|
||
* George Daramouskas <[email protected]> | ||
* Giovanni Francesco Capalbo <[email protected]> | ||
* Italo LOPES <[email protected]> | ||
* Stéphane Mangin <[email protected]> | ||
|
||
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/social <https://github.com/OCA/social/tree/16.0/mail_embed_image>`_ project on GitHub. | ||
|
||
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Copyright 2019 Therp BV <https://therp.nl> | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). | ||
from . import models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Copyright 2019 Therp BV <https://therp.nl> | ||
# Copyright 2024 Camptocamp SA | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). | ||
{ | ||
"name": "Mail Embed Image", | ||
"version": "16.0.1.0.0", | ||
"author": "Therp BV,Odoo Community Association (OCA)", | ||
"license": "AGPL-3", | ||
"category": "Social", | ||
"summary": "Replace img.src's which start with http with inline cids", | ||
"website": "https://github.com/OCA/social", | ||
"depends": [ | ||
"mail", | ||
"web", | ||
], | ||
"data": [ | ||
"views/res_config_settings_views.xml", | ||
], | ||
"installable": True, | ||
"application": False, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Translation of Odoo Server. | ||
# This file contains the translation of the following modules: | ||
# * mail_embed_image | ||
# | ||
msgid "" | ||
msgstr "" | ||
"Project-Id-Version: Odoo Server 10.0\n" | ||
"Report-Msgid-Bugs-To: \n" | ||
"Last-Translator: <>\n" | ||
"Language-Team: \n" | ||
"MIME-Version: 1.0\n" | ||
"Content-Type: text/plain; charset=UTF-8\n" | ||
"Content-Transfer-Encoding: \n" | ||
"Plural-Forms: \n" | ||
|
||
#. module: mail_embed_image | ||
#: model:ir.model,name:mail_embed_image.model_ir_mail_server | ||
msgid "ir.mail_server" | ||
msgstr "" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Copyright 2019 Therp BV <https://therp.nl> | ||
# Copyright 2024 Camptocamp SA | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). | ||
from . import ir_mail_server | ||
from . import company | ||
from . import res_config_settings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Copyright 2024 Camptocamp SA | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
from odoo import fields, models | ||
|
||
|
||
class ResCompany(models.Model): | ||
_inherit = "res.company" | ||
|
||
image_embedding_method = fields.Selection( | ||
selection=[ | ||
("none", "No postprocessing"), | ||
("cid", "Content-ID (Gmail, Office compatible)"), | ||
("data", "HTML Inline Data"), | ||
], | ||
default="cid", | ||
required=True, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
# Copyright 2019 Therp BV <https://therp.nl> | ||
# Copyright 2024 Camptocamp SA | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
|
||
import logging | ||
import uuid | ||
from base64 import b64encode | ||
from email.mime.image import MIMEImage | ||
from email.mime.multipart import MIMEMultipart | ||
|
||
import requests | ||
from lxml.html import fromstring, tostring | ||
|
||
from odoo import models | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
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", | ||
): | ||
image_embedding_method = self.env.company.image_embedding_method | ||
fileparts = None | ||
if subtype == "html" and image_embedding_method != "none": | ||
body, fileparts = self._build_email_replace_img_src(body) | ||
|
||
# TODO check if we can add attachments here. | ||
result = super(IrMailServer, self).build_email( | ||
email_from=email_from, | ||
email_to=email_to, | ||
subject=subject, | ||
body=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, | ||
) | ||
if fileparts: | ||
# Multipart method MUST be multipart/related for CIDs embedding | ||
# Gmail and Office won't process the images otherwise | ||
if image_embedding_method == "cid": | ||
result.set_type("multipart/related") | ||
for fpart in fileparts: | ||
result.attach(fpart) | ||
# after all part where added, we need to reorganize the parts | ||
# | ||
# Before: | ||
# - boundary 1 | ||
# - text/plain | ||
# - text/html | ||
# - image/png | ||
# After: | ||
# - boundary 1 | ||
# - multipart/alternative | ||
# - boundary 2 | ||
# - text/plain | ||
# - text/html | ||
# - image/png | ||
# If an attachment is present, the parts are already in the right | ||
# order in this case, we don't need to reorganize the parts | ||
# but if we find later text/plain or text/html parts, we will need | ||
# to append them to the first multipart/alternative. | ||
# | ||
# It possible to have multiple parts of type multipart/alternative, | ||
# but it's not a common case. | ||
all_parts = [] | ||
for part in result.iter_parts(): | ||
if part.get_content_type() == "multipart/alternative": | ||
all_parts.append(part) | ||
|
||
if not all_parts: | ||
all_parts = [MIMEMultipart("alternative")] | ||
|
||
for part in result.iter_parts(): | ||
if part.get_content_type() in ["text/html", "text/plain"]: | ||
all_parts[0].attach(part) | ||
elif part.get_content_type() == "multipart/alternative": | ||
pass | ||
else: | ||
all_parts.append(part) | ||
result.set_payload(all_parts) | ||
return result | ||
|
||
def _build_email_replace_img_src(self, html_body): | ||
"""Replace img src with base64 encoded image.""" | ||
if not html_body: | ||
return html_body | ||
|
||
base_url = self.env["ir.config_parameter"].get_param("web.base.url") | ||
image_embedding_method = self.env.company.image_embedding_method | ||
root = fromstring(html_body) | ||
fileparts = [] | ||
# Limit results to only internal resources to avoid malicious external | ||
# image injections | ||
for img in root.xpath( | ||
".//img[starts-with(@src, '%s')]" | ||
"| .//img[starts-with(@src, '/web/image')]" % (base_url) | ||
): | ||
image_path = img.get("src") | ||
try: | ||
response = requests.get(image_path, timeout=10) | ||
_logger.debug("Fetching image from %s", image_path) | ||
if response.status_code == 200: | ||
image_content = response.content | ||
filepart = MIMEImage(image_content) | ||
if image_embedding_method == "data": | ||
raw_content = filepart.get_payload(decode=True) | ||
base_64_content = b64encode(raw_content).decode("utf-8") | ||
mimetype = filepart.get_content_type() | ||
img.set("src", f"data:{mimetype};base64,{base_64_content}") | ||
elif image_embedding_method == "cid": | ||
cid = uuid.uuid4().hex | ||
# convert cid to rfc2047 encoding | ||
filename_encoded = "=?utf-8?b?%s?=" % b64encode( | ||
cid.encode("utf-8") | ||
).decode("utf-8") | ||
filepart.add_header("Content-ID", f"<{cid}>") | ||
filepart.add_header( | ||
"Content-Disposition", | ||
"inline", | ||
filename=filename_encoded, | ||
) | ||
img.set("src", f"cid:{cid}") | ||
fileparts.append(filepart) | ||
else: | ||
_logger.warning( | ||
"Could not get %s: HTTP status code %s", | ||
img.get("src"), | ||
response.status_code, | ||
) | ||
except Exception as e: | ||
_logger.warning("Could not get %s: %s", img.get("src"), str(e)) | ||
return tostring(root, encoding="unicode"), fileparts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Copyright 2024 Camptocamp SA | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
from odoo import fields, models | ||
|
||
|
||
class ResConfigSettings(models.TransientModel): | ||
_inherit = "res.config.settings" | ||
|
||
image_embedding_method = fields.Selection( | ||
related="company_id.image_embedding_method", | ||
readonly=False, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
* George Daramouskas <[email protected]> | ||
* Giovanni Francesco Capalbo <[email protected]> | ||
* Italo LOPES <[email protected]> | ||
* Stéphane Mangin <[email protected]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
This module finds images attached to outgoing emails and replaces their urls | ||
with cids. This will avoid rendering issues with some email clients. | ||
|
||
It also provides 2 options to embed internal URL images in a mail body: | ||
- CIDs: add fileparts as CIDs | ||
- Data URLs: add images as data URLs | ||
|
||
This option is configurable in an company settings variables. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add a test case where we have an image with a relative url, I believe this will fail here