This file documents the Emacs MIME interface functionality.

Copyright (C) 1998,99 Free Software Foundation, Inc.

Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.

Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided also that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.

Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.

Emacs MIME
**********

This manual documents the libraries used to compose and display MIME messages.

This is not a manual meant for users; it's a manual directed at people who want to write functions and commands that manipulate MIME elements.

MIME is short for Multipurpose Internet Mail Extensions. This standard is documented in a number of RFCs; mainly RFC2045 (Format of Internet Message Bodies), RFC2046 (Media Types), RFC2047 (Message Header Extensions for Non-ASCII Text), RFC2048 (Registration Procedures), RFC2049 (Conformance Criteria and Examples).  It is highly recommended that anyone who intends writing MIME-compliant software read at least RFC2045 and RFC2047.

Interface Functions
*******************

The mail-parse library is an abstraction over the actual low-level libraries that are described in the next chapter.

Standards change, and so programs have to change to fit in the new mold.  For instance, RFC2045 describes a syntax for the Content-Type header that only allows ASCII characters in the parameter list.  RFC2231 expands on RFC2045 syntax to provide a scheme for continuation headers and non-ASCII characters.

The traditional way to deal with this is just to update the library functions to parse the new syntax.  However, this is sometimes the wrong thing to do.  In some instances it may be vital to be able to understand both the old syntax as well as the new syntax, and if there is only one library, one must choose between the old version of the library and the new version of the library.

The Emacs MIME library takes a different tack.  It defines a series of low-level libraries (rfc2047.el, rfc2231.el and so on) that parses strictly according to the corresponding standard.  However, normal programs would not use the functions provided by these libraries directly, but instead use the functions provided by the mail-parse library.  The functions in this library are just aliases to the corresponding functions in the latest low-level libraries.  Using this scheme, programs get a consistent interface they can use, and library developers are free to create write code that handles new standards.

The following functions are defined by this library:

mail-header-parse-content-type
     Parse a Content-Type header and return a list on the following format:

          ("type/subtype"
           (attribute1 . value1)
           (attribute2 . value2)
           ...)

     Here's an example:

          (mail-header-parse-content-type
           "image/gif; name=\"b980912.gif\"")
          => ("image/gif" (name . "b980912.gif"))

mail-header-parse-content-disposition
     Parse a Content-Disposition header and return a list on the same format as the function above.

mail-content-type-get
     Takes two parameters---a list on the format above, and an attribute. Returns the value of the attribute.

          (mail-content-type-get
           '("image/gif" (name . "b980912.gif")) 'name)
          => "b980912.gif"

mail-header-encode-parameter
     Takes a parameter string and returns an encoded version of the string. This is used for parameters in headers like Content-Type and Content-Disposition.

mail-header-remove-comments
     Return a comment-free version of a header.

          (mail-header-remove-comments
           "Gnus/5.070027 (Pterodactyl Gnus v0.27) (Finnish Landrace)")
          => "Gnus/5.070027  "

mail-header-remove-whitespace
     Remove linear white space from a header.  Space inside quoted strings and comments is preserved.

          (mail-header-remove-whitespace
           "image/gif; name=\"Name with spaces\"")
          => "image/gif;name=\"Name with spaces\""

mail-header-get-comment
     Return the last comment in a header.

          (mail-header-get-comment
           "Gnus/5.070027 (Pterodactyl Gnus v0.27) (Finnish Landrace)")
          => "Finnish Landrace"

mail-header-parse-address
     Parse an address and return a list containing the mailbox and the plaintext name.

          (mail-header-parse-address
           "Hrvoje Niksic <hniksic@srce.hr>")
          => ("hniksic@srce.hr" . "Hrvoje Niksic")

mail-header-parse-addresses
     Parse a string with list of addresses and return a list of elements like the one described above.

          (mail-header-parse-addresses
           "Hrvoje Niksic <hniksic@srce.hr>, Steinar Bang <sb@metis.no>")
          => (("hniksic@srce.hr" . "Hrvoje Niksic")
               ("sb@metis.no" . "Steinar Bang"))

mail-header-parse-date
     Parse a date string and return an Emacs time structure.

mail-narrow-to-head
     Narrow the buffer to the header section of the buffer.  Point is placed at the beginning of the narrowed buffer.

mail-header-narrow-to-field
     Narrow the buffer to the header under point.

mail-encode-encoded-word-region
     Encode the non-ASCII words in the region.  For instance, Naïve is encoded as =?iso-8859-1?q?Na=EFve?=.

mail-encode-encoded-word-buffer
     Encode the non-ASCII words in the current buffer.  This function is meant to be called narrowed to the headers of a message.

mail-encode-encoded-word-string
     Encode the words that need encoding in a string, and return the result.

          (mail-encode-encoded-word-string
           "This is naïve, baby")
          => "This is =?iso-8859-1?q?na=EFve,?= baby"

mail-decode-encoded-word-region
     Decode the encoded words in the region.

mail-decode-encoded-word-string
     Decode the encoded words in the string and return the result.

          (mail-decode-encoded-word-string
           "This is =?iso-8859-1?q?na=EFve,?= baby")
          => "This is naïve, baby"

Currently, mail-parse is an abstraction over ietf-drums, rfc2047, rfc2045 and rfc2231.  These are documented in the subsequent sections.

Basic Functions
***************

This chapter describes the basic, ground-level functions for parsing and handling.  Covered here is parsing From lines, removing comments from header lines, decoding encoded words, parsing date headers and so on.  High-level functionality is dealt with in the next chapter (see Decoding and Viewing).

rfc2045
=======

RFC2045 is the ``main'' MIME document, and as such, one would imagine that there would be a lot to implement.  But there isn't, since most of the implementation details are delegated to the subsequent RFCs.

So rfc2045.el has only a single function:

rfc2045-encode-string
     Takes a parameter and a value and returns a PARAM=VALUE string. VALUE will be quoted if there are non-safe characters in it.

rfc2231
=======

RFC2231 defines a syntax for the Content-Type and Content-Disposition headers.  Its snappy name is MIME Parameter Value and Encoded Word Extensions: Character Sets, Languages, and Continuations.

In short, these headers look something like this:

     Content-Type: application/x-stuff;
      title*0*=us-ascii'en'This%20is%20even%20more%20;
      title*1*=%2A%2A%2Afun%2A%2A%2A%20;
      title*2="isn't it!"

They usually aren't this bad, though.

The following functions are defined by this library:

rfc2231-parse-string
     Parse a Content-Type header and return a list describing its elements.

          (rfc2231-parse-string
           "application/x-stuff;
           title*0*=us-ascii'en'This%20is%20even%20more%20;
           title*1*=%2A%2A%2Afun%2A%2A%2A%20;
           title*2=\"isn't it!\"")
          => ("application/x-stuff"
              (title . "This is even more ***fun*** isn't it!"))

rfc2231-get-value
     Takes one of the lists on the format above and returns the value of the specified attribute.

rfc2231-encode-string
     Encode a parameter in headers likes Content-Type and Content-Disposition.

ietf-drums
==========

drums is an IETF working group that is working on the replacement for RFC822.

The functions provided by this library include:

ietf-drums-remove-comments
     Remove the comments from the argument and return the results.

ietf-drums-remove-whitespace
     Remove linear white space from the string and return the results. Spaces inside quoted strings and comments are left untouched.

ietf-drums-get-comment
     Return the last most comment from the string.

ietf-drums-parse-address
     Parse an address string and return a list that contains the mailbox and the plain text name.

ietf-drums-parse-addresses
     Parse a string that contains any number of comma-separated addresses and return a list that contains mailbox/plain text pairs.

ietf-drums-parse-date
     Parse a date string and return an Emacs time structure.

ietf-drums-narrow-to-header
     Narrow the buffer to the header section of the current buffer.

rfc2047
=======

RFC2047 (Message Header Extensions for Non-ASCII Text) specifies how non-ASCII text in headers are to be encoded.  This is actually rather complicated, so a number of variables are necessary to tweak what this library does.

The following variables are tweakable:

rfc2047-default-charset
     Characters in this charset should not be decoded by this library. This defaults to iso-8859-1.

rfc2047-header-encoding-list
     This is an alist of header / encoding-type pairs.  Its main purpose is to prevent encoding of certain headers.

     The keys can either be header regexps, or t.

     The values can be either nil, in which case the header(s) in question won't be encoded, or mime, which means that they will be encoded.

rfc2047-charset-encoding-alist
     RFC2047 specifies two forms of encoding---Q (a Quoted-Printable-like encoding) and B (base64).  This alist specifies which charset should use which encoding.

rfc2047-encoding-function-alist
     This is an alist of encoding / function pairs.  The encodings are Q, B and nil.

rfc2047-q-encoding-alist
     The Q encoding isn't quite the same for all headers.  Some headers allow a narrower range of characters, and that is what this variable is for.  It's an alist of header regexps / allowable character ranges.

rfc2047-encoded-word-regexp
     When decoding words, this library looks for matches to this regexp.

Those were the variables, and these are this functions:

rfc2047-narrow-to-field
     Narrow the buffer to the header on the current line.

