Home > OS >  Odoo14: Send an email through code using a recordset in the template
Odoo14: Send an email through code using a recordset in the template

Time:03-24

Using a scheduled task, I need to send an email which will inform on the status of open tasks. After getting the recordset correponding to my search, I found how to load the template and send the email:

mail_template = self.env.ref('test_email.email_template')
mail_template.send_mail(self.id)

But I only got access to the current record in $object. I would like to be able to pass the recordset to the template and loop over it.

CodePudding user response:

You can use the context to "transport" data into the mail rendering:

mail_template.with_context(my_recordset=my_recordset).send_mail(self.id)

In your mail template just use ctx:

%for record in ctx.get('my_recordset', []):
<div>${record.my_attribute}</div>
%endfor
  • Related