Parsing email with Python -


I am writing a Python script to process the returned emails as suggested in this, I have the following Using Procmail config:

 : 0: | $ HOME / process_mail.py  

My process_mail.py script is getting an email through stdin like this:

  Hostname from Tuesdays June 15 21:43:30 2010 Received: (QM 8580 applicable from Network); 15 June 2010 21:43:22 -0400 received: by mail-fx0-f44.google.com (20 9.85.161.44) IP-73-187-35-131. IPSUSSAVERVER. From SMTP; 15 June 2010 21:43:22 -0400 Received: With SMTP ID 19so170709fxm.3 by fxm19 & lt; Username@domain.com> ;; Mars, 15 June 2010 18:47:33 -0700 (PDT) MIME-version: 1.0 Received: SMTP ID M1 MR 2774225 MUL .26.1276652853684 with 10.103.84.1; Tuesday, 15 June 2010 18:47:33 -0700 (PDT) Received: 10.123.143.4 by HTTPS; Tuesday, June 15, 2010 18:47:33 -0700 (PDT) Date: Tuesday, 15 June 2010 20:47:33 -0500 Message-ID: & lt; AANLkTikFsIjJ3KYW1HJWcAqQlGXNiXE2YMzrj39I0tdB@mail.gmail.com> Subject: By Exam 12: Full Name & lt; Username@sender.com> To: username@domain.com Content-Type: Text / Plain; Charset = ISO-8859-1 One Two Three  

I am trying to parse the message like this:

  & gt; & Gt; & Gt; Import Emails & gt; & Gt; & Gt; Msg = email.message_from_string (full_message)  

I want to receive message fields like 'to', 'per' and 'subject'. However, the message object does not contain any of these fields.

Am I doing wrong?

You should ensure that the lines are not broken by mistake (as they are above, though It is difficult to say that this was a copy paste problem) - with an intact message such as:

  received: (QM 8580 applicable from network); 15 June 2010 21:43:22 -0400 received: by mail-fx0-f44.google.com (20 9.85.161.44) IP-73-187-35-131. IPSUSSAVERVER. From SMTP; 15 June 2010 21:43:22 -0400 Received: With SMTP ID 19so170709fxm.3 by fxm19 & lt; Username@domain.com> ;; Mars, 15 June 2010 18:47:33 -0700 (PDT) MIME-version: 1.0 Received: SMTP ID M1 MR 2774225 MUL .26.1276652853684 with 10.103.84.1; Tuesday, 15 June 2010 18:47:33 -0700 (PDT) Received: 10.123.143.4 by HTTPS; Tuesday, June 15, 2010 18:47:33 -0700 (PDT) Date: Tuesday, 15 June 2010 20:47:33 -0500 Message-ID: & lt; AANLkTikFsIjJ3KYW1HJWcAqQlGXNiXE2YMzrj39I0tdB@mail.gmail.com> Subject: By Exam 12: Full Name & lt; Username@sender.com> To: username@domain.com Content-Type: Text / Plain; Charset = ISO-8859-1 one two / three  

then

  msg = email.message_from_string (msgtxt) print msg ['subject']  

Print Test 12 as desired.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -