简体   繁体   中英

procmail recipe to remove footer

I've encountered some problem when doing the procmail recipe.

Here what I have get so far :

  :0
     * ^X-Loop: myemail@gmail\.com
     /dev/null

     :0

    # filtering email by number 60
     * ^Subject:.*(60)
    {
      :0c:
      ${DEFAULT}

      #trying to take out input from the body
      :0fb
      | head -10

      #Forward it to the other folder
      :0
      mytest/
      }

The problem occur when procmail reading the body of the email.It will show output like this :

   +96szV6aBDlD/F7vuiK8fUYVknMQPfPmPNikB+fdYLvbwsv9duz6HQaDuwhGn6dh9w2U
   1sABcykpdyfWqWhLt5RzCqppYr5I4yCmB1CNOKwhlzI/w8Sx1QTzGT32G/ERTlbr91BM VmNQ==
   MIME-Version: 1.0
   Received: by 10.52.97.41 with SMTP id dx9mr14500007vdb.89.1337845760664; Thu,
   24 May 2012 00:49:20 -0700 (PDT)
   Received: by 10.52.34.75 with HTTP; Thu, 24 May 2012 00:49:20 -0700 (PDT)
   Date: Thu, 24 May 2012 15:49:20 +0800
   Message-ID: <CAE1Fe-r4Lid+YSgFTQdpsniE_wzeGjETWLLJJxat+HK94u1=AQ@mail.gmail.com>
   Subject: 60136379500
   From: my email <my email@gmail.com>
   To: your email <your email@gmail.com>
   Content-Type: multipart/alternative; boundary=20cf307f380654240604c0c37d07

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

   hi
   there
   how
   are
   you

   --20cf307f380654240604c0c37d07
   +96szV6aBDlD/F7vuiK8fUYVknMQPfPmPNikB+fdYLvbwsv9duz6HQaDuwhGn6dh9w2U
   1sABcykpdyfWqWhLt5RzCqppYr5I4yCmB1CNOKwhlzI/w8Sx1QTzGT32G/ERTlbr91BM VmNQ==

I have manage to get the output but it is not working if the sender send fewer than 3 lines as the output will print out the footer of the email as well (because it is between the range of head -10).

I only want the body of the email to be filter (print out in text file) in the procmail. Is it possible?Can anyone show me the way?I'm in my wits ends.Thanks

Attempting to treat a MIME multipart as just a lump of text is fraught with peril. In order to properly process the body, you should use a MIME-aware tool. But if you just want to assume that the first part is a text part and drop all other parts, you can create something fairly simple and robust.

# Truncate everything after first body part:
# Change second occurrence of --$MATCH to --$MATCH--
# and trim anything after it
:0fb
* ^Content-type: multipart/[a-z]+; boundary="\/[^"]+
| sed -e "1,/^--$MATCH$/b" -e "/^--$MATCH$/!b" -e 's//&--/' -eq

For elegance points, you might be able to develop the script to implement your 10-line body truncation action at the same time, but at least, this should hopefully get you started. (I would switch to awk or Perl at this point.)

:0fb
* ^Content-type: multipart/[a-z]+; boundary="\/[^"]+
| awk -v "b=--$MATCH" ' \
    ($0 == b || $0 == b "--") && seen++ { printf "%s--\n", $0; exit } \
    !seen || p++ < 10'

Properly, the MIME part's headers should not count towards the line count.

This is slightly speculative; I assume by "footer" you mean the ugly base64-encoded attachment after the first body part, and of course, this recipe will do nothing at all for single-part messages. Maybe you want to fall back to your original recipe for those.

Recently had a similar issue and solved it with this (adapted to OP)...

#trying to take out input from the body
:0fb
| sed -n '/^Content-Type/,/^--/ { /^Content-Type/b; /^--/b; p }'

Explanations: in general form....

sed -n '/begin/,/end/ { /begin/b; /end/b; p }'

-n:         --> turn printing off
/begin/     --> begin of pattern range (remainder commands only apply inside range)
,/end/      --> , end of sed pattern range
{ /begin/b; --> /b branch causes lines with pattern /begin/ to skip remaining commands
/end/b;     --> (same as above), these lines will skip the upcoming (p)rint command
p }'        --> prints lines that in pattern that made it to this command

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM