Handling MIME data

Author: Adrián Pérez <aperez@igalia.com>
Copyright: 2008-2009 Igalia S.L.
License:GPL3

Abstract

Provides tools for handling MIME-encoded data. The format is defined in RFCs number 822 and 2822.

Contents

mime_decode

mime_decode hash < input > output-body

Interprets a set of MIME-like headers given as standard input and stores them parsed into a hash table. The MIME convention of stop parsing when an empty line is found is honored. Multi-line headers are properly recognized, as well as multiple header instances, which are handled somewhat gracefully: their values are stored under a single hash table key, appended with commas (this is specified in the standard, but no quoting is done, though). Header names are all converted to lowercase when storing them in the hash table.

As an example, you could extract headers from an e-mail, saving the body to another file using the following technique:

h=$(hash_new)
mime_decode $h < input-email > output-body