diff --git a/mail_server_by_user/README.rst b/mail_server_by_user/README.rst new file mode 100644 index 0000000000..0760a235da --- /dev/null +++ b/mail_server_by_user/README.rst @@ -0,0 +1,91 @@ +==================== +Email Server By User +==================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:9313575d1d36133e0993e203d53247a3beb4122f34e111c8418dfb07f57733c8 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Alpha-red.png + :target: https://odoo-community.org/page/development-status + :alt: Alpha +.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png + :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html + :alt: License: LGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fsocial-lightgray.png?logo=github + :target: https://github.com/OCA/social/tree/16.0/mail_server_by_user + :alt: OCA/social +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/social-16-0/social-16-0-mail_server_by_user + :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/social&target_branch=16.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Change SMTP server with smtp_user equal email of +user perform mail actions, like message_post, mail.mail send, and base email sending + +.. IMPORTANT:: + This is an alpha version, the data model and design can change at any time without warning. + Only for development or testing purpose, do not use in production. + `More details on development status `_ + +**Table of contents** + +.. contents:: + :local: + +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 +~~~~~~~ + +* ForgeFlow + +Contributors +~~~~~~~~~~~~ + +* Christopher Ormaza +* `Onestein `__ + +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. + +.. |maintainer-ChrisOForgeFlow| image:: https://github.com/ChrisOForgeFlow.png?size=40px + :target: https://github.com/ChrisOForgeFlow + :alt: ChrisOForgeFlow + +Current `maintainer `__: + +|maintainer-ChrisOForgeFlow| + +This module is part of the `OCA/social `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/mail_server_by_user/__init__.py b/mail_server_by_user/__init__.py new file mode 100644 index 0000000000..0650744f6b --- /dev/null +++ b/mail_server_by_user/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/mail_server_by_user/__manifest__.py b/mail_server_by_user/__manifest__.py new file mode 100644 index 0000000000..25dac69041 --- /dev/null +++ b/mail_server_by_user/__manifest__.py @@ -0,0 +1,20 @@ +# Copyright 2022 ForgeFlow S.L. (https://forgeflow.com) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +{ + "name": "Email Server By User", + "summary": "Email Server By User", + "version": "16.0.1.0.0", + "category": "Social Network", + "website": "https://github.com/OCA/social", + "author": ("ForgeFlow, " "Odoo Community Association (OCA)"), + "license": "LGPL-3", + "application": False, + "installable": True, + "depends": ["base", "mail"], + "development_status": "Alpha", + "maintainers": ["ChrisOForgeFlow"], + "data": [], + "qweb": [], + "demo": [], +} diff --git a/mail_server_by_user/i18n/es.po b/mail_server_by_user/i18n/es.po new file mode 100644 index 0000000000..a00df997d7 --- /dev/null +++ b/mail_server_by_user/i18n/es.po @@ -0,0 +1,61 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * mail_server_by_user +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 14.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2024-01-28 11:33+0000\n" +"Last-Translator: Ivorra78 \n" +"Language-Team: none\n" +"Language: es\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 4.17\n" + +#. module: mail_server_by_user +#: model:ir.model.fields,field_description:mail_server_by_user.field_ir_mail_server__display_name +#: model:ir.model.fields,field_description:mail_server_by_user.field_mail_mail__display_name +#: model:ir.model.fields,field_description:mail_server_by_user.field_mail_thread__display_name +msgid "Display Name" +msgstr "Mostrar Nombre" + +#. module: mail_server_by_user +#: model:ir.model,name:mail_server_by_user.model_mail_thread +msgid "Email Thread" +msgstr "Hilo de Correo Electrónico" + +#. module: mail_server_by_user +#: model:ir.model.fields,field_description:mail_server_by_user.field_ir_mail_server__id +#: model:ir.model.fields,field_description:mail_server_by_user.field_mail_mail__id +#: model:ir.model.fields,field_description:mail_server_by_user.field_mail_thread__id +msgid "ID" +msgstr "ID" + +#. module: mail_server_by_user +#: model:ir.model.fields,field_description:mail_server_by_user.field_ir_mail_server____last_update +#: model:ir.model.fields,field_description:mail_server_by_user.field_mail_mail____last_update +#: model:ir.model.fields,field_description:mail_server_by_user.field_mail_thread____last_update +msgid "Last Modified on" +msgstr "Última Modificación el" + +#. module: mail_server_by_user +#: model:ir.model,name:mail_server_by_user.model_ir_mail_server +msgid "Mail Server" +msgstr "Servidor de Correo" + +#. module: mail_server_by_user +#: model:ir.model,name:mail_server_by_user.model_mail_mail +msgid "Outgoing Mails" +msgstr "Correos Salientes" + +#. module: mail_server_by_user +#: code:addons/mail_server_by_user/tests/test_mail_server_by_user.py:0 +#: code:addons/mail_server_by_user/tests/test_mail_server_by_user.py:0 +#: code:addons/mail_server_by_user/tests/test_mail_server_by_user.py:0 +#, python-format +msgid "Test" +msgstr "Prueba" diff --git a/mail_server_by_user/i18n/fr.po b/mail_server_by_user/i18n/fr.po new file mode 100644 index 0000000000..d6fff98bf7 --- /dev/null +++ b/mail_server_by_user/i18n/fr.po @@ -0,0 +1,61 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * mail_server_by_user +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 14.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2024-01-20 22:33+0000\n" +"Last-Translator: Claude R Perrin \n" +"Language-Team: none\n" +"Language: fr\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 4.17\n" + +#. module: mail_server_by_user +#: model:ir.model.fields,field_description:mail_server_by_user.field_ir_mail_server__display_name +#: model:ir.model.fields,field_description:mail_server_by_user.field_mail_mail__display_name +#: model:ir.model.fields,field_description:mail_server_by_user.field_mail_thread__display_name +msgid "Display Name" +msgstr "Nom affiché" + +#. module: mail_server_by_user +#: model:ir.model,name:mail_server_by_user.model_mail_thread +msgid "Email Thread" +msgstr "Flux Email" + +#. module: mail_server_by_user +#: model:ir.model.fields,field_description:mail_server_by_user.field_ir_mail_server__id +#: model:ir.model.fields,field_description:mail_server_by_user.field_mail_mail__id +#: model:ir.model.fields,field_description:mail_server_by_user.field_mail_thread__id +msgid "ID" +msgstr "ID" + +#. module: mail_server_by_user +#: model:ir.model.fields,field_description:mail_server_by_user.field_ir_mail_server____last_update +#: model:ir.model.fields,field_description:mail_server_by_user.field_mail_mail____last_update +#: model:ir.model.fields,field_description:mail_server_by_user.field_mail_thread____last_update +msgid "Last Modified on" +msgstr "Dernière modification le" + +#. module: mail_server_by_user +#: model:ir.model,name:mail_server_by_user.model_ir_mail_server +msgid "Mail Server" +msgstr "Serveur de messagerie" + +#. module: mail_server_by_user +#: model:ir.model,name:mail_server_by_user.model_mail_mail +msgid "Outgoing Mails" +msgstr "Emails sortants" + +#. module: mail_server_by_user +#: code:addons/mail_server_by_user/tests/test_mail_server_by_user.py:0 +#: code:addons/mail_server_by_user/tests/test_mail_server_by_user.py:0 +#: code:addons/mail_server_by_user/tests/test_mail_server_by_user.py:0 +#, python-format +msgid "Test" +msgstr "Test" diff --git a/mail_server_by_user/i18n/mail_server_by_user.pot b/mail_server_by_user/i18n/mail_server_by_user.pot new file mode 100644 index 0000000000..51fdedfdc9 --- /dev/null +++ b/mail_server_by_user/i18n/mail_server_by_user.pot @@ -0,0 +1,58 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * mail_server_by_user +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 14.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_server_by_user +#: model:ir.model.fields,field_description:mail_server_by_user.field_ir_mail_server__display_name +#: model:ir.model.fields,field_description:mail_server_by_user.field_mail_mail__display_name +#: model:ir.model.fields,field_description:mail_server_by_user.field_mail_thread__display_name +msgid "Display Name" +msgstr "" + +#. module: mail_server_by_user +#: model:ir.model,name:mail_server_by_user.model_mail_thread +msgid "Email Thread" +msgstr "" + +#. module: mail_server_by_user +#: model:ir.model.fields,field_description:mail_server_by_user.field_ir_mail_server__id +#: model:ir.model.fields,field_description:mail_server_by_user.field_mail_mail__id +#: model:ir.model.fields,field_description:mail_server_by_user.field_mail_thread__id +msgid "ID" +msgstr "" + +#. module: mail_server_by_user +#: model:ir.model.fields,field_description:mail_server_by_user.field_ir_mail_server____last_update +#: model:ir.model.fields,field_description:mail_server_by_user.field_mail_mail____last_update +#: model:ir.model.fields,field_description:mail_server_by_user.field_mail_thread____last_update +msgid "Last Modified on" +msgstr "" + +#. module: mail_server_by_user +#: model:ir.model,name:mail_server_by_user.model_ir_mail_server +msgid "Mail Server" +msgstr "" + +#. module: mail_server_by_user +#: model:ir.model,name:mail_server_by_user.model_mail_mail +msgid "Outgoing Mails" +msgstr "" + +#. module: mail_server_by_user +#: code:addons/mail_server_by_user/tests/test_mail_server_by_user.py:0 +#: code:addons/mail_server_by_user/tests/test_mail_server_by_user.py:0 +#: code:addons/mail_server_by_user/tests/test_mail_server_by_user.py:0 +#, python-format +msgid "Test" +msgstr "" diff --git a/mail_server_by_user/models/__init__.py b/mail_server_by_user/models/__init__.py new file mode 100644 index 0000000000..c49d28da15 --- /dev/null +++ b/mail_server_by_user/models/__init__.py @@ -0,0 +1,3 @@ +from . import ir_mail_server +from . import mail_mail +from . import mail_thread diff --git a/mail_server_by_user/models/ir_mail_server.py b/mail_server_by_user/models/ir_mail_server.py new file mode 100644 index 0000000000..2ef3017d7c --- /dev/null +++ b/mail_server_by_user/models/ir_mail_server.py @@ -0,0 +1,44 @@ +# Copyright 2022 ForgeFlow S.L. (https://forgeflow.com) +# License LGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from odoo import api, models +from odoo.tools.mail import email_normalize + + +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, + ): + email_from = message["From"] + if email_from: + mail_server_suggested = self.search( + [("smtp_user", "=", email_normalize(email_from))], limit=1 + ) + if mail_server_suggested: + mail_server_id = mail_server_suggested.id + 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, + ) diff --git a/mail_server_by_user/models/mail_mail.py b/mail_server_by_user/models/mail_mail.py new file mode 100644 index 0000000000..2ebd3bcc7f --- /dev/null +++ b/mail_server_by_user/models/mail_mail.py @@ -0,0 +1,26 @@ +# Copyright 2022 ForgeFlow S.L. (https://forgeflow.com) +# License LGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from odoo import models +from odoo.tools.mail import email_normalize + + +class MailMail(models.Model): + _inherit = "mail.mail" + + def _send(self, auto_commit=False, raise_exception=False, smtp_session=None): + mail_server_model = self.env["ir.mail_server"].sudo() + for rec in self: + if rec.email_from: + mail_server_suggested = mail_server_model.search( + [("smtp_user", "=", email_normalize(rec.email_from))], limit=1 + ) + if ( + mail_server_suggested + and rec.mail_server_id.id != mail_server_suggested.id + ): + rec.mail_server_id = mail_server_suggested.id + return super(MailMail, self)._send( + auto_commit=auto_commit, + raise_exception=raise_exception, + smtp_session=smtp_session, + ) diff --git a/mail_server_by_user/models/mail_thread.py b/mail_server_by_user/models/mail_thread.py new file mode 100644 index 0000000000..5fafb1680b --- /dev/null +++ b/mail_server_by_user/models/mail_thread.py @@ -0,0 +1,49 @@ +# Copyright 2022 ForgeFlow S.L. (https://forgeflow.com) +# License LGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo import models +from odoo.tools.mail import email_normalize + + +class MailThread(models.AbstractModel): + _inherit = "mail.thread" + + def _notify_thread_by_email( + self, + message, + recipients_data, + msg_vals=False, + mail_auto_delete=True, + model_description=False, + force_email_company=False, + force_email_lang=False, + resend_existing=False, + force_send=True, + send_after_commit=True, + subtitles=None, + **kwargs + ): + mail_server_model = self.env["ir.mail_server"].sudo() + if message.email_from: + mail_server_suggested = mail_server_model.search( + [("smtp_user", "=", email_normalize(message.email_from))], limit=1 + ) + if ( + mail_server_suggested + and message.mail_server_id.id != mail_server_suggested.id + ): + message.mail_server_id = mail_server_suggested.id + return super(MailThread, self)._notify_thread_by_email( + message, + recipients_data, + msg_vals=msg_vals, + mail_auto_delete=mail_auto_delete, + model_description=model_description, + force_email_company=force_email_company, + force_email_lang=force_email_lang, + resend_existing=resend_existing, + force_send=force_send, + send_after_commit=send_after_commit, + subtitles=subtitles, + **kwargs, + ) diff --git a/mail_server_by_user/readme/CONTRIBUTORS.rst b/mail_server_by_user/readme/CONTRIBUTORS.rst new file mode 100644 index 0000000000..5249edd7d3 --- /dev/null +++ b/mail_server_by_user/readme/CONTRIBUTORS.rst @@ -0,0 +1,2 @@ +* Christopher Ormaza +* `Onestein `__ diff --git a/mail_server_by_user/readme/DESCRIPTION.rst b/mail_server_by_user/readme/DESCRIPTION.rst new file mode 100644 index 0000000000..63d4083ca5 --- /dev/null +++ b/mail_server_by_user/readme/DESCRIPTION.rst @@ -0,0 +1,2 @@ +Change SMTP server with smtp_user equal email of +user perform mail actions, like message_post, mail.mail send, and base email sending diff --git a/mail_server_by_user/static/description/icon.png b/mail_server_by_user/static/description/icon.png new file mode 100644 index 0000000000..3a0328b516 Binary files /dev/null and b/mail_server_by_user/static/description/icon.png differ diff --git a/mail_server_by_user/static/description/index.html b/mail_server_by_user/static/description/index.html new file mode 100644 index 0000000000..beed5a1c90 --- /dev/null +++ b/mail_server_by_user/static/description/index.html @@ -0,0 +1,433 @@ + + + + + +Email Server By User + + + +
+

