2

I'm building a data loggerr and I've spent a lot of time trying to get this right, every forum takes me in a different direction and I think a weekend of googling warrants submitting a question here.

I'm running Ubuntu 12.10, I use fetchmail to get my mail, it sends it to procmail, and I have procmail piping it to a python script that is supposed to parse the body, and save it to a text file. The problem is I can't figure out how to write a python script that will do this, every example I find online is a bit over my head and I was hoping someone could take a little time to help me understand how this could be accomplished.

user2037800
  • 21
  • 1
  • 2

1 Answers1

7

In .promailrc, use the following recipe to pipe all emails into the python script:

:0Wc:
| /usr/bin/python [PATH TO PYTHON SCRIPT]

In the python script you can receive the incoming email using:

import sys
import email

full_msg = sys.stdin.readlines()

msg = email.message_from_string(full_msg.join());

to = msg['to']
from = msg['from']
subject = msg['subject']
body = msg['body']
  • thanks for your reply. I started back up on this project and was able to parse the body like this: – user2037800 Mar 18 '13 at 05:04
  • Python 3: `msg = email.message_from_string(''.join(full_msg))` – schroeder Aug 18 '13 at 01:26
  • full_msg is a list and does not have a join function. I am using python 2.7. But @schroeder method is working. My problem is that the body is coming as none even if the body of email exist. – Anuj Feb 10 '14 at 13:02
  • For future reference, `msg['body']` doesn't seem to return anything, you'll want `msg.get_payload()` – Ian Ling Dec 13 '16 at 22:57