Skip to Content
Menu
This question has been flagged
1 Reply
8997 Views

I have my incoming email server set up to create a new lead when an email is sent to a particular address.  How do I get access to the body of the email so I can populate other fields in the lead?

I assume that I should create a server action and write some python code to parse the body of the email. When you create the server action you fill in a base model. For me "crm.lead."  This model does not seem to have access to the email that is being used to create the lead.  Can anyone point me in the right direction?


Avatar
Discard
Best Answer

I came up with the following for Odoo9.

It parses the incoming email (and the Notes field in the Lead or Opportunity form) and populates the respective fields.

There are a couple of things hard coded in for my specific application (user, campaign etc.).

I have 2 web forms sending content to an email address, both with slightly different formatting so the Action parses them differently.

If there's nothing in the Notes field, it will search the mail_message table for an email with the same resource id and parse the body of that instead. If you use it as a Server Action when the lead is created there should only be one matching email.

It's not bulletproof and there may be better ways to do this but it works for me and could be a start for you...


 def parse_description(description):
  fields=['Name','Contact Number','Email']
  pdict={}
  split=description.splitlines()
  for line in split:
    for field in fields:
        if field in line:
            split_line=line.split(':')
            if len(split_line)>1:
                pdict[field]=split_line[1]
  return  pdict

def parse_description_contact(description):
  fields=['Name','Contact Number','Email']
  pdict={}
  split=description.splitlines()
  for line in split:
    for field in fields:
        if field in line:
            next_line=split.index(field)+1
            if split[next_line]:
                pdict[field]=split[next_line]
  return  pdict

campaign=0
lead=self.browse(cr,uid,context['active_id'],context=context)
description=lead['description']

# GET CONTENTS OF EMAIL from mail_message IF NOTHING IS IN DESCRIPTION
 if not description:
  activeid=lead['id']
  mailcontent = env['mail.message'].search([('res_id','=',activeid),('message_type','=','email')])
  description=mailcontent['body']

if description:
    if "Contact Us" in description:
        campaign=68
        ddict=parse_description_contact(description)
    if "Budget Estimator" in description:
        campaign=69
        ddict=parse_description(description)
    if campaign:
        #self.write(cr,uid,context['active_id'],{'campaign_id':campaign})
        self.write(cr,uid,context['active_id'],{
                        'user_id':37,
                        'partner_id':None,
                        'name':ddict.get('Name'),
                        'partner_name':ddict.get('Name'),
                        'contact_name':ddict.get('Name'),
                        'phone':ddict.get('Contact Number'),
                        'mobile':ddict.get('Contact Number'),
                        'email_from':ddict.get('Email'),
                        'type':'lead',
                        'campaign_id':campaign,
                        'stage_id':''})

Avatar
Discard
Related Posts Replies Views Activity
0
Aug 22
1983
3
Mar 15
4244
0
Mar 15
3981
0
Mar 15
3924
0
Jul 16
3821