I'm struggling to get procmail to first transform, and then forward, a received email. Here's what I'd like to do:

:0 f
| ~/bin/encryptemail.pl | mail [email protected]

That is, I want to run the encryptemail.pl script on the body of email that I receive, and then forward the encrypted email to [email protected] .

Piping the output to mail sort of works, except that the forwarded email is wrapped in a second set of headers.

I'd like to cleanly forward the encrypted email so that the body of the original email has been replaced with the encrypted content.

2 Answers 2


I'd take a look at the recipes on the Procmail Documentation Project website, specifically this recipe: 15.7 Forwarding sensitive messages in encrypted format.

  #   by [alan]
  #   See if addressed *directly* to me, and ..
  #   ..has not already been forwarded

  KEY             = "TheMagic"
  FORWARD_EMAIL   = "[email protected]"

  *$   ^To:.*$LOGNAME(@|[^0-9a-z]|$)
  *$ ! ^$MYXLOOP
      # now let's encrypt the body using mimencode

      :0 fbw
      |   echo "MIME-Version: 1.0" ;                              \
          echo "Content-Type: application/crypt" ;                \
          echo "Content-transfer-encoding: base64" ;              \
          echo "" ;                                               \
          crypt $KEY | mimencode -b

      #   Now let's prepare the headers for forwarding the mail,
      #   and mark it so we don't loop

      :0 fhw
      | $FORMAIL   -I"Resent-To: $FORWARD_EMAIL" -I"$MYXLOOP"



Instead of calling this line:

crypt $KEY | mimencode -b

You should be able to substitute your call in it's place:

~/bin/encryptemail.pl | mimencode -b
  • Thanks! I'm actually trying to encrypt them with GnuPG using Mike Cardwell's gpgit script. I think that there's some conflict, in that gpgit wants to process the whole message, headers and all, but the procmail script you gave me is snipping off the headers and passing through the body.
    – loseeka
    Commented Jun 25, 2013 at 19:41
  • Right it's doing the body, but I'd look through the other recipes, there are other examples that might fit your needs.
    – slm
    Commented Jun 25, 2013 at 19:49

If you could change your script to accept and pass on (i.e. simply copy) the headers, you could do this:

| ~/bin/encryptemail.pl | sendmail -oi [email protected]

Equivalently, first replace the body, then send the whole modified message:

| ~/bin/encryptemail.pl
! [email protected]

The f flag cannot meaningfully be used on a delivering recipe; it instructs Procmail to replace the message (or, with the b flag too, the body) with the output from the commands you pipe to, and continue processing with the thusly modified message.

You must log in to answer this question.

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