Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

| 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 with SMTP id vy1mr1915645vdb.123.1337929665158;
Fri, 25 May 2012 00:07:45 -0700 (PDT)
Received: by with HTTP; Fri, 25 May 2012 00:07:45 -0700 (PDT)
Date: Fri, 25 May 2012 15:07:45 +0800
Message-ID: <>
Subject: test procmail
From: newbie my <>
To: qaf azmi <>
Content-Type: multipart/alternative; boundary=bcaec53ae9ee6d1da904c0d706c2

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

hi there.this is for testing only.

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

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


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.

share|improve this question

closed as off-topic by Jenny D, Ward, MadHatter, mdpc, Jim B Sep 7 '14 at 2:37

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions must be relevant to professional system administration. Server Fault is dedicated to professional system and network administrators. End user and enthusiast questions are off-topic (contact your system administrator or hire a professional to help you out). Please see the Help Center for more information." – Jenny D, Ward, MadHatter, mdpc, Jim B
If this question can be reworded to fit the rules in the help center, please edit the question.

Suspiciously similar to… -- down to the user name. – tripleee May 31 '12 at 12:47

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():
    for part in message.get_payload():
        if part.get_content_type() == 'text/plain':

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/ and make it executable. Then just pipe messages into it, for example cat /tmp/sample.eml | ~/bin/

share|improve this answer
can you give me some step do it in the procmail?because i do not have basic in i could not implement this unless i have learned phyton first. – user119720 May 28 '12 at 8:08
Added steps on how to use the script. – mgorven May 28 '12 at 8:12
i have try your code but it show this error Traceback (most recent call last): File "/usr/bin/", 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 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 '12 at 18:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.