PGP/MIME handler

Problem discussion

RFC2015 defines a proposed internet standard for sending PGP-encrypted mails. Although only very few MUAs currently support it (Mutt, for example), decrypting and verifying such mails is very uncomfortable. Usually, you will need to save the message in a file and verify / decrypt it manually. Moreover, unencrypted messages with a detached signature cannot (without a significant amount of work) be verified at all. This can be pretty annoying.

Although there is hope with the AEGYPTEN project, I think a smaller (less featured) and generically applicable solution would be fine as well. Moreover, I sometimes am an impatient person ;-)



This script takes a complete mail text on STDIN. It then tries to parse it using the great and famous MIME::Utils by ERYQ, determines the Content-Type and acts accordingly: Signed mails are verified and the result (output by gpg) is attached.

If we're dealing with an encrypted mail, the mail is decrypted. This handles attachments as well.

For decrypting the message, of course, the passphrase for gpg is required. You need a program that can ask you the phrase and outputs that (and nothing else) on STDOUT. Theoretically, you could use a program like echo "my secret passphrase", but it is obvious that this is not really secure. So I recommend the password-dialog-enriched version of kdialog available here for all the KDE users with versions prior to 3.1 out there. Kdialog has been integrated into KDE 3.1.



  • handles RFC-2015-compliant mails such as those produced by mutt, KMail (with OpenPGP-Plugin), evolution

  • works with any program that can filter mails through a program reading/writing from/to STDIN/STDOUT, such as KMail (via a filter rule) and Pine (with a little tweaking, see the HOWTO).



  • performance is rather slow - this is still perl


    Known Problems

  • none, actually :)



  • Perl in a recent version (5.005_03 or later) should do, simply try it. I am using v5.8.0.

  • KMail 1.1.99 or later. 1.3 up to 1.5.3 have been used (read: tested). No KMail is needed if you don't want to use this script as a mail filter in KMail, of course.

  • gpg 1.0.4 or later.

  • MIME::Parser;

  • GnuPG::Interface;

  • GnuPG::Options;

  • IO::Handle;

    The above mentioned Perl modules are all obtainable via CPAN.



  • Download the script and verify its signature.

  • Put it wherever you like, e.g. ~/bin/

  • Install all the required perl modules mentioned above.

  • Look into the source code and change the configuration settings. You can test your installation by taking any mail, saving it into mail.txt and doing things like

    cat mail.txt | > testmail.txt && less testmail.txt (or similar).

    That's it. Adding a filter rule in KMail is quite trivial. Enjoy!-)



  • I don't bite. If there is anything you'd like to add, comment/rant on, if you have any better ideas, please feel free to contact me.

  • Versions prior to 0.1.5 had a bug that made signatures incorrectly seem wrong because of not having converted LF to CRLF line endings, as required by RFC 2015 and 3156. John Wright provided me with a patch - thanks.

    He also wrote some documentation on how to get pgp-mime-handler running with pine.

    If you make any changes to the source code, I'd like to be notified, too. The license for this script is the LGPL.



    15.0 K