email-aliasing-in-odoo.png
Blogger_636313953512117529.jpg
By: Jesni

E-mail aliasing in Odoo

Email aliasing is a default feature in Odoo which allows the user to fetch contents to ERP records directly from incoming emails. The feature is a type of automation. Since we can configure the E-mails to fetch contents directly; it reduces the human effort and simplifies the business process.

 

To do so we have to follow the following steps.

1. Configure default alias name and alias domain

2. Create a record in model ‘Aliases’

3. Change ‘message new’ global function according to your need

4. Configure incoming mail with an Action

 

Configure default alias name and alias domain

 

The first step is to configure the default alias name and domain.  We can fetch the alias domain from ir.config parameter.  To perform these two tasks, we have to add following fields.

 

 

alias_prefix = fields.Char('Default Alias Name)

alias_domain = fields.Char('Alias Domain', default=lambda self: self.env["ir.config_parameter"].get_param("mail.catchall.domain"))

 

 

For eg: - Suppose we need to fetch emails from the mail id ‘leave_request@cybrosys.in’.

So here Alias name is ‘leave_request’ and alias domain is ‘cybrosys.in’.

We have to set default alias domain in general settings. For that, go to


Settings -> General Settings


And provide alias domain in the field.


email-aliasing-in-odoo-cybrosys


Create a record in model ‘Aliases’

 

The second step is to create a record in ‘Aliases’ model. For that, we have to add an XML file as follow.

 

<record id="mail_alias_leave" model="mail.alias">
        <field name="alias_name">leave</field>
        <field name="alias_model_id" ref="model_hr_holidays"/>
        <field name="alias_user_id" ref="base.user_root"/>
        <field name="alias_contact">employees</field>
</record>

 

Here we have to 'refer' the model in the field ‘alias_model_id’.

This is the model which we need to create the record from our incoming mail. Then we can see this record from user interface too.

Go to Settings-> Technical -> Email-> Aliases


email-aliasing-in-odoo-cybrosys


Change ‘message_new’ global function according to the need

 

The next step is to modify the global function ‘message_new’ according to the need. Here we can add parameters which we need to create a record into the dictionary ‘custom_values’.

When we update the ‘custom_values’ dictionary, our record will be created according to that information.

 

Eg:-

 

@api.model
def message_new(self, msg_dict, custom_values=None):
   if custom_values is None:
       custom_values = {}
   email_address = email_split(msg_dict.get('email_from', False))[0]
   user = self.env['res.users'].search([
       '|',
       ('email', 'ilike', email_address))
   ], limit=1)
   msg_subject = msg_dict.get('subject', '')
   msg_body = msg_dict.get('body', '')
   custom_values.update({
           'name': msg_subject.strip(),
           'user_id': user.id,
       })
   return super(HrLeaveAlias, self).message_new(msg_dict, custom_values)

 

 

Here we can find the incoming mail id by using ‘email_split’.

For that,  we need to import this library from odoo.tools.

Eg:- from odoo.tools import email_split

 

Also, we can fetch email subject and body from ‘msg’ parameter.


Eg:-


 

msg_subject = msg_dict.get('subject', '')
msg_body = msg_dict.get('body', '')

 

In custom values, we can update any values into the corresponding field.

 

Configure incoming mail with an Action

The final step is to configure the incoming emails with an action. 


email-aliasing-in-odoo-cybrosys


Form the label ‘Actions to Perform on Incoming Mails’, we can create actions on


1. Record

2. Server Action


If we select a record it will automatically create record while clicking on the button ‘Fetch New’. If we select a server action then it will run while clicking on the button ‘Fetch New’.






Comments

0


Leave a comment