Browse Source

fixup! fixup! [ADD] mail_embed_image module

pull/404/head
George Daramouskas 5 years ago
parent
commit
fbdadcaacc
No known key found for this signature in database GPG Key ID: 5B4EF742F8CD859C
  1. 18
      mail_embed_image/models/ir_mail_server.py

18
mail_embed_image/models/ir_mail_server.py

@ -8,7 +8,7 @@ from odoo.addons.base.ir.ir_mail_server import encode_header_param
from lxml.html.soupparser import fromstring from lxml.html.soupparser import fromstring
from lxml import etree from lxml import etree
from base64 import b64encode from base64 import b64encode
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email import Encoders from email import Encoders
@ -63,22 +63,24 @@ class IrMailServer(models.Model):
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[@src^='http']"):
src = img.get('src')
content = requests.get(src).content
content_base64 = b64encode(content)
for img in root.xpath(
"//img[starts-with(@src, '/web/image')]"):
base_url = self.env['ir.config_parameter'].get_param(
'web.base.url')
src = base_url + img.get('src')
response = requests.get(src)
#content_base64 = b64encode(response.content)
cid = uuid.uuid4().hex cid = uuid.uuid4().hex
filename_rfc2047 = encode_header_param(cid) filename_rfc2047 = encode_header_param(cid)
part = MIMEBase('application', 'octet-stream')
part = MIMEImage(response.content)
part.set_param('name', filename_rfc2047) part.set_param('name', filename_rfc2047)
part.add_header( part.add_header(
'Content-Disposition', 'Content-Disposition',
'attachment', 'attachment',
cid=cid, cid=cid,
filename=filename_rfc2047, filename=filename_rfc2047,
content=content_base64,
) )
part.set_payload(content)
part.set_payload(response.content)
Encoders.encode_base64(part) Encoders.encode_base64(part)
result.attach(part) result.attach(part)
img.set('src', 'cid:%s' % (cid)) img.set('src', 'cid:%s' % (cid))

Loading…
Cancel
Save