summaryrefslogtreecommitdiff
path: root/odoo
diff options
context:
space:
mode:
authorstd-odoo <std@odoo.com>2020-05-26 06:29:34 +0000
committerThibault Delavallée <tde@odoo.com>2020-08-05 13:55:04 +0000
commited4846dd9df32aa97ba2bf73ac772428d4237f25 (patch)
tree4d7d9974309ba57d6fc898cf7c0b0f7a8cd68650 /odoo
parentac0579263bc2996321f55cb20da3332f027dbb36 (diff)
[IMP] mass_mailing: add preview block for mailings
PURPOSE When displaying an email in a list the mail client (gmail, outlook...) computes a preview based on the content. This preview is generally not well computed as it contains a lot of garbage and tags while it should contain only relevant text. SPECIFICATIONS To build this preview, all mail clients read the content of the email. The only way to be able to customize the preview is to add an invisible HTML element at the beginning of the email with the wanted preview text. We add at the end of the preview `&zwnj;` (zero-width non-joiner) to fill the end of the preview in order to not have the beginning of the mail at the end of the preview. It doesn't work with simple space as the mail clients trim each HTML element content. Task ID-2172125 PR #49886
Diffstat (limited to 'odoo')
-rw-r--r--odoo/addons/base/tests/test_mail.py53
-rw-r--r--odoo/tools/mail.py14
2 files changed, 67 insertions, 0 deletions
diff --git a/odoo/addons/base/tests/test_mail.py b/odoo/addons/base/tests/test_mail.py
index 5973fd623fb..69ad772447e 100644
--- a/odoo/addons/base/tests/test_mail.py
+++ b/odoo/addons/base/tests/test_mail.py
@@ -5,6 +5,7 @@
from unittest.mock import patch
import email.policy
import email.message
+import re
import threading
from odoo.tests.common import BaseCase, SavepointCase, TransactionCase
@@ -12,6 +13,7 @@ from odoo.tools import (
is_html_empty, html_sanitize, append_content_to_html, plaintext2html,
email_split,
misc, formataddr,
+ prepend_html_content,
)
from . import test_mail_examples
@@ -334,6 +336,57 @@ class TestHtmlTools(BaseCase):
for content in valid_html_samples:
self.assertFalse(is_html_empty(content))
+ def test_prepend_html_content(self):
+ body = """
+ <html>
+ <body>
+ <div>test</div>
+ </body>
+ </html>
+ """
+
+ content = "<span>content</span>"
+
+ result = prepend_html_content(body, content)
+ result = re.sub(r'[\s\t]', '', result)
+ self.assertEqual(result, "<html><body><span>content</span><div>test</div></body></html>")
+
+ body = "<div>test</div>"
+ content = "<span>content</span>"
+
+ result = prepend_html_content(body, content)
+ result = re.sub(r'[\s\t]', '', result)
+ self.assertEqual(result, "<span>content</span><div>test</div>")
+
+ body = """
+ <body>
+ <div>test</div>
+ </body>
+ """
+
+ result = prepend_html_content(body, content)
+ result = re.sub(r'[\s\t]', '', result)
+ self.assertEqual(result, "<body><span>content</span><div>test</div></body>")
+
+ body = """
+ <html>
+ <body>
+ <div>test</div>
+ </body>
+ </html>
+ """
+
+ content = """
+ <html>
+ <body>
+ <div>test</div>
+ </body>
+ </html>
+ """
+ result = prepend_html_content(body, content)
+ result = re.sub(r'[\s\t]', '', result)
+ self.assertEqual(result, "<html><body><div>test</div><div>test</div></body></html>")
+
class TestEmailTools(BaseCase):
""" Test some of our generic utility functions for emails """
diff --git a/odoo/tools/mail.py b/odoo/tools/mail.py
index eed992e142f..c98a575f10a 100644
--- a/odoo/tools/mail.py
+++ b/odoo/tools/mail.py
@@ -428,6 +428,20 @@ def append_content_to_html(html, content, plaintext=True, preserve=False, contai
return '%s%s' % (html, content)
return '%s%s%s' % (html[:insert_location], content, html[insert_location:])
+
+def prepend_html_content(html_body, html_content):
+ """Prepend some HTML content at the beginning of an other HTML content."""
+ html_content = re.sub(r'(?i)(</?(?:html|body|head|!\s*DOCTYPE)[^>]*>)', '', html_content)
+ html_content = html_content.strip()
+
+ insert_index = next(re.finditer(r'<body[^>]*>', html_body), None)
+ if insert_index is None:
+ insert_index = next(re.finditer(r'<html[^>]*>', html_body), None)
+
+ insert_index = insert_index.end() if insert_index else 0
+
+ return html_body[:insert_index] + html_content + html_body[insert_index:]
+
#----------------------------------------------------------
# Emails
#----------------------------------------------------------