Chapter 16 - Odoo 15 Development Book

Send Emails Using Qweb Template

An email has the potential to be the most powerful, effective, and cost-efficient strategic marketing channel available to businesses. The advantage is that emails are sent and received instantly, whether the recipient is next to you or thousands of miles away. We all know how important it is to create perfect emails in order to gain the best reliability, but nobody doesn't want to rehash the same structure repeatedly. Email templates come in handy in this situation. You save time by not having to redo the entire email structure each time, allowing you to focus on the content.

QWeb is a multi-language web framework, and it’s mostly an XML templating engine. Because of the benefits of QWeb, Odoo uses it for Email Templates. Email Templates may be quickly and simply customized in Odoo. It also offers a wide range of adaptable solutions for business operations.

Let’s see how to create the email templates here;

<odoo>
  <record id="record_id" model="mail.template">
      <field name="name">Email Template Name</field>
      <field name="model_id" ref="module_name.model_name"/>
      <!--       Sender email     -->
      <field name="email_from">{{object.res_user_id.email}}</field>
      <!--       Recipient email     -->
      <field name="email_to">{{object.client_id.email}}}</field>
      <field name="subject">{{object.subject}}</field>
      <field name="body_html">
            <p>
     Hi<t t-out="object.client_name.name"/>,
     <br/>
     <br/>
     This is our first e-mail template!
     <br/>
       </p>
       Regards,
       <br/>
       <t t-out="object.company_id.name"/>
      </field>
  </record>
</odoo>

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

id: Used to uniquely identify the record. This is also visible if you go to Settings > Technical > Email > Templates and look at all of the templates.

model: Model referred to the mail template ie, mail.template.

name: It will help to quickly identify the template created.

model_id: Reference to the required model.

email_from: Email used to send the template from.

email_to: Email used to receive the mail.

subject: Subject of the email template.

body_html: The design and content of the e-mail are contained within 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)

After executing this function an email will be sent. Here in this function, the created email template is placed in a variable using its external id, and this is searched inside the email templates from the settings. After browsing the template, it is sent using the default function send_mail(), which will send an email based on the mentioned template. QWeb is used in the email templates. Because you don't have to alter code, the composer allows you to edit emails in their final presentation, making adjustments more robust.

In Odoo, finding and delivering email templates is actually rather simple. It allows you to make the process more efficient.

whatsapp_icon
location

Calicut

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

location

Kochi

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

location

Bangalore

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

Send Us A Message