Odoo 17 Development Book

Send Emails Using Qweb Template

Email stands out as one of the most powerful, effective, and cost-efficient strategic marketing channels available to businesses. Its unique advantage lies in the ability to send and receive messages instantly, transcending geographical distances. Crafting perfect emails for optimal impact is crucial, but the repetitive nature of the task can be time-consuming. This is where email templates come to the rescue, saving time and allowing focus on content creation rather than reconstructing the entire email structure each time.

Email stands out as one of the most powerful, effective, and cost-efficient strategic marketing channels available to businesses. Its unique advantage lies in the ability to send and receive messages instantly, transcending geographical distances. Crafting perfect emails for optimal impact is crucial, but the repetitive nature of the task can be time-consuming. This is where email templates come to the rescue, saving time and allowing focus on content creation rather than reconstructing the entire email structure each time.

To create an email template, follow these steps:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <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">
                <div>
                    Dear <t t-out="object.name or ''">Abigail Peterson </t>,
                    <br/>
                    <br/>
                    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/>
               </div>
            </field>
        </record>
    </data>
</odoo>

This is a fundamental template in use. Now, let's delve into the details regarding the templates:

  • id: Unique identifier of the record, visible in Settings > Technical > Email > Templates when reviewing all templates.
  • model: Indicates the model associated with a mail template, i.e., mail.template.
  • name: Facilitates quick identification of the created template.
  • model_id: A reference to the required model.
  • email_from: Represents the sender's email address for the template.
  • email_to: Specifies the email address for receiving emails.
  • Subject: Denotes the subject of the email template.
  • body_html: Encompasses the design and content of the email within the HTML body.

Following the creation of the template, it will be visible in the menu labeled "Email Templates" within the settings. From this menu, you can view the templates along with their previews.

If you intend to send an email using this template through any custom function, Python code can be utilized for the task. Let's explore how it works:

def custom_function(self):
    """function to send email"""
    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)
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