[FIX] base: fix the email used to test the SMTP configuration
Bug === When the from_filter of an outgoing mail server contains a domain (e.g. company.com), and when the system parameter "mail.default.from" is a full email address, with a different domain name (e.g. notification@example.com) we concatenate both value which produce an invalid email (e.g. notification@example.com@company.com). Instead, we always give the priority to the from_filter, and fallback on "noreply" for the local part when needed. Task-3230917 closes odoo/odoo#115890 X-original-commit: bf02481de9932de2e0246ac36fcf59f37a89b511 Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
This commit is contained in:
@@ -202,19 +202,28 @@ class IrMailServer(models.Model):
|
||||
|
||||
def _get_test_email_addresses(self):
|
||||
self.ensure_one()
|
||||
email_to = "noreply@odoo.com"
|
||||
if self.from_filter:
|
||||
if "@" in self.from_filter:
|
||||
# All emails will be sent from the same address
|
||||
return self.from_filter, "noreply@odoo.com"
|
||||
return self.from_filter, email_to
|
||||
# All emails will be sent from any address in the same domain
|
||||
default_from = self.env["ir.config_parameter"].sudo().get_param("mail.default.from", "odoo")
|
||||
return f"{default_from}@{self.from_filter}", "noreply@odoo.com"
|
||||
if "@" not in default_from:
|
||||
return f"{default_from}@{self.from_filter}", email_to
|
||||
elif self._match_from_filter(default_from, self.from_filter):
|
||||
# the mail server is configured for a domain
|
||||
# that match the default email address
|
||||
return default_from, email_to
|
||||
# the from_filter is configured for a domain different that the one
|
||||
# of the full email configured in mail.default.from
|
||||
return f"noreply@{self.from_filter}", email_to
|
||||
# Fallback to current user email if there's no from filter
|
||||
email_from = self.env.user.email
|
||||
if not email_from:
|
||||
raise UserError(_('Please configure an email on the current user to simulate '
|
||||
'sending an email message via this outgoing server'))
|
||||
return email_from, 'noreply@odoo.com'
|
||||
return email_from, email_to
|
||||
|
||||
def test_smtp_connection(self):
|
||||
for server in self:
|
||||
|
||||
@@ -433,6 +433,33 @@ class TestIrMailServer(TransactionCase, MockSmtplibCase):
|
||||
from_filter='test.com',
|
||||
)
|
||||
|
||||
def test_mail_server_get_email_addresses(self):
|
||||
"""Test the email used to test the mail server connection."""
|
||||
self.server_notification.from_filter = 'example_2.com'
|
||||
|
||||
self.env['ir.config_parameter'].set_param('mail.default.from', 'notifications@example.com')
|
||||
email_from = self.server_notification._get_test_email_addresses()[0]
|
||||
self.assertEqual(email_from, 'noreply@example_2.com')
|
||||
|
||||
self.env['ir.config_parameter'].set_param('mail.default.from', 'notifications')
|
||||
email_from = self.server_notification._get_test_email_addresses()[0]
|
||||
self.assertEqual(email_from, 'notifications@example_2.com')
|
||||
|
||||
self.server_notification.from_filter = 'full_email@example_2.com'
|
||||
|
||||
self.env['ir.config_parameter'].set_param('mail.default.from', 'notifications')
|
||||
email_from = self.server_notification._get_test_email_addresses()[0]
|
||||
self.assertEqual(email_from, 'full_email@example_2.com')
|
||||
|
||||
self.env['ir.config_parameter'].set_param('mail.default.from', 'notifications@example.com')
|
||||
email_from = self.server_notification._get_test_email_addresses()[0]
|
||||
self.assertEqual(email_from, 'full_email@example_2.com')
|
||||
|
||||
self.env['ir.config_parameter'].set_param('mail.default.from', 'notifications@example.com')
|
||||
self.server_notification.from_filter = 'example.com'
|
||||
email_from = self.server_notification._get_test_email_addresses()[0]
|
||||
self.assertEqual(email_from, 'notifications@example.com')
|
||||
|
||||
@mute_logger('odoo.models.unlink')
|
||||
@patch.dict(config.options, {'from_filter': 'fake.com', 'smtp_server': 'cli_example.com'})
|
||||
def test_mail_server_configuration_cli(self):
|
||||
|
||||
Reference in New Issue
Block a user