Development Book V18: Send Emails Using Qweb Template

Email is widely regarded as one of the most powerful, efficient, and budget-friendly marketing tools available to businesses. Its ability to deliver messages instantly across the globe sets it apart. However, creating high-impact emails repeatedly can become a tedious task. Email templates help streamline this process by saving time and shifting the focus toward content development, rather than recreating the structure each time.

Email remains one of the most powerful, efficient, and cost-effective strategic marketing tools for businesses. Its standout feature is the ability to deliver and receive messages instantly, regardless of location. Email templates offer a practical solution by saving time and enabling marketers to focus on content creation instead of repeatedly building the email layout from scratch.

Here are the steps to create an email template:

                    <?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data noupdate="1">
        <record id="mail_template_employee_welcome" model="mail.template">
            <field name="name">HR: Employee Welcome Email</field>
            <field name="model_id" ref="hr.model_hr_employee"/>
            <field name="subject">Welcome to the Company, <t t-out="object.name"/></field>
            <field name="email_from">{{ user.email_formatted }}</field>
            <field name="email_to">{{ object.work_email or '' }}</field>
            <field name="auto_delete" eval="False"/>
            <field name="body_html" type="html">
                <div>
                    Dear <t t-out="object.name or 'Employee'"/>,
                    <br/><br/>
                    We are excited to welcome you to our company! We look forward to working with you and hope you have a great start.
                    <br/><br/>
                    Please feel free to reach out if you have any questions or need assistance getting settled.
                    <br/><br/>
                    Best regards,
                    <br/>
                    <t t-esc="user.name"/>
                    <br/>
                    Human Resources Team
                </div>
            </field>
        </record>
    </data>
</odoo>
                    

This template serves as a basic example in use. Let's explore the details and components of these templates:

  • id: The unique record identifier, which can be seen in Settings > Technical > Email > Templates when browsing the list of templates.
  • model: Specifies the model linked to the mail template, such as mail.template.
  • name: Used to identify and differentiate the created template.
  • model: Specifies the model that the template is linked to.
  • email_from: Specifies the sender's email address used in the template.
  • email_to: Indicates the recipient's email address for receiving the message.
  • subject: Specifies the title or heading of the email template.
  • body_html: Contains the layout and content of the email formatted in HTML.

Once the template is created, it will appear under the "Email Templates" menu in Settings. From there, you can view and preview all available templates.

If you want to send an email using this template through a custom function, you can achieve it using Python code. Here's how it works:

def send_custom_email(self):
    """Send email using predefined template"""
    template = self.env.ref('module_name.mail_template')
    template.send_mail(self.id, force_send=True)

Integrating email templates with custom Python functions in Odoo allows for efficient and consistent automated messaging. By leveraging the power of the mail.template model, you can send well-structured emails programmatically, ensuring a smooth and professional communication workflow across different modules.

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