From c6e365b38b3c758c43072a09ff54961f2d5e2181 Mon Sep 17 00:00:00 2001 From: Thomas Rehn Date: Fri, 12 May 2017 05:42:35 +0200 Subject: [PATCH] [ADD] new module base_mail_bcc (#56) * [ADD] new module base_mail_bcc * [IMP] correct typos in README, change name of configuration parameter * [REF] cleaned up code * [IMP] base_mail_bcc: replicate exact API of super method * [IMP] base_mail_bcc: add test code * [REF] base_mail_bcc: code clean-up * [IMP] base_mail_bcc: improve test coverage by triggering BCC-append case --- base_mail_bcc/README.rst | 58 +++++++++++++++++++++++ base_mail_bcc/__init__.py | 5 ++ base_mail_bcc/__openerp__.py | 18 +++++++ base_mail_bcc/demo/mail_bcc_demo.xml | 9 ++++ base_mail_bcc/models/__init__.py | 5 ++ base_mail_bcc/models/ir_mail_server.py | 41 ++++++++++++++++ base_mail_bcc/tests/__init__.py | 4 ++ base_mail_bcc/tests/test_base_mail_bcc.py | 20 ++++++++ 8 files changed, 160 insertions(+) create mode 100644 base_mail_bcc/README.rst create mode 100644 base_mail_bcc/__init__.py create mode 100644 base_mail_bcc/__openerp__.py create mode 100644 base_mail_bcc/demo/mail_bcc_demo.xml create mode 100644 base_mail_bcc/models/__init__.py create mode 100644 base_mail_bcc/models/ir_mail_server.py create mode 100644 base_mail_bcc/tests/__init__.py create mode 100644 base_mail_bcc/tests/test_base_mail_bcc.py diff --git a/base_mail_bcc/README.rst b/base_mail_bcc/README.rst new file mode 100644 index 00000000..b413f2fe --- /dev/null +++ b/base_mail_bcc/README.rst @@ -0,0 +1,58 @@ +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 + +============== +BCC all emails +============== + +This module extends the email mechanism to allow for sending a blind carbon copy (BCC) +of all outgoing emails to configurable e-mail addresses. + +Configuration +============= + +To configure this module, you need to: + +* Go to Settings > Parameters > System Parameters +* Create a new entry with key `base_mail_bcc.bcc_to` and set the desired e-mail addresses for BCC as value. This value must be a comma-separated list of valid e-mail addresses. + +.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas + :alt: Try me on Runbot + :target: https://runbot.odoo-community.org/runbot/205/8.0 + +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 smashing it by providing a detailed and welcomed feedback. + +Credits +======= + +Images +------ + +* Odoo Community Association: `Icon `_. + +Contributors +------------ + +* Thomas Rehn + +Maintainer +---------- + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +This module is maintained by the OCA. + +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. + +To contribute to this module, please visit https://odoo-community.org. diff --git a/base_mail_bcc/__init__.py b/base_mail_bcc/__init__.py new file mode 100644 index 00000000..251c2d64 --- /dev/null +++ b/base_mail_bcc/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# © 2014-2016 Thomas Rehn (initOS GmbH) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import models diff --git a/base_mail_bcc/__openerp__.py b/base_mail_bcc/__openerp__.py new file mode 100644 index 00000000..3bd713ec --- /dev/null +++ b/base_mail_bcc/__openerp__.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +# © 2014-2016 Thomas Rehn (initOS GmbH) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +{ + "name": "BCC all emails", + "version": "8.0.1.0.0", + "depends": ["base"], + 'author': 'initOS GmbH, Odoo Community Association (OCA)', + "category": "Tools", + 'license': 'AGPL-3', + 'data': [ + ], + 'demo': ['demo/mail_bcc_demo.xml'], + 'test': [ + ], + 'installable': True, + 'auto_install': False, +} diff --git a/base_mail_bcc/demo/mail_bcc_demo.xml b/base_mail_bcc/demo/mail_bcc_demo.xml new file mode 100644 index 00000000..509d97d6 --- /dev/null +++ b/base_mail_bcc/demo/mail_bcc_demo.xml @@ -0,0 +1,9 @@ + + + + + base_mail_bcc.bcc_to + root@example.com + + + diff --git a/base_mail_bcc/models/__init__.py b/base_mail_bcc/models/__init__.py new file mode 100644 index 00000000..721d9b73 --- /dev/null +++ b/base_mail_bcc/models/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# © 2014-2016 Thomas Rehn (initOS GmbH) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import ir_mail_server diff --git a/base_mail_bcc/models/ir_mail_server.py b/base_mail_bcc/models/ir_mail_server.py new file mode 100644 index 00000000..7158d60c --- /dev/null +++ b/base_mail_bcc/models/ir_mail_server.py @@ -0,0 +1,41 @@ +# -*- coding: utf-8 -*- +# © 2014-2017 Thomas Rehn (initOS GmbH) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from email.Utils import COMMASPACE + +from openerp import models, api + + +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_debug=False): + """"Add global bcc email addresses""" + + # These are added here in send_email instead of build_email + # because build_email is independent from the database and does not + # have a cursor as parameter. + + ir_config_parameter = self.env["ir.config_parameter"] + config_email_bcc = ir_config_parameter.\ + get_param("base_mail_bcc.bcc_to") + + if config_email_bcc: + config_email_bcc = config_email_bcc.encode('ascii') + existing_bcc = [] + if message['Bcc']: + existing_bcc.append(message['Bcc']) + del message['Bcc'] + message['Bcc'] = COMMASPACE.join( + existing_bcc + config_email_bcc.split(',') + ) + + return super(IrMailServer, self).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_debug=smtp_debug + ) diff --git a/base_mail_bcc/tests/__init__.py b/base_mail_bcc/tests/__init__.py new file mode 100644 index 00000000..aad01d1b --- /dev/null +++ b/base_mail_bcc/tests/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# © 2017 initOS GmbH +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from . import test_base_mail_bcc diff --git a/base_mail_bcc/tests/test_base_mail_bcc.py b/base_mail_bcc/tests/test_base_mail_bcc.py new file mode 100644 index 00000000..81dea2af --- /dev/null +++ b/base_mail_bcc/tests/test_base_mail_bcc.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# © 2017 initOS GmbH +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +import mock + +from openerp.tests.common import TransactionCase + + +class TestBaseMailBcc(TransactionCase): + def test_base_mail_bcc(self): + ir_mail_server = self.env['ir.mail_server'] + message = ir_mail_server.build_email( + email_from='admin@example.com', + email_to='admin@example.com', + email_bcc='unused@example.com', + subject='An example E-Mail', + body='With an example body', + ) + with mock.patch("smtplib.SMTP"): + ir_mail_server.send_email(message)