-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 <[email protected]>
To: qaf azmi <[email protected]>
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.

1

1 Answer 1

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.

4
  • 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, 2012 at 8:08
  • Added steps on how to use the script.
    – mgorven
    May 28, 2012 at 8: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, 2012 at 8: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, 2012 at 18:17

Not the answer you're looking for? Browse other questions tagged .