rfc2047-encode-message-header
     Should be called narrowed to the header of a message.  Encodes according to rfc2047-header-encoding-alist.

rfc2047-encode-region
     Encodes all encodable words in the region specified.

rfc2047-encode-string
     Encode a string and return the results.

rfc2047-decode-region
     Decode the encoded words in the region.

rfc2047-decode-string
     Decode a string and return the results.

time-date
=========

While not really a part of the MIME library, it is convenient to document this library here.  It deals with parsing Date headers and manipulating time.  (Not by using tesseracts, though, I'm sorry to say.)

These functions convert between five formats: A date string, an Emacs time structure, a decoded time list, a second number, and a day number.

The functions have quite self-explanatory names, so the following just gives an overview of which functions are available.

     (parse-time-string "Sat Sep 12 12:21:54 1998 +0200")
     => (54 21 12 12 9 1998 6 nil 7200)

     (date-to-time "Sat Sep 12 12:21:54 1998 +0200")
     => (13818 19266)

     (time-to-seconds '(13818 19266))
     => 905595714.0

     (seconds-to-time 905595714.0)
     => (13818 19266 0)

     (time-to-day '(13818 19266))
     => 729644

     (days-to-time 729644)
     => (961933 65536)

     (time-since '(13818 19266))
     => (0 430)

     (time-less-p '(13818 19266) '(13818 19145))
     => nil

     (subtract-time '(13818 19266) '(13818 19145))
     => (0 121)

     (days-between "Sat Sep 12 12:21:54 1998 +0200"
                   "Sat Sep 07 12:21:54 1998 +0200")
     => 5

     (date-leap-year-p 2000)
     => t

     (time-to-day-in-year '(13818 19266))
     => 255

And finally, we have safe-date-to-time, which does the same as date-to-time, but returns a zero time if the date is syntactically malformed.

qp
==

This library deals with decoding and encoding Quoted-Printable text.

Very briefly explained, qp encoding means translating all 8-bit characters (and lots of control characters) into things that look like =EF; that is, an equal sign followed by the byte encoded as a hex string.

The following functions are defined by the library:

quoted-printable-decode-region
     QP-decode all the encoded text in the specified region.

quoted-printable-decode-string
     Decode the QP-encoded text in a string and return the results.

quoted-printable-encode-region
     QP-encode all the encodable characters in the specified region.  The third optional parameter FOLD specifies whether to fold long lines. (Long here means 72.)

quoted-printable-encode-string
     QP-encode all the encodable characters in a string and return the results.

base64
======

Base64 is an encoding that encodes three bytes into four characters, thereby increasing the size by about 33%.  The alphabet used for encoding is very resistant to mangling during transit.

The following functions are defined by this library:

base64-encode-region
     base64 encode the selected region.  Return the length of the encoded text.  Optional third argument NO-LINE-BREAK means do not break long lines into shorter lines.

base64-encode-string
     base64 encode a string and return the result.

base64-decode-region
     base64 decode the selected region.  Return the length of the decoded text.  If the region can't be decoded, return nil and don't modify the buffer.

base64-decode-string
     base64 decode a string and return the result.  If the string can't be decoded, nil is returned.

binhex
======

binhex is an encoding that originated in Macintosh environments. The following function is supplied to deal with these:

binhex-decode-region
     Decode the encoded text in the region.  If given a third parameter, only decode the binhex header and return the filename.

uudecode
========

uuencode is probably still the most popular encoding of binaries used on Usenet, although base64 rules the mail world.

The following function is supplied by this package:

uudecode-decode-region
     Decode the text in the region.

rfc1843
=======

RFC1843 deals with mixing Chinese and ASCII characters in messages.  In essence, RFC1843 switches between ASCII and Chinese by doing this:

     This sentence is in ASCII.
     The next sentence is in GB.~{<:Ky2;S{#,NpJ)l6HK!#~}Bye.

Simple enough, and widely used in China.

The following functions are available to handle this encoding:

rfc1843-decode-region
     Decode HZ-encoded text in the region.

rfc1843-decode-string
     Decode a HZ-encoded string and return the result.

mailcap
=======

The ~/.mailcap file is parsed by most MIME-aware message handlers and describes how elements are supposed to be displayed. Here's an example file:

     image/*; gimp -8 %s
     audio/wav; wavplayer %s

This says that all image files should be displayed with xv, and that realaudio files should be played by rvplayer.

The mailcap library parses this file, and provides functions for matching types.

mailcap-mime-data
     This variable is an alist of alists containing backup viewing rules.

Interface functions:

mailcap-parse-mailcaps
     Parse the ~/.mailcap file.

mailcap-mime-info
     Takes a MIME type as its argument and returns the matching viewer.

Decoding and Viewing
********************

This chapter deals with decoding and viewing MIME messages on a higher level.

The main idea is to first analyze a MIME article, and then allow other programs to do things based on the list of handles that are returned as a result of this analysis.

Dissection
==========

The mm-dissect-buffer is the function responsible for dissecting a MIME article.  If given a multipart message, it will recursively descend the message, following the structure, and return a tree of MIME handles that describes the structure of the message.

Handles
=======

A MIME handle is a list that fully describes a MIME component.

The following macros can be used to access elements in a handle:

mm-handle-buffer
     Return the buffer that holds the contents of the undecoded MIME part.

mm-handle-type
     Return the parsed Content-Type of the part.

mm-handle-encoding
     Return the Content-Transfer-Encoding of the part.

mm-handle-undisplayer
     Return the object that can be used to remove the displayed part (if it has been displayed).

mm-handle-set-undisplayer
     Set the undisplayer object.

mm-handle-disposition
     Return the parsed Content-Disposition of the part.

mm-handle-disposition
     Return the description of the part.

mm-get-content-id
     Returns the handle(s) referred to by Content-ID.

Display
=======

Functions for displaying, removing and saving.

mm-display-part
     Display the part.

mm-remove-part
     Remove the part (if it has been displayed).

mm-inlinable-p
     Say whether a MIME type can be displayed inline.

mm-automatic-display-p
     Say whether a MIME type should be displayed automatically.

mm-destroy-part
     Free all resources occupied by a part.

mm-save-part
     Offer to save the part in a file.

mm-pipe-part
     Offer to pipe the part to some process.

mm-interactively-view-part
     Prompt for a mailcap method to use to view the part.

Composing
*********

Creating a MIME message is boring and non-trivial.  Therefore, a library called mml has been defined that parses a language called MML (MIME Meta Language) and generates MIME messages.

The main interface function is mml-generate-mime.  It will examine the contents of the current (narrowed-to) buffer and return a string containing the MIME message.

Simple MML Example
==================

Here's a simple multipart/alternative:

     <#multipart type=alternative>
     This is a plain text part.
     <#part type=text/enriched>
     <center>This is a centered enriched part</center>
     <#/multipart>

After running this through mml-generate-mime, we get this:

     Content-Type: multipart/alternative; boundary="=-=-="

     --=-=-=

     This is a plain text part.

     --=-=-=
     Content-Type: text/enriched

     <center>This is a centered enriched part</center>

     --=-=-=--

MML Definition
==============

The MML language is very simple.  It looks a bit like an SGML application, but it's not.

The main concept of MML is the part.  Each part can be of a different type or use a different charset.  The way to delineate a part is with a <#part ...> tag.  Multipart parts can be introduced with the <#multipart ...> tag.  Parts are ended by the <#/part> or <#/multipart> tags.  Parts started with the <#part ...> tags are also closed by the next open tag.

There's also the <#external ...> tag.  These introduce external/message-body parts.

Each tag can contain zero or more parameters on the form parameter=value.  The values may be enclosed in quotation marks, but that's not necessary unless the value contains white space.  So filename=/home/user/#hello$^yes is perfectly valid.

The following parameters have meaning in MML; parameters that have no meaning are ignored.  The MML parameter names are the same as the MIME parameter names; the things in the parentheses say which header it will be used in.

type
     The MIME type of the part (Content-Type).

filename
     Use the contents of the file in the body of the part (Content-Disposition).

charset
     The contents of the body of the part are to be encoded in the character set speficied (Content-Type).

name
     Might be used to suggest a file name if the part is to be saved to a file (Content-Type).

disposition
     Valid values are inline and attachment (Content-Disposition).

encoding
     Valid values are 7bit, 8bit, quoted-printable and base64 (Content-Transfer-Encoding).

description
     A description of the part (Content-Description).

creation-date
     RFC822 date when the part was created (Content-Disposition).

modification-date
     RFC822 date when the part was modified (Content-Disposition).

read-date
     RFC822 date when the part was read (Content-Disposition).

size
     The size (in octets) of the part (Content-Disposition).

Parameters for application/octet-stream:

type
     Type of the part; informal---meant for human readers (Content-Type).

Parameters for message/external-body:

access-type
     A word indicating the supported access mechanism by which the file may be obtained.  Values include ftp, anon-ftp, tftp, localfile, and mailserver.  (Content-Type.)

expiration
     The RFC822 date after which the file may no longer be fetched. (Content-Type.)

size
     The size (in octets) of the file.  (Content-Type.)

permission
     Valid values are read and read-write (Content-Type).

Advanced MML Example
====================

Here's a complex multipart message.  It's a multipart/mixed that contains many parts, one of which is a multipart/alternative.

     <#multipart type=mixed>
     <#part type=image/jpeg filename=~/rms.jpg disposition=inline>
     <#multipart type=alternative>
     This is a plain text part.
     <#part type=text/enriched name=enriched.txt>
     <center>This is a centered enriched part</center>
     <#/multipart>
     This is a new plain text part.
     <#part disposition=attachment>
     This plain text part is an attachment.
     <#/multipart>

And this is the resulting MIME message:

     Content-Type: multipart/mixed; boundary="=-=-="

     --=-=-=

     --=-=-=
     Content-Type: image/jpeg;
      filename="~/rms.jpg"
     Content-Disposition: inline;
      filename="~/rms.jpg"
     Content-Transfer-Encoding: base64

     /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRof
     Hh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/wAALCAAwADABAREA/8QAHwAA
     AQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQR
     BRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RF
     RkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ip
     qrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/9oACAEB
     AAA/AO/rifFHjldNuGsrDa0qcSSHkA+gHrXKw+LtWLrMb+RgTyhbr+HSug07xNqV9fQtZrNI
     AyiaE/NuBPOOOP0rvRNE880KOC8TbXXGCv1FPqjrF4LDR7u5L7SkTFT/ALWOP1xXgTuXfc7E
     sx6nua6rwp4IvvEM8chCxWxOdzn7wz6V9AaB4S07w9p5itow0rDLSY5Pt9K43xO66P4xs71m
     2QXiGCbA4yOVJ9+1aYORkdK434lyNH4ahCnG66VT9Nj15JFbPdX0MS43M4VQf5/yr2vSpLnw
     5ZW8dlCZ8KFXjOPX0/mK6rSPEGt3Angu44fNEReHYNvIH3TzXDeKNO8RX+kSX2ouZkicTIOc
     L+g7E810ulFjpVtv3bwgB3HJyK5L4quY/C9sVxk3ij/xx6850u7t1mtp/wDlpEw3An3Jr3Dw
     34gsbWza4nBlhC5LDsaW6+IFgupQyCF3iHH7gA7c9R9ay7zx6t7aX9jHC4smhfBkGCvHGfrm
     tLQ7hbnRrV1GPkAP1x1/Hr+Ncr8Vzjwrbf8AX6v/AKA9eQRyYlQk8Yx9K6XTNbkgia2ciSIn
     7p5Ga9Atte0LTLKO6it4i7dVRFJDcZ4PvXN+JvEMF9bILVGXJLSZ4zkjivRPDaeX4b08HOTC
     pOffmua+KkbS+GLVUGT9tT/0B68eeIpIFYjB70+OOVXyoOM9+M1eaWeCLzHPyHGO/NVWvJJm
     jQ8KGH1NfQWhXSXmh2c8eArRLwO3HSv/2Q==

     --=-=-=
     Content-Type: multipart/alternative; boundary="==-=-="

     --==-=-=

     This is a plain text part.

     --==-=-=
     Content-Type: text/enriched;
      name="enriched.txt"

     <center>This is a centered enriched part</center>

     --==-=-=--

     --=-=-=

     This is a new plain text part.

     --=-=-=
     Content-Disposition: attachment

     This plain text part is an attachment.

     --=-=-=--

Standards
*********

The Emacs MIME library implements handling of various elements according to a (somewhat) large number of RFCs, drafts and standards documents.  This chapter lists the relevant ones.  They can all be fetched from http://www.stud.ifi.uio.no/~larsi/notes/.

RFC822
STD11
     Standard for the Format of ARPA Internet Text Messages.

RFC1036
     Standard for Interchange of USENET Messages

RFC2045
     Format of Internet Message Bodies

RFC2046
     Media Types

RFC2047
     Message Header Extensions for Non-ASCII Text

RFC2048
     Registration Procedures

RFC2049
     Conformance Criteria and Examples

RFC2231
     MIME Parameter Value and Encoded Word Extensions: Character Sets, Languages, and Continuations

RFC1843
     HZ - A Data Format for Exchanging Files of Arbitrarily Mixed Chinese and ASCII characters

draft-ietf-drums-msg-fmt-05.txt
     Draft for the successor of RFC822

RFC2112
     The MIME Multipart/Related Content-type

RFC1892
     The Multipart/Report Content Type for the Reporting of Mail System Administrative Messages

RFC2183
     Communicating Presentation Information in Internet Messages: The Content-Disposition Header Field