Skip to content

[17.0][IMP] mail_operating_unit: support single operating_unit_ids and add OU mail server routing#836

Open
yankinmax wants to merge 1 commit intoOCA:17.0from
camptocamp:imp-mail_operating_unit-partner-ous
Open

[17.0][IMP] mail_operating_unit: support single operating_unit_ids and add OU mail server routing#836
yankinmax wants to merge 1 commit intoOCA:17.0from
camptocamp:imp-mail_operating_unit-partner-ous

Conversation

@yankinmax
Copy link
Copy Markdown

@yankinmax yankinmax commented Apr 8, 2026

This PR extends mail_operating_unit in two ways.

First, it adds support for records using operating_unit_ids in addition to operating_unit_id. When a mail.thread record has exactly one operating unit in operating_unit_ids and this operating unit has an alias domain, the module now uses that alias domain for mail generation. This covers models such as res.partner, which store operating units on a many2many field and are used by reminder emails. The existing behavior for operating_unit_id remains unchanged.

Second, it adds optional outgoing mail server routing based on the operating unit. When enabled in General Settings, and when no outgoing mail server is explicitly defined on the email template, the module selects the mail server from the target record:

  • from operating_unit_id when it has a configured mail server;
  • from operating_unit_ids when all operating units share the same mail server.

This allows emails such as reminders or invoices to be routed automatically through the proper SMTP server for each operating unit, while preserving template priority and falling back to standard Odoo behavior when no unambiguous operating-unit configuration can be determined.

@yankinmax yankinmax changed the title [17.0][IMP] mail_operating_unit: support single operating_unit_ids [17.0][IMP] mail_operating_unit: support single operating_unit_ids and add OU mail server routing Apr 8, 2026
@yankinmax yankinmax marked this pull request as draft April 8, 2026 13:52
@yankinmax yankinmax force-pushed the imp-mail_operating_unit-partner-ous branch from a606c6b to 71dd976 Compare April 9, 2026 06:16
@yankinmax yankinmax marked this pull request as ready for review April 9, 2026 06:17
@yankinmax yankinmax force-pushed the imp-mail_operating_unit-partner-ous branch from 71dd976 to 4f23ee2 Compare April 9, 2026 07:57
@yankinmax yankinmax force-pushed the imp-mail_operating_unit-partner-ous branch from 4f23ee2 to d38676f Compare April 9, 2026 08:28
a configured outgoing mail server, use it.
If the record has ``operating_unit_ids`` and all operating units
share the same outgoing mail server, use that server.
Otherwise return `False`.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Otherwise return `False`.
Otherwise, try to use the one of the operating unit the current user is logged on.

In case records have different mail servers, perhaps we should use the one linked to the currently logged company/operating unit, by calling the method res.users._get_default_operating_unit.

Because, if a record has multiple operating_unit_ids, it just means it's available on all of them.. but the currently active company (and thus, active operating unit) is the one that matters IMO.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. It was originally designed to return False for the mail alias domain, so I decided to follow the idea.

Otherwise return `False`.
"""
record.ensure_one()
if "operating_unit_id" in record and record.operating_unit_id:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ , check record._fields instead

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: There are other cases like this

email_values=None,
email_layout_xmlid=False,
):
# OVERRIDE: to allow routing outgoing mail server from the operating unit.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should exhaust all options before going for a complete overwrite of this method. It will be a pain to maintain in the long run.

Have you checked how the mail_outbound_static module handles this? I think it may apply in this case, too: https://github.com/OCA/social/blob/17.0/mail_outbound_static/models/ir_mail_server.py#L112

At that stage, you may be able to grab a reference to the record linked to the message (perhaps through the message['object_id']), and derive the mail server from there.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I know we should avoid that.
I didn't find any other way so far to change the mail_server_id before mail is actually sent.
I'll check your suggestion.
Thanks for the review.

email_layout_xmlid=email_layout_xmlid,
)

def _is_operating_unit_use_mail_server_enabled(self):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO this settings is useless.

It should always attempt to do this.

Users can decide whether to enable/disable this feature by simply not configuring any mail_server_id in their operating units.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feature is requested in the BA specifications 😄
I agree we can just decide by the mail_server_id set on the OU.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants