Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to send Partner Mass Mail to partner and parters contacts, v8.

By
Aleksander Nøkland
on 11/11/15, 4:31 AM 897 views

I'm trying to send mail to my partners and their contacts, and I am wondering what I should set in the "To Emails"-field on the template.

I've set a filter to only display the partners (without contacts), I select the partners, click "More" and "Partner Mass Mailing", and selects a template.

In the template (Settings > Technical > Email > Templates) i fill in the field To (Emails) as follows:

${LOOP(object.child_ids,'email')},${object.email}

In v7, this works.

In v8, i get the following error:

2015-11-10 19:37:44,182 30493 ERROR demodb openerp.addons.email_template.email_template: Failed to render template <Template memory:7f895252f110> using values {'format_tz': <function <lambda> at 0x7f89382487d0>, 'ctx': {'default_template_id': 1, 'uid': 1, 'mail_server_id': 1, 'mail_notify_user_signature': False, 'default_use_template': True, 'default_composition_mode': 'mass_mail', 'default_partner_to': "${object.id or ''}", 'search_disable_custom_filters': True, 'tpl_partners_only': True, 'active_id': 828, 'lang': 'nb_NO', 'tz': False, 'active_model': 'res.partner', 'params': {'action': 93}, 'mail_auto_delete': False, 'active_ids': [828]}, 'user': res.users(1,), 'object': res.partner(828,)}

Traceback (most recent call last):

