Browse Source

fixup! [ADD] mail_embed_image module

pull/404/head
George Daramouskas 6 years ago
parent
commit
d5e82ae716
No known key found for this signature in database GPG Key ID: 5B4EF742F8CD859C
  1. 12
      mail_embed_image/README.rst
  2. 8
      mail_embed_image/__manifest__.py
  3. 35
      mail_embed_image/models/ir_mail_server.py

12
mail_embed_image/README.rst

@ -14,21 +14,23 @@ Installation
To install this module, you need to: To install this module, you need to:
#. do this ...
Follow the usual procedure for your version of Odoo.
Configuration Configuration
============= =============
To configure this module, you need to: To configure this module, you need to:
#. go to ...
There are no configuration parameters for now
Usage Usage
===== =====
To use this module, you need to:
When this module is installed all the emails that will be sent will be examined
and if they contain images with their src attribute pointing to a url, then
that will change to Content Id cid.
#. go to ...
.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas .. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot :alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/205/10.0 :target: https://runbot.odoo-community.org/runbot/205/10.0
@ -37,7 +39,7 @@ To use this module, you need to:
Known issues / Roadmap Known issues / Roadmap
====================== ======================
* Some src attributes might contain data instead of urls. Deal with it.
* None so far.
Bug Tracker Bug Tracker
=========== ===========

8
mail_embed_image/__manifest__.py

@ -2,17 +2,15 @@
# Copyright 2019 Therp BV <https://therp.nl> # Copyright 2019 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
{ {
"name": "mail_embed_image",
"name": "Mail Embed Image",
"version": "10.0.1.0.0", "version": "10.0.1.0.0",
"author": "Therp BV,Odoo Community Association (OCA)", "author": "Therp BV,Odoo Community Association (OCA)",
"license": "AGPL-3", "license": "AGPL-3",
"category": "",
"summary": "",
"category": "Social",
"summary": "Replace img.src's which start with http with inline cids",
"depends": [ "depends": [
'mail', 'mail',
], ],
"data": [
],
"installable": True, "installable": True,
"application": False, "application": False,
} }

35
mail_embed_image/models/ir_mail_server.py

@ -37,35 +37,34 @@ class IrMailServer(models.Model):
email_to, email_to,
subject, subject,
body, body,
email_cc,
email_bcc,
reply_to,
attachments,
message_id,
references,
object_id,
subtype,
headers,
body_alternative,
subtype_alternative,
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,
) )
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
def _build_email_replace_img_src(msg, attachments):
""" Given a message, find it's img tags and if they
are URLs, replace them with cids.
:param msg: A email.message.Message :param msg: A email.message.Message
""" """
if msg.is_multipart(): if msg.is_multipart():
for part in msg.get_payload(): for part in msg.get_payload():
replace_img_src(part, attachments)
_build_email_replace_img_src(part, attachments)
else: else:
if msg.get_content_subtype() == 'html': if msg.get_content_subtype() == 'html':
body = msg.get_payload(decode=True) body = msg.get_payload(decode=True)
root = fromstring(body) root = fromstring(body)
for img in root.xpath('//img'):
for img in root.xpath("//img[@src^='http']"):
src = img.get('src') src = img.get('src')
if src.startswith('http'):
content = requests.get(src).content content = requests.get(src).content
content_base64 = b64encode(content) content_base64 = b64encode(content)
cid = uuid.uuid4().hex cid = uuid.uuid4().hex
@ -84,5 +83,5 @@ class IrMailServer(models.Model):
result.attach(part) result.attach(part)
img.set('src', 'cid:%s' % (cid)) img.set('src', 'cid:%s' % (cid))
msg.set_payload(etree.tostring(root)) msg.set_payload(etree.tostring(root))
replace_img_src(result, attachments)
_build_email_replace_img_src(result, attachments)
return result return result
Loading…
Cancel
Save