Creating Custom Email Templates in Odoo for Automated Communication

Abid Patel
07-Nov-2024 Updated : 07-Nov-2024

Learn how to create and trigger custom email templates in Odoo. Discover how to add dynamic content to emails, automate processes, and enhance customer communication.

Creating Custom Email Templates in Odoo for Automated Communication

You can create and use email templates in Odoo. This is a task that is not difficult. It can be quite beneficial. Email templates allow for consistent branding across communications. This is a step to secure a unique identity. They also save time. Reducing manual input composing repeated messages is a time-saver. This can boost overall productivity, another advantage.

What is an Email Template?

An email template in Odoo gives a structure. Customization with dynamic content is possible. This structure is for emails. Email templates are useful for sending automatic communication.

Steps to Create an Email Template in Odoo

An example below shows how to create an email template in Odoo. We will add some dynamic fields. Fields such as customer's name and company’s email are included.

Example XML Code for an Email Template

Here's a method to define email template in XML. Each tag signifies an important part of email.

xml

<odoo>
    <data noupdate="1">
        <record id="custom_email_template" model="mail.template">
            <field name="name">Custom Email Template</field>
            <field name="model_id" ref="test_app.model_test_model"/>
            <field name="subject">Test Email</field>
            <field name="email_from"><t t-esc="object.company_id.email"/></field>
            <field name="email_to"><t t-esc="object.partner_id.email_formatted"/></field>
            <field name="description">Email template for testing purposes</field>
            <field name="body_html" type="html">
                <![CDATA[
                <p>Dear <t t-esc="object.partner_id.name"/>,</p>
                <p>Thank you for being with us. This is a test email template to demonstrate email templating in Odoo.</p>
                <p>Best regards,</p>
                <p><t t-esc="object.company_id.name"/></p>
                ]]>
            </field>
        </record>
    </data>
</odoo>

Explanation of XML Code

  • ▹ name: Defines the name of the template. This makes identification easy.
  • ▹ model_id: This sets model linked to template.
  • ▹ subject: This is the subject line of email.
  • ▹ email_from: It specifies the email address of sender. <t t-esc="object.company_id.email"/> is used. The email of the company is filled in dynamically.
  • ▹ email_to: This is the email address of the recipient. It's set to <t t-esc="object.partner_id.email_formatted"/>.
  • ▹ body_html: This carries the HTML body of email. It uses <t t-esc="object.partner_id.name"/> and <t t-esc="object.company_id.name"/>. Personalizing the email is the purpose.

CDATA tag allows inclusion of HTML without XML interpretation.

Python Code to Trigger the Email Template

Email template definition in XML is accomplished. You send it from Python code within a custom model. Below is an example of how to send this template.

python

from odoo import models, fields

class TestModel(models.Model):
    _name = 'test.model'
    _description = 'Test Model for Email Template'

    def action_send_test_email(self):
        # Reference the template by its XML ID
        template = self.env.ref('test_app.custom_email_template')
        if template:
            # Send the email to the current record
            template.send_mail(self.id, force_send=True)

Explanation of Python Code

  • ▹ Template Reference: Method env.ref() is utilized. Purpose is to access email template by its XML ID (test_app.custom_email_template).
  • ▹ Sending Email: Method send_mail is invoked with ID of current record (self.id). The parameter force_send=True is present. This ensures email is dispatched immediately. It is not queued.

When to Use Email Templates in Odoo

Email templates have wide-ranging functionalities. They can be employed for:

  • ▹ 1. Dispatching order confirmations. It can send invoices and payment reminders too.
  • ▹ 2. Informing customers about fresh product introductions.
  • ▹ 3. Transmitting appointment reminders. Or even updates. They're useful for this purpose. To create automated responses based on customer actions is also a possible use. It's a versatile tool.

Conclusion

Email templating system of Odoo is highly adaptable. It can provide automated communication with customers. A guide is available. You can use it. This guide aids in creating email templates in Odoo. Templates with dynamic data fields. These templates can be triggered using Python. Your processes will be streamlined. Customer interactions would turn more professional and consistent.

Make a Comment

Your email address will not be published. Required fields are marked *