-1

I've encountered some problem when configuring my linux machine.

We are configuring fetchmail and procmail in order to receive the email from the external mail server.The problem start when we want to get the body content of the email by using "pipe" command in the procmail recipe.

:0fbw
| head -10

We discover that we cannot separate the mime part of the email with the body content of the email.Some of the output will show like this:

Received: by 10.52.155.193 with SMTP id vy1mr1915645vdb.123.1337929665158;
Fri, 25 May 2012 00:07:45 -0700 (PDT)
Received: by 10.52.34.75 with HTTP; Fri, 25 May 2012 00:07:45 -0700 (PDT)
Date: Fri, 25 May 2012 15:07:45 +0800
Message-ID: <CAE1Fe-oWCcM=K9+Nzx1c8kpDd80X3NXmf7XT_8L_KphGVczS=Q@mail.gmail.com>
Subject: test procmail
From: newbie my <newbie.my@gmail.com>
To: qaf azmi <qafazmi@gmail.com>
Content-Type: multipart/alternative; boundary=bcaec53ae9ee6d1da904c0d706c2

--bcaec53ae9ee6d1da904c0d706c2
Content-Type: text/plain; charset=ISO-8859-1

hi there.this is for testing only.

--bcaec53ae9ee6d1da904c0d706c2
Content-Type: text/html; charset=ISO-8859-1

hi there.this is for testing only.<br>

--bcaec53ae9ee6d1da904c0d706c2--

We only need the content of the body.So we figure out to convert this email into plain text.Then we will read try to take out the content by defining the content line in AWK. Its a big problem when different kind of email (ex:gmail,yahoo,etc)have their own format.So reading by line seems impossible as we do not know which line to read for the incoming email. Can anybody shed some light on this?Or different solution that we can take to tackle this problem?Thanks.

user119720
  • 380
  • 3
  • 6
  • 19
  • Suspiciously similar to http://stackoverflow.com/questions/10734412/procmail-recipe-to-remove-footer -- down to the user name. – tripleee May 31 '12 at 12:47

1 Answers1

2

You could try hack this with text processing tools, but the best option is to use something which actually parses it as a MIME message. Python's email module can do this, so you could use something like the following to output the message text. (This does assume that there is a text/plain representation in MIME messages -- if that's not always true you'll have to do some HTML rendering.)

#!/usr/bin/env python

from email.Parser import Parser
from sys import stdin, stdout

message = Parser().parse(stdin)
if not message.is_multipart():
    stdout.write(message.get_payload(decode=True))
else:
    for part in message.get_payload():
        if part.get_content_type() == 'text/plain':
            stdout.write(part.get_payload(decode=True))

This script reads a message from stdin and outputs the body on stdout. To use it, save the contents in a file such as ~/bin/output-body.py and make it executable. Then just pipe messages into it, for example cat /tmp/sample.eml | ~/bin/output-body.py.

mgorven
  • 30,036
  • 7
  • 76
  • 121
  • can you give me some step do it in the procmail?because i do not have basic in phyton.so i could not implement this unless i have learned phyton first. – user119720 May 28 '12 at 08:08
  • Added steps on how to use the script. – mgorven May 28 '12 at 08:12
  • i have try your code but it show this error `Traceback (most recent call last): File "/usr/bin/output-body.py", line 3, in ? from email.parser import Parser ImportError: No module named parser` is there something wrong in my config? – user119720 May 28 '12 at 08:41
  • I'm guessing that you're using Python 2.4, because the module name changed in 2.5 (`email.Parser` to `email.parser`). Updated script. – mgorven May 28 '12 at 18:17