diff --git a/mail_embed_image/README.rst b/mail_embed_image/README.rst new file mode 100644 index 00000000..5725e1e3 --- /dev/null +++ b/mail_embed_image/README.rst @@ -0,0 +1,78 @@ +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :target: https://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 + +================ +mail_embed_image +================ + +This module was written to extend the functionality of ... to support ... +and allow you to ... + +Installation +============ + +To install this module, you need to: + +#. do this ... + +Configuration +============= + +To configure this module, you need to: + +#. go to ... + +Usage +===== + +To use this module, you need to: + +#. go to ... +.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas + :alt: Try me on Runbot + :target: https://runbot.odoo-community.org/runbot/205/10.0 + + +Known issues / Roadmap +====================== + +* Some src attributes might contain data instead of urls. Deal with it. + +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 smashing it by providing a detailed and welcomed feedback. + +Credits +======= + +Images +------ + +* Odoo Community Association: `Icon `_. + +Contributors +------------ + +* George Daramouskas + +Do not contact contributors directly about help with questions or problems concerning this addon, but use the `community mailing list `_ or the `appropriate specialized mailinglist `_ for help, and the bug tracker linked in `Bug Tracker`_ above for technical issues. + +Maintainer +---------- + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +This module is maintained by the OCA. + +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. + +To contribute to this module, please visit https://odoo-community.org. diff --git a/mail_embed_image/__init__.py b/mail_embed_image/__init__.py new file mode 100644 index 00000000..cff20de5 --- /dev/null +++ b/mail_embed_image/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# Copyright 2019 Therp BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from . import models diff --git a/mail_embed_image/__manifest__.py b/mail_embed_image/__manifest__.py new file mode 100644 index 00000000..b4850394 --- /dev/null +++ b/mail_embed_image/__manifest__.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +# Copyright 2019 Therp BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +{ + "name": "mail_embed_image", + "version": "10.0.1.0.0", + "author": "Therp BV,Odoo Community Association (OCA)", + "license": "AGPL-3", + "category": "", + "summary": "", + "depends": [ + 'mail', + ], + "data": [ + ], + "installable": True, + "application": False, +} diff --git a/mail_embed_image/models/__init__.py b/mail_embed_image/models/__init__.py new file mode 100644 index 00000000..4278fd5d --- /dev/null +++ b/mail_embed_image/models/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# Copyright 2019 Therp BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from . import ir_mail_server diff --git a/mail_embed_image/models/ir_mail_server.py b/mail_embed_image/models/ir_mail_server.py new file mode 100644 index 00000000..3ddfdce2 --- /dev/null +++ b/mail_embed_image/models/ir_mail_server.py @@ -0,0 +1,88 @@ +# -*- coding: utf-8 -*- +# Copyright 2019 Therp BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +import requests +import uuid +from odoo import models +from odoo.addons.base.ir.ir_mail_server import encode_header_param +from lxml.html.soupparser import fromstring +from lxml import etree +from base64 import b64encode +from email.mime.base import MIMEBase +from email import Encoders + + +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=None, + attachments=None, + message_id=None, + references=None, + object_id=None, + subtype='plain', + headers=None, + body_alternative=None, + subtype_alternative='plain'): + result = super(IrMailServer, self).build_email( + email_from, + email_to, + subject, + body, + email_cc, + email_bcc, + reply_to, + attachments, + message_id, + references, + object_id, + subtype, + headers, + body_alternative, + subtype_alternative, + ) + + def replace_img_src(msg, attachments): + """replace_img_src Given a message, find it's img tags and if they + are URLs, replace them with cids + + :param msg: A email.message.Message + """ + if msg.is_multipart(): + for part in msg.get_payload(): + replace_img_src(part, attachments) + else: + if msg.get_content_subtype() == 'html': + body = msg.get_payload(decode=True) + root = fromstring(body) + for img in root.xpath('//img'): + src = img.get('src') + if src.startswith('http'): + content = requests.get(src).content + content_base64 = b64encode(content) + cid = uuid.uuid4().hex + filename_rfc2047 = encode_header_param(cid) + part = MIMEBase('application', 'octet-stream') + part.set_param('name', filename_rfc2047) + part.add_header( + 'Content-Disposition', + 'attachment', + cid=cid, + filename=filename_rfc2047, + content=content_base64, + ) + part.set_payload(content) + Encoders.encode_base64(part) + result.attach(part) + img.set('src', 'cid:%s' % (cid)) + msg.set_payload(etree.tostring(root)) + replace_img_src(result, attachments) + return result diff --git a/mail_embed_image/static/description/icon.png b/mail_embed_image/static/description/icon.png new file mode 100644 index 00000000..3a0328b5 Binary files /dev/null and b/mail_embed_image/static/description/icon.png differ diff --git a/mail_embed_image/tests/__init__.py b/mail_embed_image/tests/__init__.py new file mode 100644 index 00000000..5a437961 --- /dev/null +++ b/mail_embed_image/tests/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# Copyright 2019 Therp BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from . import test_mail_embed_image diff --git a/mail_embed_image/tests/test_mail_embed_image.py b/mail_embed_image/tests/test_mail_embed_image.py new file mode 100644 index 00000000..153a40d8 --- /dev/null +++ b/mail_embed_image/tests/test_mail_embed_image.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +# Copyright 2019 Therp BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from openerp.tests.common import TransactionCase +from lxml import etree + + +class TestMailEmbedImage(TransactionCase): + + post_install = True + at_install = False + + def test_mail_embed_image(self): + server = self.env['ir.mail_server'] + root = etree.Element('div') + etree.SubElement(root, 'img') + etree.SubElement(root, 'img') + body = etree.tostring(root) + message = server.build_email( + 'test@example.com', + 'test@example.com', + 'subject', + body, + ) + # TODO assertions