
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"> |
Python Code:
from odoo import modelsclass 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: template_id.send_mail(record.id, force_send=True) |