Search code examples
pythonpython-2.7odoo-8odoo

How to send a mail with a template through code in Odoo v8?


I'm trying to send a mail (with a template loaded) through Python.

I did this in version 7 with the function send_mail of the email.template model. Now, I want it for version 8, but I'm not able to manage this.

It's not a problem about thse SMTP server nor the template, because if I send the mail manually, it gets to its destiny rightly.

It seems that it's not going into the function send_mail (I've written a logger info message above the first line of the function and it never appears).

This is my code:

return self.env['email.template'].send_mail(
   self.env.cr, self.env.uid, template.id, self.id, force_send=True,
   context=self.env.context)

I've also checked that the template.id and the self.id parameters which the function needs are right. But there are no errors, no messages, it's ignoring the function. I've also tried without cr, uid and context, but same result.

By the way, send_mail function has a decorator which I had never seen before, @api.cr_uid_id_context, whose meaning I don't know.

Can anyone help me, please?


Solution

  • I've found the problem after a while. I compared my code to other modules which called the same function and I tried to emulate them.

    Finally I manage it calling the email.template model with pool instead of env, this way:

    return self.pool['email.template'].send_mail(
       self.env.cr, self.env.uid, template.id, self.id, force_send=True,
       context=self.env.context)
    

    Now it goes into the the function and it works, mail is sent with the desired template.