diff --git a/addons/mail/tests/test_ir_mail_server.py b/addons/mail/tests/test_ir_mail_server.py index 7137951bd9c..aef82d0c516 100644 --- a/addons/mail/tests/test_ir_mail_server.py +++ b/addons/mail/tests/test_ir_mail_server.py @@ -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" ', + from_filter='random.domain', + ) diff --git a/odoo/addons/base/models/ir_mail_server.py b/odoo/addons/base/models/ir_mail_server.py index 3704a2c6fcb..d76fab25d02 100644 --- a/odoo/addons/base/models/ir_mail_server.py +++ b/odoo/addons/base/models/ir_mail_server.py @@ -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): diff --git a/odoo/addons/base/tests/test_ir_mail_server.py b/odoo/addons/base/tests/test_ir_mail_server.py index 1b51499cb23..39fb966d892 100644 --- a/odoo/addons/base/tests/test_ir_mail_server.py +++ b/odoo/addons/base/tests/test_ir_mail_server.py @@ -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" ', + 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",