1. zikzakmedia
  2. trytond-electronic_mail_template


trytond-electronic_mail_template / electronic_mail.py

# -*- coding: UTF-8 -*-
#This file is part electronic_mail_template module for Tryton.
#The COPYRIGHT file at the top level of this repository contains 
#the full copyright notices and license terms.
from email import message_from_string
from collections import defaultdict

from trytond.model import ModelView, ModelSQL, fields
from trytond.pool import Pool
from trytond.pyson import Eval

__all__ = ['ElectronicMail']

class ElectronicMail(ModelSQL, ModelView):
    "E-Mail module extended to suit inbuilt reading and templating"
    __name__ = 'electronic.mail'

    subject = fields.Char('Subject', translate=True)
    body_html = fields.Function(
        fields.Text('HTML (BODY)'), 'get_email_body')
    body_plain = fields.Function(
        fields.Text('Plain Text (BODY)'), 'get_email_body')

    def __setup__(cls):
        super(ElectronicMail, cls).__setup__()
                'send_mail': {
                    'invisible': Eval('body_plain') == '',

    def get_email_body(self, name):
        """Returns the email body
        result = ''
        test = defaultdict(unicode).fromkeys(name)
        message = message_from_string(self._get_email(self))
        for part in message.walk():
            content_type = part.get_content_type()
            if content_type == 'text/plain':
                result = part.get_payload()
        return result

    def check_xml_record(cls, records, values):
        '''It should be possible to overwrite templates'''
        return True

    def send_mail(self, emails):
        Template = Pool().get('electronic.mail.template')
        for email in emails: