Browse Source

[ADD] mail_embed_image module

pull/404/head
George Daramouskas 5 years ago
parent
commit
d229817c5a
No known key found for this signature in database GPG Key ID: 5B4EF742F8CD859C
  1. 78
      mail_embed_image/README.rst
  2. 4
      mail_embed_image/__init__.py
  3. 18
      mail_embed_image/__manifest__.py
  4. 4
      mail_embed_image/models/__init__.py
  5. 88
      mail_embed_image/models/ir_mail_server.py
  6. BIN
      mail_embed_image/static/description/icon.png
  7. 4
      mail_embed_image/tests/__init__.py
  8. 25
      mail_embed_image/tests/test_mail_embed_image.py

78
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
<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 smashing it by providing a detailed and welcomed feedback.
Credits
=======
Images
------
* Odoo Community Association: `Icon <https://github.com/OCA/maintainer-tools/blob/master/template/module/static/description/icon.svg>`_.
Contributors
------------
* George Daramouskas <gdaramouskas@therp.nl>
Do not contact contributors directly about help with questions or problems concerning this addon, but use the `community mailing list <mailto:community@mail.odoo.com>`_ or the `appropriate specialized mailinglist <https://odoo-community.org/groups>`_ 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.

4
mail_embed_image/__init__.py

@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# Copyright 2019 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from . import models

18
mail_embed_image/__manifest__.py

@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
# Copyright 2019 Therp BV <https://therp.nl>
# 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,
}

4
mail_embed_image/models/__init__.py

@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# Copyright 2019 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from . import ir_mail_server

88
mail_embed_image/models/ir_mail_server.py

@ -0,0 +1,88 @@
# -*- coding: utf-8 -*-
# Copyright 2019 Therp BV <https://therp.nl>
# 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

BIN
mail_embed_image/static/description/icon.png

After

Width: 128  |  Height: 128  |  Size: 9.2 KiB

4
mail_embed_image/tests/__init__.py

@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# Copyright 2019 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from . import test_mail_embed_image

25
mail_embed_image/tests/test_mail_embed_image.py

@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
# Copyright 2019 Therp BV <https://therp.nl>
# 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
Loading…
Cancel
Save