[FIX] mass_mailing: precompute email_from based on alias domain

With 17.0 the `mail.alias.domain` was introduced, allowing the
configuration of the bounce, catchall and default from email addresses
at the company level (`res_company`).

For this purpose, the `_compute_email_from` method was introduced to correctly
compute the `email_from` in various email contexts.

But, the `email_from` field in mass_mailing was using an obsolete combination
of `default=` and a compute method.
This meant that when one created a new `mailing.mailing` record, it was
always defaulting to `self.env.user.email_formatted`.
The compute was only triggered when the `mail_server_id` field manually
updated.

This is corrected with this fix and a unit test was added to test three
different use cases.
An invisible `create_uid` was added to the base mailing From view xml
to have the compute method trigger correctly in some unit tests.

How to reproduce bug:
See related github issue https://github.com/odoo/odoo/issues/151310

Behavior after this fix:
If `mail.alias.domain` is setup for the company and a default `ir.mail_server`
is configured for Email Marketing, `_compute_email_from` will select
the expected `email_from` at record creation for `mailing.mailing`.

opw-3704715
Fixes #151310

commit 4b5453d87498d32b0ec3e3e7d64d35f903ac2193
Author: jorv <jorv@odoo.com>
Date:   Mon Mar 18 15:30:06 2024 +0100

[FIX] mass_mailing: precompute email_from based on alias domain

With 17.0 the `mail.alias.domain` was introduced, allowing the
configuration of the bounce, catchall and default from email addresses
at the company level (`res_company`).

For this purpose, the `_compute_email_from` method was introduced to correctly
compute the `email_from` in various email contexts.

But, the `email_from` field in mass_mailing was using an obsolete combination
of `default=` and a compute method.
This meant that when one created a new `mailing.mailing` record, it was
always defaulting to `self.env.user.email_formatted`.
The compute was only triggered when the `mail_server_id` field manually
updated.

This is corrected with this fix and a unit test was added to test three
different use cases.
An invisible `create_uid` was added to the base mailing From view xml
to have the compute method trigger correctly in some unit tests.

How to reproduce bug:
See related github issue https://github.com/odoo/odoo/issues/151310

Behavior after this fix:
If `mail.alias.domain` is setup for the company and a default `ir.mail_server`
is configured for Email Marketing, `_compute_email_from` will select
the expected `email_from` at record creation for `mailing.mailing`.

opw-3704715
Fixes #151310

closes odoo/odoo#152655

Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
This commit is contained in:
jorv
2024-03-20 13:52:17 +00:00
parent 4088111533
commit 5d469f67f1
3 changed files with 66 additions and 4 deletions
+3 -4
View File
@@ -89,8 +89,7 @@ class MassMailing(models.Model):
'Keep it empty if you prefer the first characters of your email content to appear instead.')
email_from = fields.Char(
string='Send From',
compute='_compute_email_from', readonly=False, required=True, store=True,
default=lambda self: self.env.user.email_formatted)
compute='_compute_email_from', readonly=False, required=True, store=True, precompute=True)
favorite = fields.Boolean('Favorite', copy=False, tracking=True)
favorite_date = fields.Datetime(
'Favorite Date',
@@ -252,12 +251,12 @@ class MassMailing(models.Model):
for mailing in self:
mailing.ab_testing_is_winner_mailing = mailing.campaign_id.ab_testing_winner_mailing_id == mailing
@api.depends('mail_server_id')
@api.depends('mail_server_id', 'create_uid')
def _compute_email_from(self):
user_email = self.env.user.email_formatted
notification_email = self.env['ir.mail_server']._get_default_from_address()
for mailing in self:
user_email = mailing.create_uid.email_formatted or self.env.user.email_formatted
server = mailing.mail_server_id
if not server:
mailing.email_from = mailing.email_from or user_email
@@ -313,6 +313,68 @@ class TestMassMailValues(MassMailCommon):
})
self.assertEqual(literal_eval(mailing.mailing_domain), [('email', 'ilike', 'test.example.com')])
@users('user_marketing')
def test_mailing_computed_fields_default_email_from(self):
# Testing if the email_from is correctly computed when an
# alias domain for the company is set
# Setup mail outgoing server for use cases
from_filter_match, from_filter_missmatch = self.env['ir.mail_server'].sudo().create([
# Case where alias domain is set and there is a default outgoing email server
# for mass mailing. from_filter matches domain of company alias domain
# before record creation
{
'name' : 'mass_mailing_test_match_from_filter',
'from_filter' : self.alias_domain,
'smtp_host' : 'not_real@smtp.com',
},
# Case where alias domain is set and there is a default outgoing email server
# for mass mailing. from_filter DOES NOT match domain of company alias domain
# before record creation
{
'name' : 'mass_mailing_test_from_missmatch',
'from_filter' : 'test.com',
'smtp_host' : 'not_real@smtp.com',
},
])
# Expected combos of server vs FROM values
servers = [
self.env['ir.mail_server'],
from_filter_match,
from_filter_missmatch,
]
expected_from_all = [
self.env.user.email_formatted, # default when no server
self.env.user.company_id.alias_domain_id.default_from_email, # matches company alias domain
self.env.user.email_formatted, # not matching from filter -> back to user from
]
for mail_server, expected_from in zip(servers, expected_from_all):
with self.subTest(server_name=mail_server.name):
# When a mail server is set, we update the mass mailing
# settings to designate a dedicated outgoing email server
if mail_server:
self.env['res.config.settings'].sudo().create({
'mass_mailing_mail_server_id' : mail_server.id,
'mass_mailing_outgoing_mail_server' : mail_server,
}).execute()
# Create mailing
mailing = self.env['mailing.mailing'].create({
'name': f'TestMailing {mail_server.name}',
'subject': f'Test {mail_server.name}',
})
# Check email_from
self.assertEqual(mailing.email_from, expected_from)
# If configured, check if dedicated email outgoing server is
# on mailing record
self.assertEqual(mailing.mail_server_id, mail_server)
@users('user_marketing')
def test_mailing_computed_fields_form(self):
mailing_form = Form(self.env['mailing.mailing'].with_context(
@@ -197,6 +197,7 @@
<widget name="web_ribbon" title="Archived" bg_color="text-bg-danger" invisible="active"/>
<group class="o_mass_mailing_mailing_group">
<field name="active" invisible="1"/>
<field name="create_uid" invisible="1"/>
<field name="mailing_type" widget="radio" options="{'horizontal': true}"
invisible="1"
readonly="state != 'draft'" force_save="1"/>