diff --git a/mail_autogenerated_header/README.rst b/mail_autogenerated_header/README.rst new file mode 100644 index 000000000..d7c1a6739 --- /dev/null +++ b/mail_autogenerated_header/README.rst @@ -0,0 +1,105 @@ +.. image:: https://odoo-community.org/readme-banner-image + :target: https://odoo-community.org/get-involved?utm_source=readme + :alt: Odoo Community Association + +===================== +Autogenerated headers +===================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:0dd822572bebd34c9e428fe8f7cd191012396cd99b1655fa61c5cbd8dd572bf8 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/license-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fmail-lightgray.png?logo=github + :target: https://github.com/OCA/mail/tree/19.0/mail_autogenerated_header + :alt: OCA/mail +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/mail-19-0/mail-19-0-mail_autogenerated_header + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/mail&target_branch=19.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module was written to mark Odoo's emails as being autogenerated +according to ``RFC 3834 ``\ \_, +section 5. This allows receiving mail servers to act accordingly by for +example not sending a vacation autoreply. + +On the receiving side, this module drops all notifications for +autogenerated incoming e-mails. + +The combination of both avoids possible mail loops with misconfigured or +broken email servers on the opposite side. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +For sending outgoing emails, users can change the config parameter +``mail_autogenerated_header.allow_autogenerated_outgoing`` to ``False`` +to avoid any outgoing emails being marked as autogenerated. For only +avoiding in certain cases, developers can set the context flag +``mail_autogenerated_header_disable`` in calls to ``send_email`` in +order to suppress adding any autogenerated headers. + +For receiving incoming emails, developers can override the method +``_message_route_process_autoreply`` to fine tune dropping autogenerated +mails per model. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* Hunki Enterprises BV +* Therp BV + +Contributors +------------ + +- Holger Brunn + (https://hunki-enterprises.com) + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/mail `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/mail_autogenerated_header/__init__.py b/mail_autogenerated_header/__init__.py new file mode 100644 index 000000000..ffe1a38a3 --- /dev/null +++ b/mail_autogenerated_header/__init__.py @@ -0,0 +1,4 @@ +# Copyright 2018 Therp BV +# Copyright 2022 Hunki Enterprises BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from . import models diff --git a/mail_autogenerated_header/__manifest__.py b/mail_autogenerated_header/__manifest__.py new file mode 100644 index 000000000..9ec9b6ab6 --- /dev/null +++ b/mail_autogenerated_header/__manifest__.py @@ -0,0 +1,14 @@ +# Copyright 2018 Therp BV +# Copyright 2022 Hunki Enterprises BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +{ + "name": "Autogenerated headers", + "version": "19.0.1.0.0", + "author": "Hunki Enterprises BV, Therp BV,Odoo Community Association (OCA)", + "website": "https://github.com/OCA/mail", + "license": "AGPL-3", + "category": "Tools", + "summary": "Add headers to Odoo's mails indicating they are autogenerated", + "depends": ["mail"], + "data": ["data/ir_config_parameter.xml"], +} diff --git a/mail_autogenerated_header/data/ir_config_parameter.xml b/mail_autogenerated_header/data/ir_config_parameter.xml new file mode 100644 index 000000000..8d4cfc3e6 --- /dev/null +++ b/mail_autogenerated_header/data/ir_config_parameter.xml @@ -0,0 +1,7 @@ + + + + mail_autogenerated_header.allow_autogenerated_outgoing + True + + diff --git a/mail_autogenerated_header/i18n/it.po b/mail_autogenerated_header/i18n/it.po new file mode 100644 index 000000000..5c50400c8 --- /dev/null +++ b/mail_autogenerated_header/i18n/it.po @@ -0,0 +1,27 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * mail_autogenerated_header +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 17.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2024-11-25 11:06+0000\n" +"Last-Translator: mymage \n" +"Language-Team: none\n" +"Language: it\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" +"X-Generator: Weblate 5.6.2\n" + +#. module: mail_autogenerated_header +#: model:ir.model,name:mail_autogenerated_header.model_mail_thread +msgid "Email Thread" +msgstr "Discussione e-mail" + +#. module: mail_autogenerated_header +#: model:ir.model,name:mail_autogenerated_header.model_ir_mail_server +msgid "Mail Server" +msgstr "Server di posta" diff --git a/mail_autogenerated_header/i18n/mail_autogenerated_header.pot b/mail_autogenerated_header/i18n/mail_autogenerated_header.pot new file mode 100644 index 000000000..fea32471f --- /dev/null +++ b/mail_autogenerated_header/i18n/mail_autogenerated_header.pot @@ -0,0 +1,24 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * mail_autogenerated_header +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 18.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: mail_autogenerated_header +#: model:ir.model,name:mail_autogenerated_header.model_mail_thread +msgid "Email Thread" +msgstr "" + +#. module: mail_autogenerated_header +#: model:ir.model,name:mail_autogenerated_header.model_ir_mail_server +msgid "Mail Server" +msgstr "" diff --git a/mail_autogenerated_header/models/__init__.py b/mail_autogenerated_header/models/__init__.py new file mode 100644 index 000000000..0ce24b0b1 --- /dev/null +++ b/mail_autogenerated_header/models/__init__.py @@ -0,0 +1,5 @@ +# Copyright 2018 Therp BV +# Copyright 2022 Hunki Enterprises BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from . import ir_mail_server +from . import mail_thread diff --git a/mail_autogenerated_header/models/ir_mail_server.py b/mail_autogenerated_header/models/ir_mail_server.py new file mode 100644 index 000000000..08dfcb4ff --- /dev/null +++ b/mail_autogenerated_header/models/ir_mail_server.py @@ -0,0 +1,96 @@ +# Copyright 2018 Therp BV +# Copyright 2022 Hunki Enterprises BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from odoo import api, models +from odoo.fields import Domain + + +class IrMailServer(models.Model): + _inherit = "ir.mail_server" + + @api.model + def send_email( + self, + message, + mail_server_id=None, + smtp_server=None, + smtp_port=None, + smtp_user=None, + smtp_password=None, + smtp_encryption=None, + smtp_ssl_certificate=None, + smtp_ssl_private_key=None, + smtp_debug=False, + smtp_session=None, + ): + # Inject autogenerated header for outgoing mails + allow_autogenerated_outgoing = ( + self.env["ir.config_parameter"] + .sudo() + .get_param("mail_autogenerated_header.allow_autogenerated_outgoing", False) + ) + if allow_autogenerated_outgoing: + mail_autogenerated_header_disable = self.env.context.get( + "mail_autogenerated_header_disable" + ) + if ( + not mail_autogenerated_header_disable + and self._send_email_set_autogenerated( + message, + mail_server_id=mail_server_id, + smtp_server=smtp_server, + smtp_port=smtp_port, + smtp_user=smtp_user, + smtp_password=smtp_password, + smtp_encryption=smtp_encryption, + smtp_ssl_certificate=smtp_ssl_certificate, + smtp_ssl_private_key=smtp_ssl_private_key, + smtp_debug=smtp_debug, + smtp_session=smtp_session, + ) + ): + # MS Exchange's broken version as of + # http://blogs.technet.com/b/exchange/archive/2006/10/06/ + # 3395024.aspx + message["Precedence"] = "bulk" + message["X-Auto-Response-Suppress"] = "OOF" + # The right way to do it as of + # https://tools.ietf.org/html/rfc3834 + message["Auto-Submitted"] = "auto-generated" + + return super().send_email( + message, + mail_server_id=mail_server_id, + smtp_server=smtp_server, + smtp_port=smtp_port, + smtp_user=smtp_user, + smtp_password=smtp_password, + smtp_encryption=smtp_encryption, + smtp_ssl_certificate=smtp_ssl_certificate, + smtp_ssl_private_key=smtp_ssl_private_key, + smtp_debug=smtp_debug, + smtp_session=smtp_session, + ) + + @api.model + def _send_email_set_autogenerated( + self, + message, + mail_server_id=None, + smtp_server=None, + smtp_port=None, + smtp_user=None, + smtp_password=None, + smtp_encryption=None, + smtp_ssl_certificate=None, + smtp_ssl_private_key=None, + smtp_debug=False, + smtp_session=None, + ): + # Determine if some mail should have the autogenerated headers + mail = self.env["mail.mail"].search( + Domain("message_id", "=", message["Message-Id"]) + ) + if not mail: + return False + return mail.subtype_id != self.env.ref("mail.mt_comment") diff --git a/mail_autogenerated_header/models/mail_thread.py b/mail_autogenerated_header/models/mail_thread.py new file mode 100644 index 000000000..7b6338b2c --- /dev/null +++ b/mail_autogenerated_header/models/mail_thread.py @@ -0,0 +1,46 @@ +# Copyright 2018 Therp BV +# Copyright 2022 Hunki Enterprises BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +import logging + +from odoo import api, models + +_logger = logging.getLogger(__name__) + + +class MailThread(models.AbstractModel): + _inherit = "mail.thread" + + @api.model + def _message_route_process(self, message, message_dict, routes): + # Set context key to suppress notification for autogenerated incoming mails + if self._message_route_process_autoreply(message, message_dict, routes): + _logger.info( + "Ignoring email %s from %s because it seems to be an auto reply", + message.get("Message-ID"), + message.get("From"), + ) + self = self.with_context(mail_autogenerated_header=message) + return super()._message_route_process( + message, + message_dict, + routes, + ) + + def _notify_thread(self, message, msg_vals=False, **kwargs): + # Inhibit notifications if this is the notification for an incoming + # autogenerated mail from another system + if self.env.context.get("mail_autogenerated_header"): + return False + return super()._notify_thread(message, msg_vals=msg_vals, **kwargs) + + @api.model + def _message_route_process_autoreply(self, message, message_dict, routes): + """Determine if some message is an autoreply""" + return ( + message["Auto-Submitted"] + and message["Auto-Submitted"] != "no" + or message["X-Auto-Response-Suppress"] + and set(message["X-Auto-Response-Suppress"].split(", ")) + & set(["AutoReply", "All"]) + ) diff --git a/mail_autogenerated_header/pyproject.toml b/mail_autogenerated_header/pyproject.toml new file mode 100644 index 000000000..4231d0ccc --- /dev/null +++ b/mail_autogenerated_header/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/mail_autogenerated_header/readme/CONTRIBUTORS.md b/mail_autogenerated_header/readme/CONTRIBUTORS.md new file mode 100644 index 000000000..577341c32 --- /dev/null +++ b/mail_autogenerated_header/readme/CONTRIBUTORS.md @@ -0,0 +1,2 @@ +- Holger Brunn \<\> + () diff --git a/mail_autogenerated_header/readme/DESCRIPTION.md b/mail_autogenerated_header/readme/DESCRIPTION.md new file mode 100644 index 000000000..9f26a0b86 --- /dev/null +++ b/mail_autogenerated_header/readme/DESCRIPTION.md @@ -0,0 +1,9 @@ +This module was written to mark Odoo's emails as being autogenerated according +to `RFC 3834 `_, section 5. This allows +receiving mail servers to act accordingly by for example not sending a vacation autoreply. + +On the receiving side, this module drops all notifications for +autogenerated incoming e-mails. + +The combination of both avoids possible mail loops with misconfigured or +broken email servers on the opposite side. diff --git a/mail_autogenerated_header/readme/USAGE.md b/mail_autogenerated_header/readme/USAGE.md new file mode 100644 index 000000000..d0e8e8885 --- /dev/null +++ b/mail_autogenerated_header/readme/USAGE.md @@ -0,0 +1,10 @@ +For sending outgoing emails, users can change the config parameter +``mail_autogenerated_header.allow_autogenerated_outgoing`` to ``False`` to +avoid any outgoing emails being marked as autogenerated. For only avoiding +in certain cases, developers can set the context flag +``mail_autogenerated_header_disable`` in calls to ``send_email`` in order to +suppress adding any autogenerated headers. + +For receiving incoming emails, developers can override the method +``_message_route_process_autoreply`` to fine tune dropping autogenerated mails +per model. diff --git a/mail_autogenerated_header/readme/newsfragments/.gitkeep b/mail_autogenerated_header/readme/newsfragments/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/mail_autogenerated_header/static/description/icon.png b/mail_autogenerated_header/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/mail_autogenerated_header/static/description/icon.png differ diff --git a/mail_autogenerated_header/static/description/index.html b/mail_autogenerated_header/static/description/index.html new file mode 100644 index 000000000..4a62c4fb3 --- /dev/null +++ b/mail_autogenerated_header/static/description/index.html @@ -0,0 +1,451 @@ + + + + + +README.rst + + + +
+ + + +Odoo Community Association + +
+

