From 66115454be875d1e1fab709f35801862c6bbb81a Mon Sep 17 00:00:00 2001 From: David Beal Date: Thu, 7 Sep 2017 13:16:21 +0200 Subject: [PATCH 1/2] ADD module mail_inline_css --- mail_inline_css/README.rst | 101 +++++++++++++++++++++++++++++ mail_inline_css/__init__.py | 1 + mail_inline_css/__manifest__.py | 18 +++++ mail_inline_css/models/__init__.py | 1 + mail_inline_css/models/mail.py | 24 +++++++ requirements.txt | 1 + 6 files changed, 146 insertions(+) create mode 100644 mail_inline_css/README.rst create mode 100644 mail_inline_css/__init__.py create mode 100644 mail_inline_css/__manifest__.py create mode 100644 mail_inline_css/models/__init__.py create mode 100644 mail_inline_css/models/mail.py create mode 100644 requirements.txt diff --git a/mail_inline_css/README.rst b/mail_inline_css/README.rst new file mode 100644 index 00000000..b76d1dfe --- /dev/null +++ b/mail_inline_css/README.rst @@ -0,0 +1,101 @@ +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :target: http://www.gnu.org/licenses/agpl + :alt: License: AGPL-3 + +=============== +Mail Inline CSS +=============== + +When you send HTML emails you can't use style tags but instead you have +to put inline ``style`` attributes on every element. So from this: + +.. code:: html + + + +

Peter

+

Hej

+ + +You want this: + +.. code:: html + + +

Peter

+

Hej

+ + +This module use premailer library to do this. + +It parses an HTML page, looks up ``style`` blocks +and parses the CSS. It then uses the ``lxml.html`` parser to modify the +DOM tree of the page accordingly. + +Installation +============ + +To install this module, you need first to install `premailer` python library with: + +.. code:: bash + + pip install premailer + + +Usage +===== + +Just use any mail template as Odoo standard feature + +.. 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/10 + + +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 smash it by providing detailed and welcomed feedback. + +Credits +======= + +Images +------ + +* Odoo Community Association: `Icon `_. + +Contributors +------------ + +* David BEAL + +Do not contact contributors directly about support or help with technical issues. + +Funders +------- + +The development of this module has been financially supported by: + +* Akretion + +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/mail_inline_css/__init__.py b/mail_inline_css/__init__.py new file mode 100644 index 00000000..0650744f --- /dev/null +++ b/mail_inline_css/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/mail_inline_css/__manifest__.py b/mail_inline_css/__manifest__.py new file mode 100644 index 00000000..a0c9d942 --- /dev/null +++ b/mail_inline_css/__manifest__.py @@ -0,0 +1,18 @@ +# coding: utf-8 +# © 2017 David BEAL @ Akretion +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +{ + 'name': 'Mail Inline CSS', + "summary": "Convert style tags in inline style in your mails", + 'version': '10.0.1.0.0', + 'author': 'Akretion, Odoo Community Association (OCA)', + 'website': 'https://github.com/OCA/social', + 'license': 'AGPL-3', + 'category': 'Tools', + 'depends': ['mail'], + 'installable': True, + "external_dependencies": { + "python": ['premailer'], + }, +} diff --git a/mail_inline_css/models/__init__.py b/mail_inline_css/models/__init__.py new file mode 100644 index 00000000..37a9875b --- /dev/null +++ b/mail_inline_css/models/__init__.py @@ -0,0 +1 @@ +from . import mail diff --git a/mail_inline_css/models/mail.py b/mail_inline_css/models/mail.py new file mode 100644 index 00000000..58135b2b --- /dev/null +++ b/mail_inline_css/models/mail.py @@ -0,0 +1,24 @@ +# coding: utf-8 +# © 2017 David BEAL @ Akretion +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +import logging +from odoo import models + +_logger = logging.getLogger(__name__) + +try: + from premailer import transform +except (ImportError, IOError) as err: + _logger.debug(err) + + +class MailTemplate(models.Model): + _inherit = 'mail.template' + + def generate_email(self, res_ids, fields=None): + res = super(MailTemplate, self).generate_email(res_ids, fields=fields) + for id in res: + if res[id].get('body_html'): + res[id]['body_html'] = transform(res[id]['body_html']) + return res diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..8c94d1e2 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +premailer \ No newline at end of file From f29a5429de04bc4976fee4352abacbd47996801d Mon Sep 17 00:00:00 2001 From: David Beal Date: Tue, 2 Jan 2018 15:59:43 +0100 Subject: [PATCH 2/2] IMP README use case --- mail_inline_css/README.rst | 8 ++++++++ mail_inline_css/models/mail.py | 5 ++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/mail_inline_css/README.rst b/mail_inline_css/README.rst index b76d1dfe..b67fc9c2 100644 --- a/mail_inline_css/README.rst +++ b/mail_inline_css/README.rst @@ -55,6 +55,14 @@ Just use any mail template as Odoo standard feature :target: https://runbot.odoo-community.org/runbot/205/10 +Note: + + Odoo with module web_editor already implements this feature on the client side (js). + This module brings this server side feature for cases without js part. It could the more stable way over the Odoo versions with a stable api in a dedicated library + with adhoc python unit tests. + + + Bug Tracker =========== diff --git a/mail_inline_css/models/mail.py b/mail_inline_css/models/mail.py index 58135b2b..a4be0194 100644 --- a/mail_inline_css/models/mail.py +++ b/mail_inline_css/models/mail.py @@ -18,7 +18,6 @@ class MailTemplate(models.Model): def generate_email(self, res_ids, fields=None): res = super(MailTemplate, self).generate_email(res_ids, fields=fields) - for id in res: - if res[id].get('body_html'): - res[id]['body_html'] = transform(res[id]['body_html']) + if 'body_html' in res: + res['body_html'] = transform(res['body_html']) return res