Repository navigation
[ADD] mail_incoming_no_forward: don't forward incoming mails to followers #369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
divad1196
wants to merge
2
commits into
OCA:18.0
Choose a base branch
from
camptocamp:18.0-add-mail_incoming_no_forward
base: 18.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+247
−2
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from . import models |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # Copyright 2026 Camptocamp SA | ||
| # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). | ||
| { | ||
| "name": "Incoming Mail No Forward", | ||
| "summary": "Don't forward incoming mails to the followers", | ||
| "version": "18.0.1.0.0", | ||
| "author": "Camptocamp,Odoo Community Association (OCA)", | ||
| "website": "https://github.com/OCA/mail", | ||
| "license": "AGPL-3", | ||
| "category": "Tools", | ||
| "depends": ["mail"], | ||
| "data": ["views/res_config_settings_views.xml"], | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| from . import mail_thread | ||
| from . import res_company | ||
| from . import res_config_settings |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # Copyright 2026 Camptocamp SA | ||
| # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). | ||
|
|
||
| from odoo import models | ||
|
|
||
|
|
||
| class MailThread(models.AbstractModel): | ||
| _inherit = "mail.thread" | ||
|
|
||
| def _notify_get_recipients(self, message, msg_vals, **kwargs): | ||
| recipients = super()._notify_get_recipients(message, msg_vals, **kwargs) | ||
| message_type = ( | ||
| msg_vals.get("message_type") if msg_vals else message.sudo().message_type | ||
| ) | ||
| if message_type == "email" and self.env.company.disable_incoming_mail_forward: | ||
| recipients = self._filter_forward_recipients(recipients) | ||
| return recipients | ||
|
|
||
| def _filter_forward_recipients(self, recipients): | ||
| """Don't forward incoming mails to external followers. | ||
|
|
||
| The forward leaks replies meant for the company only | ||
| and is flagged as impersonation by some mail servers. | ||
|
|
||
| Senders must decide who receives the message (e.g. "Reply all"). | ||
| """ | ||
| notify_internal = self.env.company.notify_internal_on_incoming_email | ||
| return [ | ||
| recipient | ||
| for recipient in recipients | ||
| if recipient["notif"] != "email" | ||
| or (notify_internal and not recipient["share"]) | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # Copyright 2026 Camptocamp SA | ||
| # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). | ||
|
|
||
| from odoo import fields, models | ||
|
|
||
|
|
||
| class ResCompany(models.Model): | ||
| _inherit = "res.company" | ||
|
|
||
| disable_incoming_mail_forward = fields.Boolean( | ||
| string="Don't forward incoming mails to followers", | ||
| default=True, | ||
| help="Odoo forwards incoming mails to all the followers by email.", | ||
| ) | ||
| notify_internal_on_incoming_email = fields.Boolean( | ||
| string="Email internal users about incoming mails", | ||
| default=True, | ||
| help="Forward incoming mails to internal followers notified by email.", | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # Copyright 2026 Camptocamp SA | ||
| # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). | ||
|
|
||
| from odoo import fields, models | ||
|
|
||
|
|
||
| class ResConfigSettings(models.TransientModel): | ||
| _inherit = "res.config.settings" | ||
|
|
||
| disable_incoming_mail_forward = fields.Boolean( | ||
| related="company_id.disable_incoming_mail_forward", | ||
| readonly=False, | ||
| ) | ||
| notify_internal_on_incoming_email = fields.Boolean( | ||
| related="company_id.notify_internal_on_incoming_email", | ||
| readonly=False, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| [build-system] | ||
| requires = ["whool"] | ||
| build-backend = "whool.buildapi" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| Go to *Settings > General Settings > Discuss*: | ||
|
|
||
| - *Don't forward incoming mails to followers*: enabled by default. | ||
| Disable it to get the standard Odoo behaviour back. | ||
| - *Email internal users about incoming mails*: only visible when the forward is | ||
| disabled. Enabled by default: internal followers notified by email keep | ||
| receiving the incoming mails. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| - [Camptocamp](https://www.camptocamp.com): | ||
| - David Gallay |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| When Odoo receives a mail on a record, it forwards a copy to every follower of | ||
| the record, sent as `<sender name> <catchall address>`. | ||
|
|
||
| This copy: | ||
|
|
||
| - leaks replies meant for the company only to the other external followers; | ||
| - is flagged as impersonation by some mail servers (e.g. Microsoft Defender). | ||
|
|
||
| This module stops forwarding incoming mails to the followers by email. | ||
| The mail is still logged in the chatter, and followers notified in their Odoo | ||
| inbox are still notified. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from . import test_incoming_mail_notification |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # Copyright 2026 Camptocamp SA | ||
| # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). | ||
|
|
||
| from odoo.tools import file_open | ||
|
|
||
|
|
||
| def read_eml(filename, mode="r"): | ||
| """Return the content of ``mail_incoming_no_forward/tests/eml/<filename>``.""" | ||
| with file_open(f"mail_incoming_no_forward/tests/eml/{filename}", mode) as eml: | ||
| return eml.read() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| From: Sender <sender@example.com> | ||
| To: odoo@example.com | ||
| Subject: Re: Thread | ||
| Message-ID: {message_id} | ||
| In-Reply-To: {parent_id} | ||
| Content-Type: text/plain; charset=utf-8 | ||
|
|
||
| Hello back |
90 changes: 90 additions & 0 deletions
90
mail_incoming_no_forward/tests/test_incoming_mail_notification.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| # Copyright 2026 Camptocamp SA | ||
| # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). | ||
|
|
||
| from email import message_from_string | ||
|
|
||
| from odoo.tests.common import TransactionCase, new_test_user | ||
|
|
||
| from .common import read_eml | ||
|
|
||
| REPLY_TEMPLATE = read_eml("reply.eml") | ||
|
|
||
|
|
||
| class TestIncomingMailNotification(TransactionCase): | ||
| @classmethod | ||
| def setUpClass(cls): | ||
| super().setUpClass() | ||
| cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True)) | ||
| partner_model = cls.env["res.partner"] | ||
| cls.record = partner_model.create({"name": "Thread"}) | ||
| cls.sender = partner_model.create( | ||
| {"name": "Sender", "email": "sender@example.com"} | ||
| ) | ||
| cls.external = partner_model.create( | ||
| {"name": "External", "email": "external@example.com"} | ||
| ) | ||
| cls.internal_email = new_test_user( | ||
| cls.env, | ||
| login="internal_email", | ||
| email="internal_email@example.com", | ||
| groups="base.group_user", | ||
| notification_type="email", | ||
| ).partner_id | ||
| cls.internal_inbox = new_test_user( | ||
| cls.env, | ||
| login="internal_inbox", | ||
| groups="base.group_user", | ||
| notification_type="inbox", | ||
| ).partner_id | ||
| cls.record.message_subscribe( | ||
| partner_ids=( | ||
| cls.sender | cls.external | cls.internal_email | cls.internal_inbox | ||
| ).ids | ||
| ) | ||
| cls.parent = cls.record.message_post( | ||
| body="Hello", | ||
| message_type="comment", | ||
| subtype_xmlid="mail.mt_comment", | ||
| ) | ||
|
|
||
| def _notified(self, message): | ||
| return { | ||
| notification.res_partner_id: notification.notification_type | ||
| for notification in message.notification_ids | ||
| } | ||
|
|
||
| def _receive_reply(self): | ||
| message_id = f"<{self._testMethodName}@example.com>" | ||
| raw = REPLY_TEMPLATE.format( | ||
| message_id=message_id, parent_id=self.parent.message_id | ||
| ) | ||
| self.assertEqual( | ||
| message_from_string(raw)["In-Reply-To"], self.parent.message_id | ||
| ) | ||
| self.env["mail.thread"].message_process("res.partner", raw) | ||
| reply = self.env["mail.message"].search([("message_id", "=", message_id)]) | ||
| self.assertEqual(reply.res_id, self.record.id) | ||
| return self._notified(reply) | ||
|
|
||
| def test_incoming_mail_not_forwarded_to_externals(self): | ||
| notified = self._receive_reply() | ||
| self.assertNotIn(self.external, notified) | ||
| self.assertEqual(notified[self.internal_email], "email") | ||
| self.assertEqual(notified[self.internal_inbox], "inbox") | ||
|
|
||
| def test_incoming_mail_internal_email_disabled(self): | ||
| self.env.company.notify_internal_on_incoming_email = False | ||
| notified = self._receive_reply() | ||
| self.assertNotIn(self.external, notified) | ||
| self.assertNotIn(self.internal_email, notified) | ||
| self.assertEqual(notified[self.internal_inbox], "inbox") | ||
|
|
||
| def test_incoming_mail_forward_enabled(self): | ||
| self.env.company.disable_incoming_mail_forward = False | ||
| notified = self._receive_reply() | ||
| self.assertEqual(notified[self.external], "email") | ||
|
|
||
| def test_comment_still_sent_to_externals(self): | ||
| notified = self._notified(self.parent) | ||
| self.assertEqual(notified[self.external], "email") | ||
| self.assertEqual(notified[self.internal_email], "email") |
27 changes: 27 additions & 0 deletions
27
mail_incoming_no_forward/views/res_config_settings_views.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?xml version="1.0" encoding="utf-8" ?> | ||
| <!-- Copyright 2026 Camptocamp SA | ||
| License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). --> | ||
| <odoo> | ||
| <record id="res_config_settings_view_form" model="ir.ui.view"> | ||
| <field | ||
| name="name" | ||
| >res.config.settings.view.form.mail_incoming_no_forward</field> | ||
| <field name="model">res.config.settings</field> | ||
| <field name="inherit_id" ref="mail.res_config_settings_view_form" /> | ||
| <field name="arch" type="xml"> | ||
| <xpath expr="//block[@id='emails']" position="inside"> | ||
| <setting | ||
| help="Odoo forwards incoming mails to all the followers by email." | ||
| > | ||
| <field name="disable_incoming_mail_forward" /> | ||
| </setting> | ||
| <setting | ||
| help="Forward incoming mails to internal followers notified by email." | ||
| invisible="not disable_incoming_mail_forward" | ||
| > | ||
| <field name="notify_internal_on_incoming_email" /> | ||
| </setting> | ||
| </xpath> | ||
| </field> | ||
| </record> | ||
| </odoo> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be better to disable
mail_incoming_no_forwardmodule instead of changing what is tested on an other module.Why do we have this conflict ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test is wrong, an email coming from Odoo is never of type
email.message_type='email'is for incoming=> It must be fixed in any case
Because the test in
mail_layout_forceis wrong.It tries to send an email but marks it as an incoming email (
message_type='email'is for incoming).This module prevents forwarding an incoming email to the followers
-> When it sees the
message_type='email', it stops the message, which is the correct behavior.