Email is the information stored on a computer that is exchanged between two users over telecommunications. In business terms, it acts as a communication channel between businesses and customers.
Odoo has the option to transfer emails, which will be helpful for transferring out information to customers, vendors, and other concerned people.
But you don’t need to write the same structure every time. In that case, we use the possibility of an email template. Thus we can save the time to rewrite the same content for all time.
Here the email template is created by using XML, Jinja2, and Python in Odoo 16.
The first thing that you need to do is add a dependency to other modules that you’ll need. Open up the __manifest.py__ file and find the line ‘depends.’ In this list, you should add all the modules that you need in your custom-developed module.
If you want to create a new email template, you will need to inherit the mail module in this list.
'depends': ['mail','contacts']
After the dependencies, it is time to create the email template.
Odoo email templates are built in XML and send the email template to somebody. Jinja2 will render your email.
<odoo>
<data noupdate="1">
<record id="test_email_template" model="mail.template">
<field name="name">Test Email Template</field>
<field name="model_id" ref="test_app.model_test_model"/>
<field name="subject">Test Email</field>
<field name="email_from">{{ object.company_id.email }}</field>
<field name="email_to">{{ object.partner_id.email_formatted }}</field>
<field name="description">Sent to the customer to test mail</field>
<field name="body_html" type="html">
<p>
Dear<t t-out="object.partner_id.name"/>,
<br/>
<br/>
Good job, this is our first email template!
<br/>
</p>
Regards,
<br/>
<t t-out="object.company_id.name"/>
</field>
</record>
</data>
</odoo>
Now, let us go over it line by the line.
1. id
The generated record's ‘external id’
2. model (mandatory)
The email template model is the same for all because of the default email functions in Odoo.
3. name (mandatory)
This gives your email template a name, which is also shown in all templates under Settings > Technical > Email > Email templates.
4. ref (mandatory)
The values needed from a model that needs to be sent.
5. email_from
This will show the email from which this email is sent. We can use {{}} to get data from a record and to use this for filling in the data automatically with Jinja2.
6. email_to(mandatory)
This will contain the email address to whom the email should be sent.
7. subject
This will be the title of the email that is being sent to the user.
8. body_html
The content of the email is described herewith by using jinja2. All the content that contains HTML or variables should be within CDATA.
Odoo email templates come with jinja2 by default. This means that you can access any value on a record and fill it in on the email automatically.
Now we need to create a Python function as below for calling the email and passing the values to it.
def action_send_email(self):
self.ensure_one()
ir_model_data = self.env['ir.model.data']
try:
template_id = ir_model_data._xmlid_lookup('test_app.test_email_template')[2]
except ValueError:
template_id = False
try:
compose_form_id = ir_model_data._xmlid_lookup('mail.email_compose_message_wizard_form')[2]
except ValueError:
compose_form_id = False
template_id = self.env.ref('test_app.test_email_template')[2]
ctx = {
'default_model': 'test.model',
'default_res_id': self.ids[0],
'default_use_template': bool(template_id),
'default_template_id': template_id,
'default_composition_mode': 'comment',
'mark_so_as_sent': True,
'force_email': True,
}
return {
'type': 'ir.actions.act_window',
'view_type': 'form',
'view_mode': 'form',
'res_model': 'mail.compose.message',
'views': [(compose_form_id, 'form')],
'view_id': compose_form_id,
'target': 'new',
'context': ctx,
}
Finally, we got a wizard like the one below to send email with a predefined email template.
This is how we can create an email template in Odoo 16 platform.