Email Server By User

+ + +

Alpha License: LGPL-3 OCA/social Translate me on Weblate Try me on Runboat

+

Change SMTP server with smtp_user equal email of +user perform mail actions, like message_post, mail.mail send, and base email sending

+
+

Important

+

This is an alpha version, the data model and design can change at any time without warning. +Only for development or testing purpose, do not use in production. +More details on development status

+
+

Table of contents

+ +
+

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

+
    +
  • ForgeFlow
  • +
+
+
+

Contributors

+ +
+
+

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.

+

Current maintainer:

+

ChrisOForgeFlow

+

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

+

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

+
+
+
+ + diff --git a/mail_server_by_user/tests/__init__.py b/mail_server_by_user/tests/__init__.py new file mode 100644 index 0000000000..8064b28d60 --- /dev/null +++ b/mail_server_by_user/tests/__init__.py @@ -0,0 +1 @@ +from . import test_mail_server_by_user diff --git a/mail_server_by_user/tests/test.msg b/mail_server_by_user/tests/test.msg new file mode 100644 index 0000000000..1f0b96da33 --- /dev/null +++ b/mail_server_by_user/tests/test.msg @@ -0,0 +1,70 @@ +Delivered-To: test@gmail.com +Received: by 10.74.138.167 with SMTP id m36csp7226976ooj; + Tue, 12 Sep 2017 10:37:56 -0700 (PDT) +X-Google-Smtp-Source: AOwi7QDKSb3BE6lIhVXub9wcPA/HxFKKpnNPconNr9f1L35SVw+EIm8itVQkbOdAW6TohImypmrF +X-Received: by 10.28.158.208 with SMTP id h199mr250060wme.47.1505237876258; + Tue, 12 Sep 2017 10:37:56 -0700 (PDT) +ARC-Seal: i=1; a=rsa-sha256; t=1505237876; cv=none; + d=google.com; s=arc-20160816; + b=E2B6KUxHOJQk1YrT12BpitEMCgkxyqEXcFlwPWKjA/i/Xyvlh+09spNOF4VPmD/ZJm + 5lkY6hYyxvIH2RpRPeZVPkRIYhaEASkMIygdJu9Gd4weBdO2rd8iP/zSGHYyAmO/hLN2 + 64hXtKexrWnO/YNWlpfhAo1kiwgSRVnZx55EopbWP49cy7BzKfwr1kHN0T9A5Lw1w+BW + ZrXdCX6LRxHS2USKHb76PAVt0bhwsM/ZznBauR2zNKYcPxAWzdpN/vK3BDmWUdqbbSaB + BOKINjuI9EmWynogDZE7Riu+sbc5QafE3owla1/2d0Bogp9FLtJe0YyQeW2qLvZKcmlI + ftSQ== +ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; + h=to:list-archive:list-unsubscribe:list-subscribe:precedence + :list-post:list-id:date:reply-to:from:subject:references:message-id + :mime-version:arc-authentication-results; + bh=DwvSiw5K7ryb4S8O/8HcIaGhJqbOxcXKsnPAr63iQZ4=; + b=U0Ac9Rqvv+tfqO9fCx+F79oZknn3rOv9N9ekViEuL5DtjpJxKDDkO1xw//sV3eRILT + nqGuxd2yQXwC4U+WAwraBwoLC3ScHb/9gWtzlrLCgv6WbNE7HZi5g6L8c0LWRN24cIe9 + AOdc/8fOdGoaL8yajrGEHgMz9B2KMltA9tZyxFOeKsyODxJ6iWjXcG1BSQTxERwosV3h + ch8AznQr7xLLvc/u9VTEqC5ome3RqsxKRxOGenEqIbCOr11sxwpZQdQcNR6faNRom3+2 + 6gz++4tVIV9cqYX1j9eEU/ufoUzBJ6Uzm0jMGZZQOHAF+YX3tZUEsPmc75PsvRCAIWby + urMg== +ARC-Authentication-Results: i=1; mx.google.com; + spf=pass (google.com: domain of postmaster-odoo@odoo-community.org designates 2a01:4f8:a0:430d::2 as permitted sender) smtp.mailfrom=postmaster-odoo@odoo-community.org; + dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=QUARANTINE) header.from=laslabs.com +Return-Path: +Received: from odoo-community.org (odoo-community.org. [2a01:4f8:a0:430d::2]) + by mx.google.com with ESMTP id j72si1795626wmg.60.2017.09.12.10.37.55 + for ; + Tue, 12 Sep 2017 10:37:56 -0700 (PDT) +Received-SPF: pass (google.com: domain of postmaster-odoo@odoo-community.org designates 2a01:4f8:a0:430d::2 as permitted sender) client-ip=2a01:4f8:a0:430d::2; +Authentication-Results: mx.google.com; + spf=pass (google.com: domain of postmaster-odoo@odoo-community.org designates 2a01:4f8:a0:430d::2 as permitted sender) smtp.mailfrom=postmaster-odoo@odoo-community.org; + dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=QUARANTINE) header.from=laslabs.com +Received: from odoo.odoo-community.org (localhost.localdomain [127.0.0.1]) + by odoo-community.org (Postfix) with ESMTP id DB5DC2EC2277; + Tue, 12 Sep 2017 19:37:53 +0200 (CEST) +Content-Type: multipart/mixed; boundary="===============7439524030966430607==" +MIME-Version: 1.0 +Message-Id: +references: <0db43737-b846-4890-6801-44ff9617e3b3@camptocamp.com> +Subject: Re: OCA Code sprint: sprint topics +From: Dave Lasley +Reply-To: "Odoo Community Association \(OCA\) Contributors" + +Date: Tue, 12 Sep 2017 17:37:53 -0000 +List-Id: contributors.odoo-community.org +List-Post: +Precedence: list +X-Auto-Response-Suppress: OOF +List-Subscribe: +List-Unsubscribe: +List-Archive: +To: "Contributors" + +--===============7439524030966430607== +Content-Type: multipart/alternative; + boundary="===============8317593469411551167==" +MIME-Version: 1.0 + +--===============8317593469411551167== +Content-Type: text/plain; charset="utf-8" +MIME-Version: 1.0 +Content-Transfer-Encoding: base64 + +VGhpcyBpcyBhIGZha2UsIHRlc3QgbWVzc2FnZQ== +--===============7439524030966430607==-- diff --git a/mail_server_by_user/tests/test_mail_server_by_user.py b/mail_server_by_user/tests/test_mail_server_by_user.py new file mode 100644 index 0000000000..e714449819 --- /dev/null +++ b/mail_server_by_user/tests/test_mail_server_by_user.py @@ -0,0 +1,166 @@ +# Copyright 2022 ForgeFlow S.L. +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). +import logging +import os +from email import message_from_string +from unittest.mock import MagicMock + +from odoo import _ +from odoo.tests.common import TransactionCase + +_logger = logging.getLogger(__name__) + + +class TestIrMailServer(TransactionCase): + def setUp(self): + super(TestIrMailServer, self).setUp() + self.smtp_server_model = self.env["ir.mail_server"] + self.parameter_model = self.env["ir.config_parameter"] + # self.default_template = self.env.ref("mail.message_notification_email") + # self.paynow_template = self.env.ref("mail.mail_notification_paynow") + self.server_1 = self.smtp_server_model.create( + { + "name": "localhost", + "smtp_host": "localhost", + "smtp_user": "user1@somemail.com", + } + ) + self.server_2 = self.smtp_server_model.create( + { + "name": "localhost", + "smtp_host": "localhost", + "smtp_user": "user2@somemail.com", + } + ) + self.user1 = self.env["res.users"].create( + { + "login": "user1@somemail.com", + "email": "user1@somemail.com", + "partner_id": self.env["res.partner"].create({"name": "User 1"}).id, + "groups_id": [ + ( + 6, + 0, + [ + self.env.ref("base.group_user").id, + self.env.ref("base.group_partner_manager").id, + ], + ) + ], + } + ) + self.user2 = self.env["res.users"].create( + { + "login": "user2@somemail.com", + "email": "user2@somemail.com", + "partner_id": self.env["res.partner"].create({"name": "User 2"}).id, + "groups_id": [ + ( + 6, + 0, + [ + self.env.ref("base.group_user").id, + self.env.ref("base.group_partner_manager").id, + ], + ) + ], + } + ) + self.user3 = self.env["res.users"].create( + { + "login": "user3@somemail.com", + "email": "user3@somemail.com", + "partner_id": self.env["res.partner"].create({"name": "User 3"}).id, + "groups_id": [ + ( + 6, + 0, + [ + self.env.ref("base.group_user").id, + self.env.ref("base.group_partner_manager").id, + ], + ) + ], + } + ) + self.partner = self.env.ref("base.res_partner_1") + + message_file = os.path.join( + os.path.dirname(os.path.realpath(__file__)), "test.msg" + ) + with open(message_file, "r") as fh: + self.message = message_from_string(fh.read()) + + def _send_mail(self, message=None, mail_server_id=None, smtp_server=None): + if message is None: + message = self.message + connect = MagicMock() + self.smtp_server_model._patch_method("connect", connect) + try: + self.smtp_server_model.send_email(message, mail_server_id, smtp_server) + except Exception as e: + _logger.debug(str(e)) + finally: + self.smtp_server_model._revert_method("connect") + call_args = connect.call_args + return call_args + + def test_send_email_change_smtp_server(self): + """It should inject the FROM header correctly when no canonical name.""" + self.message.replace_header("From", self.user1.login) + call_args = self._send_mail() + mail_server_id = call_args.kwargs.get("mail_server_id") + self.assertEqual(mail_server_id, self.server_1.id) + + self.message.replace_header("From", self.user2.login) + call_args = self._send_mail() + mail_server_id = call_args.kwargs.get("mail_server_id") + self.assertEqual(mail_server_id, self.server_2.id) + + self.message.replace_header("From", self.user3.login) + call_args = self._send_mail() + mail_server_id = call_args.kwargs.get("mail_server_id", False) + # With this module, you always get mail server on call, only test when is not installed + if not self.env["ir.module.module"].search( + [("name", "=", "mail_outbound_static"), ("state", "=", "installed")] + ): + self.assertEqual(mail_server_id, None) + + def test_message_thread_send(self): + mail_message_1 = self.partner.with_user(self.user1).message_post( + body=_("Test"), + subtype_xmlid="mail.mt_comment", + partner_ids=self.partner.ids, + ) + self.assertEqual(mail_message_1.mail_server_id.id, self.server_1.id) + mail_message_2 = self.partner.with_user(self.user2).message_post( + body=_("Test"), + subtype_xmlid="mail.mt_comment", + partner_ids=self.partner.ids, + ) + self.assertEqual(mail_message_2.mail_server_id.id, self.server_2.id) + mail_message_3 = self.partner.with_user(self.user3).message_post( + body=_("Test"), + subtype_xmlid="mail.mt_comment", + partner_ids=self.partner.ids, + ) + self.assertFalse(mail_message_3.mail_server_id.id) + + def _create_mail(self, from_user): + MailMessage = self.env["mail.mail"] + email_values = { + "email_from": from_user.email, + "subject": "Hello", + "email_to": "contact@example.com", + "reply_to": "contact@example.com", + } + return MailMessage.create(email_values) + + def test_mail_mail_send(self): + mail1 = self._create_mail(self.user1) + mail2 = self._create_mail(self.user2) + mail3 = self._create_mail(self.user3) + (mail1 + mail2 + mail3).send() + self.assertEqual(mail1.mail_server_id.id, self.server_1.id) + self.assertEqual(mail2.mail_server_id.id, self.server_2.id) + self.assertFalse(mail3.mail_server_id.id) diff --git a/setup/mail_server_by_user/odoo/addons/mail_server_by_user b/setup/mail_server_by_user/odoo/addons/mail_server_by_user new file mode 120000 index 0000000000..e849918a0d --- /dev/null +++ b/setup/mail_server_by_user/odoo/addons/mail_server_by_user @@ -0,0 +1 @@ +../../../../mail_server_by_user \ No newline at end of file diff --git a/setup/mail_server_by_user/setup.py b/setup/mail_server_by_user/setup.py new file mode 100644 index 0000000000..28c57bb640 --- /dev/null +++ b/setup/mail_server_by_user/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)