File "/opt/odoo/odoo-server/addons/email_template/email_template.py", line 201, in render_template_batch
render_result = template.render(variables)
File "/usr/lib/python2.7/dist-packages/jinja2/environment.py", line 969, in render
return self.environment.handle_exception(exc_info, True)
File "/usr/lib/python2.7/dist-packages/jinja2/environment.py", line 742, in handle_exception
reraise(exc_type, exc_value, tb)
File "<template>", line 1, in top-level template code
File "/usr/lib/python2.7/dist-packages/jinja2/sandbox.py", line 354, in call
if not __self.is_safe_callable(__obj):
File "/usr/lib/python2.7/dist-packages/jinja2/sandbox.py", line 283, in is_safe_callable
return not (getattr(obj, 'unsafe_callable', False) or
UndefinedError: 'LOOP' is undefined

If I try to use:

${object.child_ids.email}

i get the following error:

2015-11-10 20:47:18,506 30493 ERROR demodb openerp.addons.email_template.email_template: Failed to render template <Template memory:7f8952453410> using values {'format_tz': <function <lambda> at 0x7f895204c758>, 'ctx': {'default_template_id': 1, 'uid': 1, 'mail_server_id': 1, 'mail_notify_user_signature': False, 'default_use_template': True, 'default_composition_mode': 'mass_mail', 'default_partner_to': "${object.id or ''}", 'search_disable_custom_filters': True, 'tpl_partners_only': True, 'active_id': 828, 'lang': 'nb_NO', 'tz': False, 'active_model': 'res.partner', 'params': {'action': 93}, 'mail_auto_delete': False, 'active_ids': [828]}, 'user': res.users(1,), 'object': res.partner(828,)}

Traceback (most recent call last):
File "/opt/odoo/odoo-server/addons/email_template/email_template.py", line 201, in render_template_batch
render_result = template.render(variables)
File "/usr/lib/python2.7/dist-packages/jinja2/environment.py", line 969, in render
return self.environment.handle_exception(exc_info, True)
File "/usr/lib/python2.7/dist-packages/jinja2/environment.py", line 742, in handle_exception
reraise(exc_type, exc_value, tb)
File "<template>", line 1, in top-level template code
File "/usr/lib/python2.7/dist-packages/jinja2/sandbox.py", line 330, in getattr
value = getattr(obj, attribute)
File "/opt/odoo/odoo-server/openerp/fields.py", line 807, in __get__
record.ensure_one()
File "/opt/odoo/odoo-server/openerp/models.py", line 5268, in ensure_one
raise except_orm("ValueError", "Expected singleton: %s" % self)
except_orm: ('ValueError', 'Expected singleton: res.partner(829, 830)')


Am i missing some python library? Can I use something else in the To Emails field? Any and all help is appreciated.

The LOOP-part was not in v7, but part of a custom module in v7. I've ported/created a module that inherits email_template. I've ported/created a module that inherits email_template With this we can use: ${LOOP(object.child_ids,'email')},${object.email} _init__.py : import email_template __openerp.py__ : { 'name' : 'Email Templates LOOP', 'version' : '1.0', 'author' : 'Random', 'website' : 'http://www.google.com', 'category' : 'Marketing', 'depends' : ['mail','email_template'], 'description': """ Email Templating - hack for doing loop on partner ============================================================================== Usage: On email template, in field To Email use: ${LOOP(object.child_ids,'email')} """, 'data': [ ], 'demo': [], 'installable': True, 'auto_install': False, } email_template.py : # -*- coding: utf-8 -*- import base64 import datetime import dateutil.relativedelta as relativedelta import logging import lxml import urlparse import openerp from openerp import SUPERUSER_ID from openerp.osv import osv, fields from openerp import tools, api from openerp.tools.translate import _ from urllib import urlencode, quote as quote _logger = logging.getLogger(__name__) try: # We use a jinja2 sandboxed environment to render mako templates. # Note that the rendering does not cover all the mako syntax, in particular # arbitrary Python statements are not accepted, and not all expressions are # allowed: only "public" attributes (not starting with '_') of objects may # be accessed. # This is done on purpose: it prevents incidental or malicious execution of # Python code that may break the security of the server. from jinja2.sandbox import SandboxedEnvironment mako_template_env = SandboxedEnvironment( block_start_string="<%", block_end_string="%>", variable_start_string="${", variable_end_string="}", comment_start_string="<%doc>", comment_end_string="%doc>", line_statement_prefix="%", line_comment_prefix="##", trim_blocks=True, # do not output newline after blocks autoescape=True, # XML/HTML automatic escaping ) mako_template_env.globals.update({ 'str': str, 'quote': quote, 'urlencode': urlencode, 'datetime': datetime, 'len': len, 'abs': abs, 'min': min, 'max': max, 'sum': sum, 'filter': filter, 'reduce': reduce, 'map': map, 'round': round, # dateutil.relativedelta is an old-style class and cannot be directly # instanciated wihtin a jinja2 expression, so a lambda "proxy" is # is needed, apparently. 'relativedelta': lambda *a, **kw : relativedelta.relativedelta(*a, **kw), }) except ImportError: _logger.warning("jinja2 not available, templating features will not work!") class email_template(osv.osv): _inherit = 'email.template' def render_template_batch(self, cr, uid, template, model, res_ids, context=None, post_process=False): """Render the given template text, replace mako expressions ``${expr}`` with the result of evaluating these expressions with an evaluation context containing: * ``user``: browse_record of the current user * ``object``: browse_record of the document record this mail is related to * ``context``: the context passed to the mail composition wizard :param str template: the template text to render :param str model: model name of the document record this mail is related to. :param int res_ids: list of ids of document records those mails are related to. """ if context is None: context = {} res_ids = filter(None, res_ids) # to avoid browsing [None] below results = dict.fromkeys(res_ids, u"") # try to load the template try: template = mako_template_env.from_string(tools.ustr(template)) except Exception: _logger.exception("Failed to load template %r", template) return results # prepare template variables user = self.pool.get('res.users').browse(cr, uid, uid, context=context) def _do_loop(loop, field): return ','.join(map(str,[x[field] for x in loop])) records = self.pool[model].browse(cr, uid, res_ids, context=context) or [None] variables = { 'format_tz': lambda dt, tz=False, format=False, context=context: format_tz(self.pool, cr, uid, dt, tz, format, context), 'user': user, 'ctx': context, # context kw would clash with mako internals 'LOOP': _do_loop, } for record in records: res_id = record.id if record else None variables['object'] = record try: render_result = template.render(variables) except Exception: _logger.exception("Failed to render template %r using values %r" % (template, variables)) render_result = u"" if render_result == u"False": render_result = u"" results[res_id] = render_result if post_process: for res_id, result in results.iteritems(): results[res_id] = self.render_post_process(cr, uid, result, context=context) return results

Aleksander Nøkland
on 11/12/15, 9:04 AM
0
Aleksander Nøkland
On 11/12/15, 7:00 AM

Thank you for the suggestion, but when I try to use this:

${object.mapped('child_ids.email')}

The system creates new partners with name and email like this: myreal@address.com&#39
It does this for all the contacts on the partner I send to.

Then this fails of course with the following error:

 SMTPRecipientsRefused: {'"myreal@address.com&#39"': (501, '5.1.3 Invalid address'), 'myreal@address.com&#39': (501, '5.1.3 Invalid address')}

2015-11-12 09:41:32,884 30493 ERROR demodb openerp.addons.mail.mail_mail: failed sending mail.mail 117


EDIT:

I've solved this, i think.

The LOOP-part was not in v7, but part of a custom module in v7.

I've ported/created a quick and dirty module that inherits email_template

With this we can use: ${LOOP(object.child_ids,'email')},${object.email}

_init__.py :

import email_template

__openerp.py__ :

{
'name' : 'Email Templates LOOP',
'version' : '1.0',
'author' : 'Random',
'website' : 'http://www.google.com',
'category' : 'Marketing',
'depends' : ['mail','email_template'],
'description': """
Email Templating - hack for doing loop on partner
==============================================================================
Usage: On email template, in field To Email use: ${LOOP(object.child_ids,'email')}
""",
'data': [
],
'demo': [],
'installable': True,
'auto_install': False,
}

email_template.py :

# -*- coding: utf-8 -*-
import base64
import datetime
import dateutil.relativedelta as relativedelta
import logging
import lxml
import urlparse
import openerp
from openerp import SUPERUSER_ID
from openerp.osv import osv, fields
from openerp import tools, api
from openerp.tools.translate import _
from urllib import urlencode, quote as quote
_logger = logging.getLogger(__name__)
try:
     from jinja2.sandbox import SandboxedEnvironment
     mako_template_env = SandboxedEnvironment(
         block_start_string="<%",
         block_end_string="%>",
         variable_start_string="${",
         variable_end_string="}",
         comment_start_string="<%doc>",
         comment_end_string="</%doc>",
         line_statement_prefix="%",
         line_comment_prefix="##",
         trim_blocks=True, # do not output newline after blocks
         autoescape=True, # XML/HTML automatic escaping
     )
     mako_template_env.globals.update({
         'str': str,
         'quote': quote,
         'urlencode': urlencode,
         'datetime': datetime,
         'len': len,
         'abs': abs,
         'min': min,
         'max': max,
         'sum': sum,
         'filter': filter,
         'reduce': reduce,
         'map': map,
         'round': round,
        'relativedelta': lambda *a, **kw : relativedelta.relativedelta(*a, **kw),
     })
except ImportError:
     _logger.warning("jinja2 not available, templating features will not work!")
class email_template(osv.osv):
     _inherit = 'email.template'
     def render_template_batch(self, cr, uid, template, model, res_ids, context=None, post_process=False):
    if context is None:
         context = {}
     res_ids = filter(None, res_ids) # to avoid browsing [None] below
     results = dict.fromkeys(res_ids, u"")
    # try to load the template
     try:
        template = mako_template_env.from_string(tools.ustr(template))
 except Exception:
     _logger.exception("Failed to load template %r", template)
     return results
     # prepare template variables
     user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
    def _do_loop(loop, field):
         return ','.join(map(str,[x[field] for x in loop]))
     records = self.pool[model].browse(cr, uid, res_ids, context=context) or [None]
     variables = {
         'format_tz': lambda dt, tz=False, format=False, context=context: format_tz(self.pool, cr, uid, dt, tz, format, context),
         'user': user,
         'ctx': context, # context kw would clash with mako internals
         'LOOP': _do_loop,
    }
     for record in records:
         res_id = record.id if record else None
         variables['object'] = record
         try:
             render_result = template.render(variables)
        except Exception:
            _logger.exception("Failed to render template %r using values %r" % (template, variables))
             render_result = u""
         if render_result == u"False":
             render_result = u""
         results[res_id] = render_result
     if post_process:
         for res_id, result in results.iteritems():
             results[res_id] = self.render_post_process(cr, uid, result, context=context)
     return results

0

Ahmed M.Elmubarak

--Ahmed M.Elmubarak--
2988
| 5 3 5
Sudan
--Ahmed M.Elmubarak--


Ahmed M.Elmubarak
On 11/12/15, 2:42 AM

Hello,

Try this one:

${object.mapped('child_ids.email')}

I hope it'll help,


Regards

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 11/11/15, 4:31 AM
Seen: 897 times
Last updated: 11/12/15, 9:21 AM