Dr.Derp
Dr.Derp

Reputation: 1

Extracting content out of Email within Odoo

i try to make my problem as clear as possible to you. I have set up an incoming email server within Odoo. Every incoming mail creates a new applicant for my hr_recruitment.

The default code for this action in the module looks like this:

def message_new(self, cr, uid, msg, custom_values=None, context=None):
    """ Overrides mail_thread message_new that is called by the mailgateway
        through message_process.
        This override updates the document according to the email. """ 

if custom_values is None:
        custom_values = {}
    val = msg.get('from').split('<')[0]
    defaults = {
        'name':  msg.get('subject') or _("No Subject"),
        'partner_name': val,
        'email_from': msg.get('from'),
        'email_cc': msg.get('cc'),
        'user_id': False,
        'partner_id': msg.get('author_id', False), }

Example for the incoming email body:

User ID: 1234
User Name: Nicolas Mustermann
Programming Skills: Java, C++, Python
Country: Germany
etc.

Everything works fine but i want that e.g. 'name' gets its value (Nicolas Mustermann) out of the email body. How do i have to change the line :'partner_name': val, to achive that?

Best regards

Upvotes: 0

Views: 724

Answers (1)

CzechErface
CzechErface

Reputation: 326

I am assuming that the e-mail body is in the msg variable. If that is the case then you want to get a substring that only contains the name on the second line and pass that to your code that creates the applicant. Here is a function that does that and accounts for some corner cases along the way:

def get_name_from_msg(msg):
    prefix = 'User Name: '
    name_start = msg.find(prefix)
    if name_start == -1:
        return None
    name_start += len(prefix)
    name_end = msg.find('\n', name_start)
    return msg[name_start:] if name_end == -1 else msg[name_start:name_end]

This function will return None if there exists no 'User Name: ' in the msg.

I would like someone to post a more "pythonic" way of doing this, however. I feel that this method is very bulky.

Upvotes: 0

Related Questions