From 13d1f3e012d0c528dbea67ee5e1d4c32548195a3 Mon Sep 17 00:00:00 2001 From: std-odoo Date: Mon, 10 Aug 2020 08:07:48 +0000 Subject: [PATCH] [IMP] mass_mailing: allow to customize the mail preview Purpose ======= Allow to customize the preview text in mass mailing. Specifications ============== We do not generate the preview text anymore, instead we add a new field to let the user choose want he want to put into the preview. As the preview is added in the mail body, it supports dynamic placeholders. Rename "View in browser" into "View Online" because the text will be displayed in the preview if the snippet is added at the beginning of the email. And we want to make it shorter. Remove the alt attribute of the image tag which are not part of the "email content" in the mass mailing template. So, we improve the email preview in the mail clients (otherwise the mail client include the alt attribute into the preview). Task-2313872 closes odoo/odoo#55695 Related: odoo/upgrade#1584 Related: odoo/enterprise#12323 Signed-off-by: Thibault Delavallee (tde) --- addons/mail/models/mail_render_mixin.py | 45 ++++++------------- .../mass_mailing/data/mass_mailing_demo.xml | 2 +- addons/mass_mailing/models/mailing.py | 7 ++- .../views/mailing_mailing_views.xml | 1 + addons/mass_mailing/views/snippets_themes.xml | 26 +++++------ .../mass_mailing/views/themes_templates.xml | 2 +- .../wizard/mailing_mailing_test.py | 2 +- .../test_mail_full/tests/test_mass_mailing.py | 10 +---- 8 files changed, 38 insertions(+), 57 deletions(-) diff --git a/addons/mail/models/mail_render_mixin.py b/addons/mail/models/mail_render_mixin.py index db29d266114..d35fbf5dddf 100644 --- a/addons/mail/models/mail_render_mixin.py +++ b/addons/mail/models/mail_render_mixin.py @@ -207,45 +207,26 @@ class MailRenderMixin(models.AbstractModel): return html @api.model - def _generate_preview(self, html): - """ Generate a preview of html, to used for example in marketing emails. - - We add the character `‌` (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 (don't work with simple space as the content is trimmed). - https://litmus.com/blog/the-ultimate-guide-to-preview-text-support - - :param html: html content for which we generate the preview - :return: html to use as preview - """ - preview = "" - for element in re.finditer(r"\>([^<>]+)\<", html): - element = element.group(1).strip() - if element: - preview += " %s" % element - if len(preview) > 500: - break - - html_preview = f""" -
- {tools.html_escape(preview)} {'‌ ' * 500} -
- """ if preview else '' - - return html_preview - - @api.model - def _prepend_preview(self, html): + def _prepend_preview(self, html, preview): """ Prepare the email body before sending. Add the text preview at the beginning of the mail. The preview text is displayed bellow the mail subject of most mail client (gmail, outlook...). - See ``_generate_preview()`` for more details about the preview computation. - :param html: html content for which we want to prepend a preview + :param preview: the preview to add before the html content :return: html with preprended preview """ - return tools.prepend_html_content(html, self._generate_preview(html)) + if preview: + preview = preview.strip() + + if preview: + html_preview = f""" +
+ {tools.html_escape(preview)} +
+ """ + return tools.prepend_html_content(html, html_preview) + return html # ------------------------------------------------------------ # RENDERING diff --git a/addons/mass_mailing/data/mass_mailing_demo.xml b/addons/mass_mailing/data/mass_mailing_demo.xml index 34e72a7418b..9587eb48d2a 100644 --- a/addons/mass_mailing/data/mass_mailing_demo.xml +++ b/addons/mass_mailing/data/mass_mailing_demo.xml @@ -83,7 +83,7 @@ - Your Logo ​ + ​ diff --git a/addons/mass_mailing/models/mailing.py b/addons/mass_mailing/models/mailing.py index d329cb2e572..1048a3da425 100644 --- a/addons/mass_mailing/models/mailing.py +++ b/addons/mass_mailing/models/mailing.py @@ -67,6 +67,11 @@ class MassMailing(models.Model): active = fields.Boolean(default=True, tracking=True) subject = fields.Char('Subject', help='Subject of your Mailing', required=True, translate=True) + preview = fields.Char( + 'Preview', translate=True, + help='Catchy preview sentence that encourages recipients to open this email.\n' + 'In most inboxes, this is displayed next to the subject.\n' + 'Keep it empty if you prefer the first characters of your email content to appear instead.') email_from = fields.Char(string='Send From', required=True, default=lambda self: self.env.user.email_formatted) sent_date = fields.Datetime(string='Sent Date', copy=False) @@ -569,7 +574,7 @@ class MassMailing(models.Model): composer_values = { 'author_id': author_id, 'attachment_ids': [(4, attachment.id) for attachment in mailing.attachment_ids], - 'body': self._prepend_preview(self.body_html), + 'body': self._prepend_preview(self.body_html, self.preview), 'subject': mailing.subject, 'model': mailing.mailing_model_real, 'email_from': mailing.email_from, diff --git a/addons/mass_mailing/views/mailing_mailing_views.xml b/addons/mass_mailing/views/mailing_mailing_views.xml index 5c689b0cf57..a645334412a 100644 --- a/addons/mass_mailing/views/mailing_mailing_views.xml +++ b/addons/mass_mailing/views/mailing_mailing_views.xml @@ -160,6 +160,7 @@ +