[IMP] base: use the notification email by default for miss-configured database

Purpose
=======
When a database is miss-configured, we have no other choices to
potentially spoof the FROM. We decided that a better heuristic would
be to use the notification email instead of the user email.

Task-3645895

closes odoo/odoo#149189

X-original-commit: eae9214ade8487ac326fc83abe67af53d861e00a
Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
This commit is contained in:
std-odoo
2024-01-12 16:12:47 +00:00
parent 92fd50cda7
commit 55a3a17090
3 changed files with 45 additions and 7 deletions
+17 -1
View File
@@ -225,7 +225,7 @@ class TestIrMailServer(MailCommon):
self.assertEqual(mail_server, expected_mail_server)
self.assertEqual(mail_from, expected_email_from)
@mute_logger('odoo.models.unlink')
@mute_logger('odoo.models.unlink', 'odoo.addons.base.models.ir_mail_server')
def test_mail_server_send_email(self):
""" Test main 'send_email' usage: check mail_server choice based on from
filters, encapsulation, spoofing. """
@@ -292,3 +292,19 @@ class TestIrMailServer(MailCommon):
message_from=f'"Name" <{self.default_from}@{self.alias_domain}>',
mail_server=self.mail_server_domain,
)
# miss-configured database, no mail servers from filter
# match the user / notification email
self.env['ir.mail_server'].search([]).from_filter = "random.domain"
self.mail_alias_domain.default_from = 'test'
self.mail_alias_domain.name = 'custom_domain.com'
with self.mock_smtplib_connection():
message = self._build_email(mail_from='specific_user@test.com')
IrMailServer.send_email(message)
self.connect_mocked.assert_called_once()
self.assertSMTPEmailsSent(
smtp_from='test@custom_domain.com',
message_from='"specific_user" <test@custom_domain.com>',
from_filter='random.domain',
)
+12 -4
View File
@@ -748,13 +748,17 @@ class IrMailServer(models.Model):
# 3. Take the first mail server without "from_filter" because
# nothing else has been found... Will spoof the FROM because
# we have no other choices
# we have no other choices (will use the notification email if available
# otherwise we will use the user email)
if mail_server := mail_servers.filtered(lambda m: not m.from_filter):
return mail_server[0], email_from
return mail_server[0], notifications_email or email_from
# 4. Return the first mail server even if it was configured for another domain
if mail_servers:
return mail_servers[0], email_from
_logger.warning(
"No mail server matches the from_filter, using %s as fallback",
notifications_email or email_from)
return mail_servers[0], notifications_email or email_from
# 5: SMTP config in odoo-bin arguments
from_filter = self.env['ir.mail_server']._get_default_from_filter()
@@ -765,7 +769,11 @@ class IrMailServer(models.Model):
if notifications_email and self._match_from_filter(notifications_email, from_filter):
return None, notifications_email
return None, email_from
_logger.warning(
"The from filter of the CLI configuration does not match the notification email "
"or the user email, using %s as fallback",
notifications_email or email_from)
return None, notifications_email or email_from
@api.model
def _match_from_filter(self, email_from, from_filter):
+16 -2
View File
@@ -287,7 +287,7 @@ class TestIrMailServer(TransactionCase, MockSmtplibCase):
from_filter=False,
)
@mute_logger('odoo.models.unlink')
@mute_logger('odoo.models.unlink', 'odoo.addons.base.models.ir_mail_server')
def test_mail_server_send_email_context_force(self):
""" Allow to force notifications_email / bounce_address from context
to allow higher-level apps to send values until end of mail stack
@@ -317,6 +317,20 @@ class TestIrMailServer(TransactionCase, MockSmtplibCase):
from_filter=context_server.from_filter,
)
# miss-configured database, no mail servers from filter
# match the user / notification email
self.env['ir.mail_server'].search([]).from_filter = "random.domain"
with self.mock_smtplib_connection():
message = self._build_email(mail_from='specific_user@test.com')
IrMailServer.with_context(domain_notifications_email='test@custom_domain.com').send_email(message)
self.connect_mocked.assert_called_once()
self.assertSMTPEmailsSent(
smtp_from='test@custom_domain.com',
message_from='"specific_user" <test@custom_domain.com>',
from_filter='random.domain',
)
@mute_logger('odoo.models.unlink')
def test_mail_server_send_email_IDNA(self):
""" Test that the mail from / recipient envelop are encoded using IDNA """
@@ -332,7 +346,7 @@ class TestIrMailServer(TransactionCase, MockSmtplibCase):
from_filter=False,
)
@mute_logger('odoo.models.unlink')
@mute_logger('odoo.models.unlink', 'odoo.addons.base.models.ir_mail_server')
@patch.dict(config.options, {
"from_filter": "dummy@example.com, test.mycompany.com, dummy2@example.com",
"smtp_server": "example.com",