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':''})