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. 69
      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:
#. do this ...
Follow the usual procedure for your version of Odoo.
Configuration
=============
To configure this module, you need to:
#. go to ...
There are no configuration parameters for now
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
:alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/205/10.0
@ -37,7 +39,7 @@ To use this module, you need to:
Known issues / Roadmap
======================
* Some src attributes might contain data instead of urls. Deal with it.
* None so far.
Bug Tracker
===========

8
mail_embed_image/__manifest__.py

@ -2,17 +2,15 @@
# Copyright 2019 Therp BV <https://therp.nl>
# 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",
"author": "Therp BV,Odoo Community Association (OCA)",
"license": "AGPL-3",
"category": "",
"summary": "",
"category": "Social",
"summary": "Replace img.src's which start with http with inline cids",
"depends": [
'mail',
],
"data": [
],
"installable": True,
"application": False,
}

69
mail_embed_image/models/ir_mail_server.py

@ -37,52 +37,51 @@ class IrMailServer(models.Model):
email_to,
subject,
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
"""
if msg.is_multipart():
for part in msg.get_payload():
replace_img_src(part, attachments)
_build_email_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'):
for img in root.xpath("//img[@src^='http']"):
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))
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)
_build_email_replace_img_src(result, attachments)
return result
Loading…
Cancel
Save