Skip to content

Commit 5e84036

Browse files
committed
[14.0][MIG] mail_company_aware_sender: Backport to 14.0
1 parent 51a0268 commit 5e84036

File tree

6 files changed

+43
-26
lines changed

6 files changed

+43
-26
lines changed

mail_company_aware_sender/__manifest__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{
55
"name": "Mail Sender Company Aware",
66
"summary": "Send emails with company specific mail domain",
7-
"version": "16.0.1.0.0",
7+
"version": "14.0.1.0.0",
88
"category": "Social Network",
99
"website": "https://github.com/OCA/social",
1010
"author": ("Therp BV, " "Odoo Community Association (OCA)"),

mail_company_aware_sender/models/ir_mail_server.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ def _is_domain_whitelisted(self, domain):
2121

2222
def _get_test_email_addresses(self):
2323
self.ensure_one()
24-
if self.from_filter or not self.env.user.email:
25-
return super()._get_test_email_addresses()
2624
email_to = "noreply@odoo.com"
25+
# if server forces a sender, use it.
26+
if self.smtp_from:
27+
return self.smtp_from, email_to
28+
if not self.env.user.email:
29+
return super()._get_test_email_addresses()
2730
email_from = self.env.user.company_aware_email()
2831
email_domain = email_domain_extract(email_from)
2932
valid_domains = self._get_domain_whitelist(self.domain_whitelist)
3033
if email_domain not in valid_domains:
3134
raise ValidationError(
32-
_(
33-
"Domain %s not whitelisted on this server",
34-
email_domain,
35-
)
35+
_("Domain %s not whitelisted on this server") % email_domain
3636
)
3737
return email_from, email_to
3838

mail_company_aware_sender/models/mail_thread.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
class MailThread(models.AbstractModel):
77
_inherit = "mail.thread"
88

9-
def _message_compute_author(self, author_id, email_from, raise_on_email=True):
9+
def _message_compute_author(self, author_id, email_from, raise_exception=True):
1010
"""Set email from using company email domain.
1111
1212
We will NOT override an explicitly passed email_from.
@@ -16,7 +16,7 @@ def _message_compute_author(self, author_id, email_from, raise_on_email=True):
1616
"""
1717
email_passed = bool(email_from)
1818
author_id, email_from = super()._message_compute_author(
19-
author_id, email_from, raise_on_email=raise_on_email
19+
author_id, email_from, raise_exception=raise_exception
2020
)
2121
if (not email_passed) and author_id:
2222
author = self.env["res.partner"].browse(author_id)

mail_company_aware_sender/tests/common.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# Copyright 2025 Therp BV <https://therp.nl>.
22
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
3-
from odoo import Command
4-
from odoo.tests import TransactionCase
3+
from odoo.tests.common import SavepointCase
54

65

7-
class CompanyAwareSenderCase(TransactionCase):
6+
class CompanyAwareSenderCase(SavepointCase):
87
@classmethod
98
def setUpClass(cls):
109
super().setUpClass()
@@ -47,7 +46,7 @@ def setUpClass(cls):
4746
"email": "charlemagne@therp.nl",
4847
"company_id": cls.company_kingdom.id,
4948
"company_ids": [
50-
Command.set([cls.company_kingdom.id, cls.company_imperium.id]),
49+
(6, 0, [cls.company_kingdom.id, cls.company_imperium.id]),
5150
],
5251
}
5352
)

mail_company_aware_sender/tests/test_company_aware_sender.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
# Copyright 2025 Therp BV <https://therp.nl>.
22
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
3+
from email.utils import parseaddr
4+
35
from .common import CompanyAwareSenderCase
46

57

68
class TestCompanyAwareSender(CompanyAwareSenderCase):
9+
def _assert_email(self, email_from, expected_email, expected_name=None):
10+
"""Assert email_from matches expected parts, tolerant to quoting differences."""
11+
name, email = parseaddr(email_from or "")
12+
self.assertEqual(email, expected_email)
13+
if expected_name is not None:
14+
self.assertEqual(name, expected_name)
15+
716
def test_nothing_changed(self):
817
# Check with default user and author (current user).
918
mail_thread = (
@@ -13,12 +22,12 @@ def test_nothing_changed(self):
1322
)
1423
author_id, email_from = mail_thread._message_compute_author(None, None)
1524
self.assertEqual(author_id, self.partner_charles.id)
16-
self.assertEqual(email_from, '"Charles Le Magne" <charlemagne@therp.nl>')
25+
self._assert_email(email_from, "charlemagne@therp.nl", "Charles Le Magne")
1726
author_id, email_from = mail_thread._message_compute_author(
1827
None, '"Unknown Person" <unknown.person@example.com>'
1928
)
2029
self.assertEqual(author_id, False)
21-
self.assertEqual(email_from, '"Unknown Person" <unknown.person@example.com>')
30+
self._assert_email(email_from, "unknown.person@example.com", "Unknown Person")
2231

