Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to get information from the body of an email for a lead

By
Jayne Taylor
on 11/3/15, 4:09 AM 389 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?


0
Nathan Reid
On 4/8/16, 2:46 AM

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

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 11/3/15, 4:09 AM
Seen: 389 times
Last updated: 4/8/16, 2:46 AM