Thursday, 1 June 2017

Odoo: Set/use context in email templates



Today we are going to discuss, how to use/change the context in dynamic/custom templates.

Let's say we want to send some values to an email template other than normal object id, here is the code for that

In controllers/main.py

local_context = request.env.context.copy()

In module.py file

local_context = self.env.context.copy()
local_context.update({
    'name': 'Shiv',
    'place': 'Bangalore'
})

template = request.env.ref('module_name.email_template_id')
            template.with_context(local_context).send_mail(object.id, force_send=True, raise_exception=True)

In email template code you can access context as:

<p>${ctx['name']}</p>
<p>${ctx['place']}</p>

That's it.

To know how to create a custom email template, please check here: http://www.odoo.yenthevg.com/creating-mail-templates/


Thanks!!! Enjoy Programming!!! :)

7 comments:

  1. Thank you. This tip helped me today after I'd spent about 2 days trying to send an email.

    ReplyDelete
    Replies
    1. Happy to hear that, at-least this post helped someone :)

      Delete
  2. great article :) you save my day today.... thumbs up

    ReplyDelete
  3. Can we send multiple email id in email_to field like this method.

    ReplyDelete

Thanks for your valuable comments.

Nginx: Cannot allocate memory