Autogenerated headers

+ +

Beta License: AGPL-3 OCA/mail Translate me on Weblate Try me on Runboat

+

This module was written to mark Odoo’s emails as being autogenerated +according to RFC 3834 <https://tools.ietf.org/html/rfc3834>_, +section 5. This allows receiving mail servers to act accordingly by for +example not sending a vacation autoreply.

+

On the receiving side, this module drops all notifications for +autogenerated incoming e-mails.

+

The combination of both avoids possible mail loops with misconfigured or +broken email servers on the opposite side.

+

Table of contents

+ +
+

Usage

+

For sending outgoing emails, users can change the config parameter +mail_autogenerated_header.allow_autogenerated_outgoing to False +to avoid any outgoing emails being marked as autogenerated. For only +avoiding in certain cases, developers can set the context flag +mail_autogenerated_header_disable in calls to send_email in +order to suppress adding any autogenerated headers.

+

For receiving incoming emails, developers can override the method +_message_route_process_autoreply to fine tune dropping autogenerated +mails per model.

+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Hunki Enterprises BV
  • +
  • Therp BV
  • +
+
+ +
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/mail project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+
+ + diff --git a/mail_autogenerated_header/tests/__init__.py b/mail_autogenerated_header/tests/__init__.py new file mode 100644 index 000000000..b707cdfc9 --- /dev/null +++ b/mail_autogenerated_header/tests/__init__.py @@ -0,0 +1,4 @@ +# Copyright 2018 Therp BV +# Copyright 2022 Hunki Enterprises BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from . import test_mail_autogenerated_header diff --git a/mail_autogenerated_header/tests/test_mail_autogenerated_header.py b/mail_autogenerated_header/tests/test_mail_autogenerated_header.py new file mode 100644 index 000000000..2f297c1bd --- /dev/null +++ b/mail_autogenerated_header/tests/test_mail_autogenerated_header.py @@ -0,0 +1,117 @@ +# Copyright 2018 Therp BV +# Copyright 2022 Hunki Enterprises BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +import copy + +from odoo.fields import Command +from odoo.tools.mail import generate_tracking_message_id + +from odoo.addons.base.tests.common import BaseCommon + + +class TestMailAutogeneratedHeader(BaseCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.mail = cls.env["mail.mail"].create( + { + "subject": "testmessage", + "email_from": "test@test.com", + "email_to": "test@test.com", + "message_id": "message_id", + } + ) + cls.base_message = cls.env["ir.mail_server"]._build_email__( + [cls.mail.email_from], + [cls.mail.email_to], + cls.mail.subject, + "", + message_id=cls.mail.message_id, + ) + + cls.partner = cls.env["res.partner"].create( + { + "name": "Marc Demo", + "company_id": cls.env.ref("base.main_company").id, + "company_name": "YourCompany", + "street": "3575 Buena Vista Avenue", + "city": "Eugene", + "state_id": cls.env.ref("base.state_us_32").id, + "zip": "97401", + "country_id": cls.env.ref("base.us").id, + "tz": "Europe/Brussels", + "email": "mark.brown23@example.com", + "phone": "(441)-695-2334", + } + ) + cls.user = cls.env["res.users"].create( + { + "partner_id": cls.partner.id, + "login": "demo", + "password": "demo", + "signature": "--
Mr Demo
", + "company_id": cls.env.ref("base.main_company").id, + "group_ids": [ + Command.set( + [ + cls.env.ref("base.group_user").id, + cls.env.ref("base.group_partner_manager").id, + cls.env.ref("base.group_allow_export").id, + ] + ) + ], + } + ) + + def test_sending(self): + """Test that sending a mail has the Auto-Submitted header""" + self.message = copy.deepcopy(self.base_message) + self.env["ir.mail_server"].send_email(self.message) + self.assertEqual(self.message["Auto-Submitted"], "auto-generated") + # Test that the header is not set when the parameter is disabled + self.env["ir.config_parameter"].sudo().set_param( + "mail_autogenerated_header.allow_autogenerated_outgoing", False + ) + self.message = copy.deepcopy(self.base_message) + self.env["ir.mail_server"].send_email(self.message) + self.assertNotIn("Auto-Submitted", self.message) + + def test_receiving(self): + """Test that receiving mails with some auto submitted marker won't + cause new notifications being sent""" + self.message = copy.deepcopy(self.base_message) + self.message.replace_header("Message-Id", generate_tracking_message_id(42)) + self.env["mail.notification"].search( + [("res_partner_id", "=", self.user.partner_id.id)] + ).unlink() + + partner_id = self.env["mail.thread"].message_process( + "res.partner", self.message.as_string() + ) + partner = self.env["res.partner"].browse(partner_id) + partner.message_subscribe(partner_ids=self.user.partner_id.ids) + reply = self.message + reply["References"] = self.message["Message-Id"] + reply.replace_header("Message-Id", "message_id3") + thread_id = self.env["mail.thread"].message_process( + "res.partner", reply.as_string() + ) + self.assertEqual(thread_id, partner.id) + notifications = self.env["mail.notification"].search( + [("res_partner_id", "=", self.user.partner_id.id)] + ) + # mail is not autogenerated, should have generated mails + self.assertTrue(notifications.notification_type == "email") + notifications.unlink() + + reply.replace_header("Message-Id", "message_id4") + reply["Auto-Submitted"] = "auto-generated" + thread_id = self.env["mail.thread"].message_process( + "res.partner", reply.as_string() + ) + self.assertEqual(thread_id, partner.id) + notifications = self.env["mail.notification"].search( + [("res_partner_id", "=", self.user.partner_id.id)] + ) + # mail is autogenerated, shouldn't have generated mails + self.assertFalse(notifications.notification_type == "email")