[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) <tde@openerp.com>
This commit is contained in:
@@ -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"""
|
||||
<div style="display:none;font-size:1px;height:0px;width:0px;opacity:0;">
|
||||
{tools.html_escape(preview)} {'‌ ' * 500}
|
||||
</div>
|
||||
""" 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"""
|
||||
<div style="display:none;font-size:1px;height:0px;width:0px;opacity:0;">
|
||||
{tools.html_escape(preview)}
|
||||
</div>
|
||||
"""
|
||||
return tools.prepend_html_content(html, html_preview)
|
||||
return html
|
||||
|
||||
# ------------------------------------------------------------
|
||||
# RENDERING
|
||||
|
||||
@@ -83,7 +83,7 @@
|
||||
<tr>
|
||||
<td valign="center" width="30%" class="text-center o_mail_v_padding pb0" style="padding:20px 0 0px 0;vertical-align:middle;text-align:center;">
|
||||
<a href="http://www.example.com" style="text-decoration:none;font-weight:bold;background-color:transparent;color:rgb(100, 89, 116);">
|
||||
<img border="0" src="/mass_mailing/static/src/img/theme_default/s_default_image_logo.png" style="border-style:none;height:auto;vertical-align:middle;max-width:400px;width:auto" alt="Your Logo"/>
|
||||
<img border="0" src="/mass_mailing/static/src/img/theme_default/s_default_image_logo.png" style="border-style:none;height:auto;vertical-align:middle;max-width:400px;width:auto"/>
|
||||
</a>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -160,6 +160,7 @@
|
||||
<field name="mailing_type" widget="radio" options="{'horizontal': true}" invisible="1"
|
||||
attrs="{'readonly': [('state', 'in', ('sending', 'done'))]}"/>
|
||||
<field name="subject" string="Subject" attrs="{'readonly': [('state', 'in', ('sending', 'done'))]}" widget="char_emojis" placeholder="e.g. New Sale on all T-shirts"/>
|
||||
<field name="preview" string="Preview Text" attrs="{'readonly': [('state', 'in', ('sending', 'done'))]}" widget="char_emojis" placeholder="e.g. Check it out before it's too late!"/>
|
||||
<label for="mailing_model_id" string="Recipients"/>
|
||||
<div name="mailing_model_id_container">
|
||||
<div class="row">
|
||||
|
||||
@@ -127,7 +127,7 @@
|
||||
<td width="35%"/>
|
||||
<td valign="center" width="30%" class="text-center o_mail_v_padding">
|
||||
<a t-att-href="(company_id.website) or '#'" style="text-decoration:none;">
|
||||
<img border="0" src="/mass_mailing/static/src/img/theme_basic/s_default_image_logo.png" style="height:auto;max-width:400px;width:auto" alt="Your Logo" />
|
||||
<img border="0" src="/mass_mailing/static/src/img/theme_basic/s_default_image_logo.png" style="height:auto;max-width:400px;width:auto"/>
|
||||
</a>
|
||||
</td>
|
||||
<td width="35%" style="text-align:right"/>
|
||||
@@ -145,7 +145,7 @@
|
||||
<tr>
|
||||
<td width="100%" valign="top" class="o_mail_full_width_padding o_mail_no_colorpicker">
|
||||
<a href="#">
|
||||
<img src="/mass_mailing/static/src/img/theme_basic/s_default_image_block_banner.jpg" class="d-block mx-auto" alt="Your Banner Image" />
|
||||
<img src="/mass_mailing/static/src/img/theme_basic/s_default_image_block_banner.jpg" class="d-block mx-auto"/>
|
||||
</a>
|
||||
</td>
|
||||
</tr>
|
||||
@@ -155,7 +155,7 @@
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<template id="s_mail_block_header_view" name="View in browser">
|
||||
<template id="s_mail_block_header_view" name="View Online">
|
||||
<div class="o_snippet_view_in_browser">
|
||||
<div class="o_mail_snippet_general">
|
||||
<table align="center" cellspacing="0" cellpadding="0" border="0" class="o_mail_table_styles">
|
||||
@@ -163,7 +163,7 @@
|
||||
<tr>
|
||||
<td width="100%" class="o_mail_h_padding o_mail_v_padding o_mail_no_colorpicker text-center">
|
||||
<a href="/view">
|
||||
View this email in your browser
|
||||
View Online
|
||||
</a>
|
||||
</td>
|
||||
</tr>
|
||||
@@ -376,7 +376,7 @@
|
||||
<tr>
|
||||
<td style="vertical-align:top;width:270px;" class="o_mail_col_mv o_mail_col_container">
|
||||
<div>
|
||||
<img src="/mass_mailing/static/src/img/theme_basic/s_default_image_block_two_cols_1.jpg" class="d-block mx-auto" alt="Your Picture" />
|
||||
<img src="/mass_mailing/static/src/img/theme_basic/s_default_image_block_two_cols_1.jpg" class="d-block mx-auto"/>
|
||||
<h4 class="text-center">Column title</h4>
|
||||
<p class="text-center o_mail_no_margin">
|
||||
Write one paragraph describing your product,
|
||||
@@ -391,7 +391,7 @@
|
||||
|
||||
<td style="vertical-align:top;width:270px;" class="o_mail_col_mv o_mail_col_container">
|
||||
<div>
|
||||
<img src="/mass_mailing/static/src/img/theme_basic/s_default_image_block_two_cols_2.jpg" class="d-block mx-auto" alt="Your Picture" />
|
||||
<img src="/mass_mailing/static/src/img/theme_basic/s_default_image_block_two_cols_2.jpg" class="d-block mx-auto"/>
|
||||
<h4 class="text-center">Column title</h4>
|
||||
<p class="text-center o_mail_no_margin">
|
||||
Write one paragraph describing your product,
|
||||
@@ -418,7 +418,7 @@
|
||||
<tr>
|
||||
<td style="vertical-align:top;width:180px;" class="o_mail_col_mv o_mail_col_container">
|
||||
<div>
|
||||
<img src="/mass_mailing/static/src/img/theme_basic/s_default_image_block_three_cols_1.jpg" class="d-block mx-auto" alt="Your Picture" />
|
||||
<img src="/mass_mailing/static/src/img/theme_basic/s_default_image_block_three_cols_1.jpg" class="d-block mx-auto"/>
|
||||
<h4 class="text-center">Column Title</h4>
|
||||
<p class="text-center o_mail_no_margin">
|
||||
A short description
|
||||
@@ -431,7 +431,7 @@
|
||||
|
||||
<td style="vertical-align:top;width:180px;" class="o_mail_col_mv o_mail_col_container">
|
||||
<div>
|
||||
<img src="/mass_mailing/static/src/img/theme_basic/s_default_image_block_three_cols_2.jpg" class="d-block mx-auto" alt="Your Picture" />
|
||||
<img src="/mass_mailing/static/src/img/theme_basic/s_default_image_block_three_cols_2.jpg" class="d-block mx-auto"/>
|
||||
<h4 class="text-center">Column Title</h4>
|
||||
<p class="text-center o_mail_no_margin">
|
||||
A short description
|
||||
@@ -444,7 +444,7 @@
|
||||
|
||||
<td style="vertical-align:top;width:180px;" class="o_mail_col_mv o_mail_col_container">
|
||||
<div>
|
||||
<img src="/mass_mailing/static/src/img/theme_basic/s_default_image_block_three_cols_3.jpg" class="d-block mx-auto" alt="Your Picture" />
|
||||
<img src="/mass_mailing/static/src/img/theme_basic/s_default_image_block_three_cols_3.jpg" class="d-block mx-auto"/>
|
||||
<h4 class="text-center">Column Title</h4>
|
||||
<p class="text-center o_mail_no_margin">
|
||||
A short description
|
||||
@@ -468,7 +468,7 @@
|
||||
<tbody>
|
||||
<tr>
|
||||
<td style="width:270px;text-align:center;vertical-align:middle" class="o_mail_col_mv o_mail_img_container o_mail_h_padding o_mail_v_padding">
|
||||
<img src="/mass_mailing/static/src/img/theme_basic/s_default_image_block_image_text.jpg" class="d-block mx-auto" alt="Your Picture" />
|
||||
<img src="/mass_mailing/static/src/img/theme_basic/s_default_image_block_image_text.jpg" class="d-block mx-auto"/>
|
||||
</td>
|
||||
<td style="width:270px;vertical-align:middle;text-align:center;" class="o_mail_col_mv o_mail_h_padding o_mail_v_padding">
|
||||
<h3>Omnichannel sales</h3>
|
||||
@@ -498,7 +498,7 @@
|
||||
</div>
|
||||
</td>
|
||||
<td style="width:270px;text-align:center;vertical-align:middle" class="o_mail_col_mv o_mail_img_container o_mail_h_padding o_mail_v_padding">
|
||||
<img src="/mass_mailing/static/src/img/theme_basic/s_default_image_block_text_image.jpg" class="d-block mx-auto" alt="Your Picture" />
|
||||
<img src="/mass_mailing/static/src/img/theme_basic/s_default_image_block_text_image.jpg" class="d-block mx-auto"/>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
@@ -514,7 +514,7 @@
|
||||
<tbody>
|
||||
<tr>
|
||||
<td width="100%" align="center" style="text-align:center" class="o_mail_h_padding">
|
||||
<img src="/mass_mailing/static/src/img/theme_basic/s_default_image_block_image.jpg" class="d-block mx-auto" alt="Your Picture" />
|
||||
<img src="/mass_mailing/static/src/img/theme_basic/s_default_image_block_image.jpg" class="d-block mx-auto"/>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
@@ -666,7 +666,7 @@
|
||||
</div>
|
||||
</td>
|
||||
<td style="width:25%;min-height: 150px;" class="text-center bg-o-color-4 o_mail_col_mv">
|
||||
<img src="/mass_mailing/static/src/img/theme_basic/s_default_image_block_event.jpg" class="d-block mx-auto" alt="Your Picture" />
|
||||
<img src="/mass_mailing/static/src/img/theme_basic/s_default_image_block_event.jpg" class="d-block mx-auto"/>
|
||||
</td>
|
||||
<td class="o_mail_h_padding o_mail_v_padding o_mail_col_mv" style="min-height: 150px;">
|
||||
<div style="min-height: 111px">
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
<td width="20%"/>
|
||||
<td valign="center" width="60%" class="o_mail_logo_container text-center o_mail_v_padding">
|
||||
<a t-att-href="(company_id.website) or '#'" style="text-decoration:none;">
|
||||
<img border="0" src="/mass_mailing/static/src/img/theme_default/s_default_image_logo.png" style="height:auto;max-width:400px;" alt="Your Logo" />
|
||||
<img border="0" src="/mass_mailing/static/src/img/theme_default/s_default_image_logo.png" style="height:auto;max-width:400px;"/>
|
||||
</a>
|
||||
</td>
|
||||
<td width="20%" style="text-align:right"/>
|
||||
|
||||
@@ -24,7 +24,7 @@ class TestMassMailing(models.TransientModel):
|
||||
mass_mail_layout = self.env.ref('mass_mailing.mass_mailing_mail_layout')
|
||||
for test_mail in test_emails:
|
||||
# Convert links in absolute URLs before the application of the shortener
|
||||
body = mailing._prepend_preview(mailing.body_html)
|
||||
body = mailing._prepend_preview(mailing.body_html, mailing.preview)
|
||||
body = self.env['mail.render.mixin']._replace_local_links(body)
|
||||
body = tools.html_sanitize(body, sanitize_attributes=True, sanitize_style=True)
|
||||
mail_values = {
|
||||
|
||||
@@ -33,15 +33,9 @@ class TestMassMailing(TestMailFullCommon):
|
||||
</a>
|
||||
</p>
|
||||
</div>'''
|
||||
jinja_html_preview = self.env['mail.render.mixin']._generate_preview(jinja_html)
|
||||
self.assertIn('Hello ${object.name}', jinja_html_preview)
|
||||
self.assertIn('Here are your personal links', jinja_html_preview)
|
||||
self.assertIn('External link', jinja_html_preview)
|
||||
self.assertIn('Internal link', jinja_html_preview)
|
||||
self.assertIn('Unsubscribe link', jinja_html_preview)
|
||||
self.assertIn('View link', jinja_html_preview)
|
||||
|
||||
mailing.write({
|
||||
'preview': 'Hi ${object.name} :)',
|
||||
'body_html': jinja_html,
|
||||
'mailing_model_id': self.env['ir.model']._get('mailing.test.optout').id,
|
||||
})
|
||||
@@ -70,7 +64,7 @@ class TestMassMailing(TestMailFullCommon):
|
||||
email = self._find_sent_mail_wemail(recipient.email_normalized)
|
||||
# preview correctly integrated rendered jinja
|
||||
self.assertIn(
|
||||
'Hello %s Here are your personal links External link Internal link Unsubscribe link View link' % recipient.name,
|
||||
'Hi %s :)' % recipient.name,
|
||||
email['body'])
|
||||
# rendered unsubscribe
|
||||
self.assertIn(
|
||||
|
||||
Reference in New Issue
Block a user