creating-email-templates-odoo-12.png
Blogger_637005666753977292.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 transacting necessary communication from one destination 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. 

So let us see 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

'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 have 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 I need to send.

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

6. email_to(mandatory)
Here we can give the recipients 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 email subject like that are given 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 on a record and fill it in on the Email automatically. 

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

The next following thing is to create python function like given below. Here calling 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,
    }

Hope the blog was useful.





cybrosys youtube

Comments

0


Leave a comment

 
Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

London

Cybrosys Limited
Alpha House,
100 Borough High Street, London,
SE1 1LB, United Kingdom

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message