Creating email templates in Odoo using XML code is a powerful way to customize email communications directly from your Odoo modules. Email templates in Odoo allow you to automate and standardize email communications for various processes, such as sending invoices, notifications, or alerts.
Here’s a step-by-step guide on how to create email templates using XML code in Odoo:
Create the XML File for Email Templates
Create XML File: Inside your module, create a directory named data
(if it doesn’t already exist) and add a file named email_template_data.xml
<?xml version="1.0" encoding="UTF-8"?>
<odoo> <!-- Define the email template -->
<record id="my_email_template" model="mail.template">
<field name="name">My Custom Email Template</field>
<field name="email_from">${object.user_id.email | default('noreply@example.com')}</field>
<field name="subject">Reminder: ${object.name}</field>
<field name="model_id" ref="model_my_model"/>
<field name="body_html">
<![CDATA[ <p>Hello ${object.partner_id.name},</p>
<p>This is a reminder regarding ${object.name}.</p>
<p>Best Regards,</p> <p>Your Company</p> ]]> </field>
<field name="report_template" eval="False"/>
<field name="report_name" eval="False"/>
<field name="email_to" eval="False"/>
<field name="email_cc" eval="False"/>
<field name="email_bcc" eval="False"/> </record>
</odoo>
3. Define the Model
Ensure that the model referenced in the model_id
field exists. Here’s an example of a simple model definition that the email template might interact with:
pythonCopy codefrom odoo import models, fields
class MyModel(models.Model):
_name = 'my.model'
_description = 'My Model'
name = fields.Char('Name')
user_id = fields.Many2one('res.users', 'Assigned User')
partner_id = fields.Many2one('res.partner', 'Partner')
4. Update the Module
After defining your email template in XML and ensuring your model is set up correctly, update your module to apply the changes.
- Restart Odoo: Restart the Odoo server to load the new module changes.bashCopy code
sudo service odoo restart
- Update the Module: Go to the Apps menu, search for your custom module, and click Upgrade.
5. Use the Email Template
You can now use your email template in various ways:
- Automated Actions: Set up automated actions (scheduled actions) to use your email template for automated notifications.
- Manually Sending Emails: Go to the relevant model records (e.g.,
my.model
), and use the “Send Email” action, selecting your template from the list.
Example: Using the Email Template
To send an email using the template programmatically, you can use the following code:
pythonCopy codefrom odoo import api, models
class MyModel(models.Model):
_inherit = 'my.model'
@api.model
def send_email_reminder(self):
template = self.env.ref('my_email_templates_module.my_email_template')
for record in self:
template.send_mail(record.id, force_send=True)
Conclusion
Creating email templates using XML code in Odoo allows for precise control over your email communications. By following the steps outlined above, you can automate and customize email content to fit your business needs, enhancing both efficiency and consistency in your communications.
Feel free to expand on this setup by adding more complex templates or integrating with other Odoo features. Happy coding!