Finding and sending e-mail templates in Odoo

In this tutorial I will teach you how to send e-mail templates in Odoo from the Python code. You’ll learn how to find e-mail templates, how to call them and how to send them to the user. In this tutorial I will continue with the e-mail template that I’ve made in my other tutorial, which you can find here. We’ll create a ‘send e-mail’ button on the contact form and after clicking on this button we will send out the e-mail template.
The result after this tutorial:
Example result e-mail templates

If you just want to follow this tutorial please download and install the sample module from my previous tutorial. You can find the module here.
If you’d like to send out your own e-mail template you can continue reading without using the sample module.

Updated tutorial available on Oocademy

1. Create the send e-mail button

First of all we have to add a button to the existing view. Create a new XML file and inherit the existing contact form:

Now that we’ve inherited the view we should create an xpath in the view in order to add in the button on the form. Write an xpath expression that inserts content before the ‘sheet’ of the form, wrap it in header tags and add a button:

So, what does this code do? It will inherit the existing contact form and will add a button at the top of the form. If you’d install this module right now you would get this as a result:
Contact form example

Now have a closer look at the XML code for the button:

This button will be named ‘send_mail_template’, has the text ‘Send e-mail’ on it and is of the type object. The type ‘object’ tells Odoo that we want to trigger Python code after clicking on the button.

2. Catch the button click

Now that you’ve created the view (XML) side it is time to code the Python side. First of all create a new Python file, create a class in it and inherit the ‘res.partner’ model. Why? Because we will need to execute Python code on the ‘res.partner’ model. Without this inheritance Odoo will not know where to execute our code. Your code should look like this:

Now that you’ve inherited the model ‘res.partner’ we have to add a function that goes off when the user clicks on the button ‘send e-mail’.
The name of your function should be the same as the name of your button because Odoo uses this to execute the Python code. Go ahead and create a function named ‘send_mail_template’:

3. Finding and sending e-mail templates

The final step is to find the e-mail template and send it to the end user.
To find the template you should use self.env.ref to search the template in the database. In this tutorial I’m looking for the e-mail template with the name ‘example_email_template’, which is in the module ‘mail_template_demo’:

It is important to first name the module where the template is in followed by a dot (.) and then the name of the e-mail template. Otherwise Odoo will not find your template.
As a result we will get the variable which contains the link to our e-mail template. Because we only have a ‘pointer’ to this e-mail template we should still find the record itself in the database and trigger the function ‘send_mail’ to push the e-mail out. Your code should look like this:

The self.env will make sure we’re searching on the model ‘mail.template’ (where all e-mail templates are stored) and with browse it can find the correct template. At this point we have the e-mail template but we still need to render it and ‘push’ it to the user. This is exactly what the function ‘send_mail’ does. Since the function ‘send_mail’ needs to know which record it should get data from (to parse it in the e-mail template with jinja2) we will pass along the ID of the current record with
That’s it! The result of the Python function looks like this:

3.1 The result

You’re all done now! Save all the files, update your modules list and install the module. When you go to the contact form and click on the button ‘Send e-mail’ you’ll see an e-mail in your inbox within a few minutes:
Send e-mail result

4. Conclusion

Finding and sending out e-mail templates in Odoo is actually really easy. It only requires a few lines of Python code and all the rest is handled by Odoo itself.

Do you want to try the demo code and see the source code of this tutorial? You can view it on my Github account.
Has this tutorial helped you, do you have any feedback or questions? Post away!
Tutorial sponsored by Oocademy


Leave a Reply