2332
def test_company_overwrite(self):
2433
# Check with default user and author (current user).
@@ -30,25 +39,25 @@ def test_company_overwrite(self):
3039
# Should not work if domain not whitelisted.
3140
author_id, email_from = mail_thread._message_compute_author(None, None)
3241
self.assertEqual(author_id, self.partner_charles.id)
33-
self.assertEqual(email_from, '"Charles Le Magne" <charlemagne@therp.nl>')
42+
self._assert_email(email_from, "charlemagne@therp.nl", "Charles Le Magne")
3443
# Whitelist domain.
3544
self.mail_server.write(
3645
{"domain_whitelist": "therp.nl,kingdom.fr,imperiumromanum.org"}
3746
)
3847
author_id, email_from = mail_thread._message_compute_author(None, None)
3948
self.assertEqual(author_id, self.partner_charles.id)
40-
self.assertEqual(email_from, "charlemagne@imperiumromanum.org")
49+
self._assert_email(email_from, "charlemagne@imperiumromanum.org")
4150
self.company_imperium.write({"format_email": True})
4251
author_id, email_from = mail_thread._message_compute_author(None, None)
4352
self.assertEqual(author_id, self.partner_charles.id)
44-
self.assertEqual(
45-
email_from, '"Charles Le Magne" <charlemagne@imperiumromanum.org>'
53+
self._assert_email(
54+
email_from, "charlemagne@imperiumromanum.org", "Charles Le Magne"
4655
)
4756
# Now opt out for the override.
4857
self.partner_charles.write({"fixed_email": True})
4958
author_id, email_from = mail_thread._message_compute_author(None, None)
5059
self.assertEqual(author_id, self.partner_charles.id)
51-
self.assertEqual(email_from, '"Charles Le Magne" <charlemagne@therp.nl>')
60+
self._assert_email(email_from, "charlemagne@therp.nl", "Charles Le Magne")
5261

5362
def test_get_sender_from_object(self):
5463
# Whitelist domain.
@@ -60,4 +69,4 @@ def test_get_sender_from_object(self):
6069
himiltrude = self.partner_himiltrude.sudo().with_company(main_company)
6170
# Make sure user and company from object used.
6271
email_from = self.env.user.sudo().get_company_aware_email(himiltrude)
63-
self.assertEqual(email_from, "charlemagne@imperiumromanum.org")
72+
self._assert_email(email_from, "charlemagne@imperiumromanum.org")

mail_company_aware_sender/tests/test_mail_server.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,46 @@
11
# Copyright 2025 Therp BV <https://therp.nl>.
22
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
3+
from email.utils import parseaddr
4+
35
from odoo.exceptions import ValidationError
46

57
from .common import CompanyAwareSenderCase
68

79

810
class TestMailServer(CompanyAwareSenderCase):
11+
def _assert_email(self, email_from, expected_email, expected_name=None):
12+
"""Assert email_from matches expected parts, tolerant to quoting differences."""
13+
name, email = parseaddr(email_from or "")
14+
self.assertEqual(email, expected_email)
15+
if expected_name is not None:
16+
self.assertEqual(name, expected_name)
17+
918
def test_test_email_adresses(self):
1019
# Whitelist domain, and enable from address.
1120
self.mail_server.write(
1221
{
13-
"from_filter": "info@therp.nl",
22+
"smtp_from": "info@therp.nl",
1423
"domain_whitelist": "therp.nl,kingdom.fr,imperiumromanum.org",
1524
}
1625
)
1726
email_from, email_to = self.mail_server._get_test_email_addresses()
1827
self.assertEqual(email_from, "info@therp.nl")
1928
self.assertEqual(email_to, "noreply@odoo.com")
20-
# Disable from_filter and remove therp.nl from whitelist.
29+
# Disable smtp_from and remove therp.nl from whitelist.
2130
self.mail_server.write(
2231
{
23-
"from_filter": "info@therp.nl",
32+
"smtp_from": "info@therp.nl",
2433
"domain_whitelist": "kingdom.fr,imperiumromanum.org",
2534
}
2635
)
27-
self.mail_server.write({"from_filter": False})
36+
self.mail_server.write({"smtp_from": False})
2837
# Partner charles in imperium should use company aware from.
2938
email_from, email_to = (
3039
self.mail_server.with_user(self.user_charles)
3140
.with_company(self.company_imperium)
3241
._get_test_email_addresses()
3342
)
34-
self.assertEqual(email_from, "charlemagne@imperiumromanum.org")
43+
self._assert_email(email_from, "charlemagne@imperiumromanum.org")
3544
self.assertEqual(email_to, "noreply@odoo.com")
3645
# There should be an exception when using an invalid email domain.
3746
self.company_imperium.write({"email": "court@aachen.de"})

0 commit comments

Comments
 (0)