« Back to Blogs

Handling E-mail template in Odoo V9

Nowadays mailing mechanism is too fast for any communication, Odoo is providing functionality to send and receive email from odoo to user’s mail account using outgoing mail & incoming mail server configuration.

In this blog I am going to explain how to create your own custom email template and how to send them to the user’s email account using server action and workflow.

Prerequisite:

Create email template and use it in place of default email template by following steps:

  1. Create an email template xml file in your module and copy below code in it.

 

<?xml version="1.0" ?>
<openerp>
<data>
  <record id="custom_template_id" model="mail.template">
    <field name="name">Custom Email Template</field>
    <field name="email_from">${object.company_id and object.company_id.email or ''}</field>
            <field name="subject">Custom mail testing</field>
            <field name="email_to"> ${object.user_id.email} </field> 
            <field name="email_cc"> ${object.user_id.parent_id.email} </field>
            <field name="reply_to"> preferred Reply address</field>
            <field name="model_id" ref="base_module.model_modelname"/>
            <field name="auto_delete" eval="True"/>
            <field name="user_signature" eval="True"/>
            <field name="body_html"><![CDATA[
                  <div>
                       <p>
                           <!-- Your e-mail body (the HTML) will be added here -->
                       </p>
                   </div>   ]]>
              </field>
   </record>
</data>
</openerp>

 

where,

  • name : name of the template
  • email_from: sender address
  • email_to: destination mail address
  • reply_to : preferred reply address (optional)
  • subject : subject of the mail
  • model_id : your custom module name with model_<modelname> ( Use base module if you want to inherit base template, i.e hr_holidays.model_hr_holidays)
  • body : body of the email [either plain text or HTML format]
  • user_signature: predefined signature for all if true. (optional)
  • auto_delete : After sending mail successfully, email will be deleted from Emails. (optional)
  1. Add following code to create server action in the same template xml file.

<record id="custom_server_action_id" model="ir.actions.server">
       <field name="name">Custom Email Action</field>
       <field name="model_id" ref="model_modelname"/>
       <field name="condition">True</field>
       <field name="type">ir.actions.server</field>
       <field name="state">email</field>
       <field name="template_id" ref="custom_template_id"/>
</record>
  1. Inherit or create workflow file to call server action.

  • Inherit workflow to break default functionality and attach your custom email template using below code. As per changing the state's value, corresponded server action will be call automatically.
<?xml version="1.0" ?>
<openerp>
<data>
    <record model="workflow.activity" id="base_module.wrk_act_id">
<field name="wkf_id" ref="base_module.wkf_id" />
<field name="name">state_name</field>
<field name="flow_start" eval="False"/>
<field name="kind">function</field>
<!-- add your server action id here -->
<field name="action_id" ref="custom_server_action_id"/>
<field name="action">base_function()</field>
    </record>
</data>
</openerp>

 

Note: You can create new workflow file as same as above, if workflow file is not exist.

  1. Don’t forget to add both template xml and workflow xml file in __openerp__.py file.

  2. Once your server action will be called, automatically mail will be sent to the users within specific time interval which is mentioned in Settings > Technical > Automation > Scheduled Actions > Email Queue Manager. You can change the time interval as per your need.

After successful execution of above, you can easily create email templates and send mail from Odoo with desire template. It only requires a few lines of code and all the rest is handled by Odoo itself.

For more implementation details or support, you may contact us at enquiry@surekhatech.com.

Comments
No comments yet. Be the first.
contact-us Request a callback