How to Create Email Template in Odoo 18

April 10, 2025 by
admin


                                                                                                                 


cover page
How to Create Email Template in Odoo 18
Email templates are predefined messages saved in the system for efficient reuse. They allow users to send consistent, well-crafted communications without the need to draft content repeatedly. By tailoring templates to suit specific situations, users can deliver precise messages to their target audience, ensuring effective communication and fostering stronger customer relationships.

To create an email template, first, confirm that your custom module includes the necessary dependencies. Edit the __manifest__.py file and add 'mail' and 'contacts' to the 'depends' list. This step ensures the module can utilize key features from the mail module for effective integration.



Email Template Creation
In Odoo, email templates are built using XML, Jinja2, and Python. Start by adding a new record in your XML file, where you define the core attributes of the email template. This setup enables you to design flexible and reusable templates for different scenarios.

Xml Code:


<record id="email_template_edi_sale" model="mail.template">
           <field name="name">Sales: Send Quotation</field>
           <field name="model_id" ref="sale.model_sale_order"/>
           <field name="subject">{{ object.company_id.name }} {{ object.state in ('draft', 'sent') and    (ctx.get('proforma') and 'Proforma' or 'Quotation') or 'Order' }} (Ref {{ object.name or 'n/a' }})</field>
           <field name="email_from">{{ (object.user_id.email_formatted or object.company_id.email_formatted or user.email_formatted) }}</field>
           <field name="partner_to">{{ object.partner_id.id }}</field>
           <field name="description">Used by salespeople when they send quotations or proforma to prospects</field>
           <field name="body_html" type="html">
    <div style="margin: 0px; padding: 0px;">
   <p style="margin: 0px; padding: 0px; font-size: 13px;">
           <t t-set="doc_name" t-value="'quotation' if object.state in ('draft', 'sent') else 'order'"/>
         Hello, <br/><br/>
       Your 
    <t t-if="ctx.get('proforma')">
Pro forma invoice for <t t-out="doc_name or ''">quotation</t> <span style="font-weight: bold;"  t-out="object.name or ''">S00052</span>
            <t t-if="object.origin">(with reference: <t t-out="object.origin or ''"></t> )
               </t>
amounting in <span style="font-weight: bold;" t-out="format_amount(object.amount_total, object.currency_id) or ''">$ 10.00</span> is available.
            </t>
            <t t-else="">
<t t-out="doc_name or ''">quotation</t> <span style="font-weight: bold;" t-out="object.name or ''"></span>
            <t t-if="object.origin">
      (with reference: <t t-out="object.origin or ''">S00052</t> )
            </t>
amounting in <span style="font-weight: bold;" t-out="format_amount(object.amount_total, object.currency_id) or ''">$ 10.00</span> is ready for review.
        </t><br/><br/>
  </t>
          Do not hesitate to contact us if you have any questions.
            <t t-if="not is_html_empty(object.user_id.signature)"><br/><br/>
            <t t-out="object.user_id.signature or ''">--<br/>Mitchell Admin</t>
          </t> <br/><br/>
   </p>
</div>
       </field>
       <field name="report_template_ids" eval="[(4, ref('sale.action_report_saleorder'))]"/>
       <field name="lang">{{ object.partner_id.lang }}</field>
        <field name="auto_delete" eval="True"/>
 </record>


Understanding the Key Fields in Email Templates
* id: A unique external identifier for the email template record.
* model: Specifies the model to which the email template is linked, ensuring consistent usage across templates.
* name: A descriptive name for the email template, making it easily identifiable.
* ref: References data from the related model required for sending the email.
* email_from: Defines the sender's email address.
* email_to: Specifies the recipient's email address.
* subject: Sets the subject line for the email.
* body_html: The main content of the email, crafted using Jinja2 for dynamic rendering.
Once the XML file for the email template is created and the custom module is installed, activate developer mode to unlock advanced options. Go to Technical > Email Templates (as illustrated below) and locate the customized template.



The layout will appear as shown, allowing you to customize different sections of the email template to suit your needs. You can also configure report attachments to include relevant documents, such as sale orders, invoices, or quotations, directly in the email. Furthermore, auto-delete options can be enabled to automatically remove outdated or unnecessary attachments after they have been sent, helping to optimize storage and maintain email efficiency.



Python Function for Sending Emails
To utilize the email template, define a Python function that triggers it. Begin by adding a button to the desired model and linking it to an action. Use the following approach to invoke the email template:

<record id="button_send_email" model="ir.ui.view">
<field name="name">sale.order.form</field>
<field name="model">sale.order</field>
<field name="arch" type="xml">
<form string="Your Model">
<header> 
<button name="%(action_send_email)d" string="Send Email" type="action" class="btn-primary"/> 
</header> 
</form> 
</field>
 </record>


Python Code:

from odoo import models 
class YourModel(models.Model): 
_inherit  = 'sale.order' 

def send_email(self): 
template_id = self.env.ref(.email_template_id')
 if template_id: 
for record in self: t
emplate_id.send_mail(record.id, force_send=True)

After installing the changes, a button will appear on the Sale Order form view. When clicked, it will send an email to the corresponding customer with the relevant sale order details as per the customization. The following image shows the button (Send Mail) on the Sale Order view.



The image below illustrates the sale order log note, confirming that the email has been successfully sent to the customer. The email body includes the sale order details, customized according to your requirements. The corresponding customer will receive this email.



Conclusion
Odoo 18 simplifies the creation of email templates, automating the process of sending dynamic and personalized messages. Using a combination of XML, Jinja2, and Python, users can design templates tailored to their specific business needs. These templates can be easily linked to buttons or actions, streamlining the email-sending process and making it quick and efficient. Additionally, features like attaching reports and enabling the auto-deletion of outdated files enhance the functionality of email templates while optimizing storage usage. By ensuring consistency, saving time, and improving productivity, email templates in Odoo 18 empower businesses to manage communications effortlessly and professionally.


If you are looking for an ERP implementation partner with diverse industry experience feel free to contact us. Zesty Beanz Technologies is headquartered in Trivandrum Kerala,  We have proven track record of successful implementations across the world in various sectors including Odoo for Manufacturing, Odoo for Trading, Odoo for FMCG, Odoo for Oil & Gas, Odoo for Diary, Odoo for Pharma, Odoo for Cosmetic Clinic, Odoo for Contracting Companies, Odoo for HVAC, Odoo for Logistics, Odoo for Automobile, Odoo for Laundry, Odoo for Field Service, Odoo for E-Commerce & many more

ZestyBeanz offers Developer / Consultant outsourcing programs, Chat with us in Whatsapp and Hire Odoo Developers, Mobile Application Developers, Consultants.
#OdooKerala #OdooKochi #OdooTrivandrum #OdooERP #ProjectManagement #OdooVansales #HireOdooDeveloper