Enable Dark Mode!
creating-email-templates-odoo-12.png
By: Ijaz Ahammed

Creating Email Templates in Odoo 12

Technical Odoo 12

The emails play a vital role in the business as it gives insight into crucial assessments and reflections of the business operations carried every now and then. It paves a platform for transacting necessary communication from one to another.

This blog is a tutorial to teach how one can create email templates using XML, Jinja2, python in odoo 12. 

We know, odoo ERP is a virtual jungle with immense possibilities of customizations. Even the latest version, odoo 12 doesn’t disappoint the users as it comes with diversified options quenching business thirsts. 

Now let move on to understanding how email templates can be created in odoo using the aforesaid languages. 

The first thing that you need to do is add a dependency on other modules that you’ll need the following codes

'depends': ['mail','contacts']

The next step is to create a new XML record like given below-

1. id
The generated record's `external id`

2. model (mandatory)
Every email template model will be the same because the odoo platform has default email functions.

3. name (mandatory)
Only useful as a mnemonic/description of the email when looking for one in a list of some sort.

4. ref(mandatory)
It is used for taking values from a model that you need to send.

5. email_from
It’s useful for identifying who sent the email.

6. email_to(mandatory)
Here we can give the recipient's email address.

7. report_template
If you have to send any attachments like pdf,  here you can give report template id.

8. subject
The subject is a very important thing in email,  so here one can give an email subject such as the one provided below.

9. body_html
Here one can give email contents by using ninja2.

<odoo>
    <data>
         <record id="email_template" model="mail.template">
            <field name="name">EMAIL TEMPLATE</field>
            <field name="model_id"ref="module_name.model_sample.mail">            
            <field name="auto_delete" eval="True"/>
            <field name="email_from">${(object.res_user_id.email}</field>
            <field name="email_to">${object.client_name.email}</field>
             <field name="report_template" ref="action_example_pdf"/>
            <field name="subject">${object.amc}</field>
            <field name="body_html"><![CDATA[
         <p>Dear ${(object.client_name.name)},<br/><br/>
         Good job, you've just created your first e-mail template!<br/></p>
              Regards,<br/>
              ${(object.company_id.name)} ]]></field>
        </record>
    </data>
</odoo>

Odoo Email templates come with Jinja2 by default. This denotes that one can access any value from a record and will be filled into the Email automatically. 

Example:
<field name="subject">${object.amc}</field>

The next step is to create a python following thing is to create a python function like given below. Here you can call email wizards and return values to email.

@api.multi
     def action_send_email(self):
    self.ensure_one()
    ir_model_data = self.env['ir.model.data']
    try:
        template_id = ir_model_data.get_object_reference('module_name',   'template_name')[1]
    except ValueError:
        template_id = False
    try:
        compose_form_id = ir_model_data.get_object_reference('mail', 'email_compose_message_wizard_form')[1]
    except ValueError:
        compose_form_id = False
    ctx = {
        'default_model': 'exmaple.email',
        'default_res_id': self.ids[0],
        'default_use_template': bool(template_id),
        'default_template_id': template_id,
        'default_composition_mode': 'comment',
        'mark_so_as_sent': True,
        'force_email': True
    }
    return {
        'type': 'ir.actions.act_window',
        'view_type': 'form',
        'view_mode': 'form',
        'res_model': 'mail.compose.message',
        'views': [(compose_form_id, 'form')],
        'view_id': compose_form_id,
        'target': 'new',
        'context': ctx,
    }

In this manner, you can create email templates in odoo 12.


If you need any assistance in odoo, we are online, please chat with us.



1
Comments

Zubair

How to set default value for partner_ids in email template?

15/10/2021

-

9:31PM



Leave a comment



whatsapp
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