Odoo 16 Development Book

Send Emails Using Qweb Template

Email can be the most powerful, effective, and cost-effective strategic marketing channel available to businesses. The advantage is that emails are sent and received instantly, regardless of whether the recipient is next to you or thousands of miles away. We all know how important it is to craft perfect emails for maximum reliability, but who doesn't want to rehash the same structure over and over again? In such cases, email templates can help. It saves time by allowing you to focus on your content instead of having to rebuild your entire email structure each time.

QWeb is a multilingual web framework and primarily an XML template engine. Due to QWeb advantage, Odoo uses his QWeb for email templates. Email templates can be customized quickly and easily in Odoo. We also offer a wide range of customizable business operations solutions.

Here's how to create an email template:

<?xml version="1.0" encoding="utf-8"?>
	<data noupdate="1">
    	<record id="mail_template_presence" model="mail.template">
        	<field name="name">HR: Employee Absence email</field>
        	<field name="model_id" ref="hr.model_hr_employee"/>
        	<field name="subject">Unexpected Absence</field>
        	<!--	Sender email    	-->
        	<field name="email_from">{{ user.email_formatted }}</field>
        	<!--   Recipient email     	-->
        	<field name="email_to">{{ (object.work_email) }}</field>
        	<field name="auto_delete" eval="False"/>
        	<field name="body_html" type="html">
                	Dear <t t-out="object.name or ''">Abigail Peterson
                	Exception made if there was a mistake of ours,
                	it seems that you are not at your office and
                	there is not request of time off from you.<br/>
                	Please, take appropriate measures in order to
                	carry out this work absence.<br/>
                	<br/>Best Regards,<br/><br/>

This is a basic template used. Now let’s check the details regarding the templates;

id: Unique ID of the record. You can see this when you go to Settings > Technical > Email > Templates and check all templates.

model: A model associated with a mail template, ie mail.template.

name:Helps to identify the created template quickly.

model_id: It is the reference to the required model.

email_from: It is the email from which the template was sent.

email_to: Email for receiving emails.

Subject: It is the subject of the email template.

body_html: The design and content of the email is contained in the body HTML.

After creating the template, it will appear in the menu called “Email Templates” from the settings. From there, we can see the templates and their preview.

If you want to send an email using this template from any custom function, then the python code can be used to send it. Let’s see how its works;

def custom_function(self):
    template = self.env.ref(module_name.mail_template').id
    template_id = self.env['mail.template'].browse(template)
    template_id.send_mail(self.id, force_send=True)


Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635



Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.



Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message