Chapter 5. Mutt's MIME Support

Quite a bit of effort has been made to make Mutt the premier text-mode MIME MUA. Every effort has been made to provide the functionality that the discerning MIME user requires, and the conformance to the standards wherever possible. When configuring Mutt for MIME, there are two extra types of configuration files which Mutt uses. One is the mime.types file, which contains the mapping of file extensions to IANA MIME types. The other is the mailcap file, which specifies the external commands to use for handling specific MIME types.

There are three areas/menus in Mutt which deal with MIME, they are the pager (while viewing a message), the attachment menu and the compose menu.

When you add an attachment to your mail message, Mutt searches your personal mime.types file at HOME/.mime.types, and then the system mime.types file at /usr/local/share/mutt/mime.types or /etc/mime.types

The mime.types file consist of lines containing a MIME type and a space separated list of extensions. For example:

application/postscript          ps eps
application/pgp                 pgp
audio/x-aiff                    aif aifc aiff

A sample mime.types file comes with the Mutt distribution, and should contain most of the MIME types you are likely to use.

If Mutt can not determine the mime type by the extension of the file you attach, it will look at the file. If the file is free of binary information, Mutt will assume that the file is plain text, and mark it as text/plain. If the file contains binary information, then Mutt will mark it as application/octet-stream. You can change the MIME type that Mutt assigns to an attachment by using the edit-type command from the compose menu (default: T). The MIME type is actually a major mime type followed by the sub-type, separated by a '/'. 6 major types: application, text, image, video, audio, and model have been approved after various internet discussions. Mutt recognizes all of these if the appropriate entry is found in the mime.types file. It also recognizes other major mime types, such as the chemical type that is widely used in the molecular modeling community to pass molecular data in various forms to various molecular viewers. Non-recognized mime types should only be used if the recipient of the message is likely to be expecting such attachments.

Mutt supports RFC 1524 MIME Configuration, in particular the Unix specific format specified in Appendix A of RFC 1524. This file format is commonly referred to as the mailcap format. Many MIME compliant programs utilize the mailcap format, allowing you to specify handling for all MIME types in one place for all programs. Programs known to use this format include Netscape, XMosaic, lynx and metamail.

In order to handle various MIME types that Mutt can not handle internally, Mutt parses a series of external configuration files to find an external handler. The default search string for these files is a colon delimited list containing the following files:

where HOME is your home directory. The PKGDATADIR and the SYSCONFDIR directories depend on where mutt is installed: the former is the default for shared data, the latter for system configuration files.

The default search path can be obtained by running the following command:

mutt -nF /dev/null -Q mailcap_path

In particular, the metamail distribution will install a mailcap file, usually as /usr/local/etc/mailcap, which contains some baseline entries.

A mailcap file consists of a series of lines which are comments, blank, or definitions.

A comment line consists of a character followed by anything you want.

A blank line is blank.

A definition line consists of a content type, a view command, and any number of optional fields. Each field of a definition line is divided by a semicolon ';' character.

The content type is specified in the MIME standard type/subtype method. For example, text/plain, text/html, image/gif, etc. In addition, the mailcap format includes two formats for wildcards, one using the special '*' subtype, the other is the implicit wild, where you only include the major type. For example, image/*, or video, will match all image types and video types, respectively.

The view command is a Unix command for viewing the type specified. There are two different types of commands supported. The default is to send the body of the MIME message to the command on stdin. You can change this behavior by using s as a parameter to your view command. This will cause Mutt to save the body of the MIME message to a temporary file, and then call the view command with the s replaced by the name of the temporary file. In both cases, Mutt will turn over the terminal to the view program until the program quits, at which time Mutt will remove the temporary file if it exists.

So, in the simplest form, you can send a text/plain message to the external pager more on stdin:

text/plain; more

Or, you could send the message as a file:

text/plain; more %s

Perhaps you would like to use lynx to interactively view a text/html message:

text/html; lynx %s

In this case, lynx does not support viewing a file from stdin, so you must use the s syntax. Note: Some older versions of lynx contain a bug where they will check the mailcap file for a viewer for text/html. They will find the line which calls lynx, and run it. This causes lynx to continuously spawn itself to view the object.

On the other hand, maybe you don't want to use lynx interactively, you just want to have it convert the text/html to text/plain, then you can use:

text/html; lynx -dump %s | more

Perhaps you wish to use lynx to view text/html files, and a pager on all other text formats, then you would use the following:

text/html; lynx %s
text/*; more

This is the simplest form of a mailcap file.

The interpretation of shell meta-characters embedded in MIME parameters can lead to security problems in general. Mutt tries to quote parameters in expansion of s syntaxes properly, and avoids risky characters by substituting them, see the mailcapsanitize variable.

Although mutt's procedures to invoke programs with mailcap seem to be safe, there are other applications parsing mailcap, maybe taking less care of it. Therefore you should pay attention to the following rules:

Keep the -expandos away from shell quoting. Don't quote them with single or double quotes. Mutt does this for you, the right way, as should any other program which interprets mailcap. Don't put them into backtick expansions. Be highly careful with eval statements, and avoid them if possible at all. Trying to fix broken behavior with quotes introduces new leaks - there is no alternative to correct quoting in the first place.

If you have to use the -expandos' values in context where you need quoting or backtick expansions, put that value into a shell variable and reference the shell variable where necessary, as in the following example (using charset inside the backtick expansion is safe, since it is not itself subject to any further expansion):

text/test-mailcap-bug; cat %s; copiousoutput; test=charset=%{charset} 
        && test "`echo $charset | tr '[A-Z]' '[a-z]'`" != iso-8859-1

In addition to the required content-type and view command fields, you can add semi-colon ';' separated fields to set flags and other options. Mutt recognizes the following optional fields:

copiousoutput

This flag tells Mutt that the command passes possibly large amounts of text on stdout. This causes Mutt to invoke a pager (either the internal pager or the external pager defined by the pager variable) on the output of the view command. Without this flag, Mutt assumes that the command is interactive. One could use this to replace the pipe to more in the lynx -dump example in the Basic section:

text/html; lynx -dump %s ; copiousoutput

This will cause lynx to format the text/html output as text/plain and Mutt will use your standard pager to display the results.

needsterminal

Mutt uses this flag when viewing attachments with autoview, in order to decide whether it should honor the setting of the waitkey variable or not. When an attachment is viewed using an interactive program, and the corresponding mailcap entry has a needsterminal flag, Mutt will use waitkey and the exit status of the program to decide if it will ask you to press a key after the external program has exited. In all other situations it will not prompt you for a key.

compose=<command>

This flag specifies the command to use to create a new attachment of a specific MIME type. Mutt supports this from the compose menu.

composetyped=<command>

This flag specifies the command to use to create a new attachment of a specific MIME type. This command differs from the compose command in that mutt will expect standard MIME headers on the data. This can be used to specify parameters, filename, description, etc. for a new attachment. Mutt supports this from the compose menu.

print=<command>

This flag specifies the command to use to print a specific MIME type. Mutt supports this from the attachment and compose menus.

edit=<command>

This flag specifies the command to use to edit a specific MIME type. Mutt supports this from the compose menu, and also uses it to compose new attachments. Mutt will default to the defined editor for text attachments.

nametemplate=<template>

This field specifies the format for the file denoted by s in the command fields. Certain programs will require a certain file extension, for instance, to correctly view a file. For instance, lynx will only interpret a file as text/html if the file ends in .html. So, you would specify lynx as a text/html viewer with a line in the mailcap file like:

text/html; lynx %s; nametemplate=%s.html

test=<command>

This field specifies a command to run to test whether this mailcap entry should be used. The command is defined with the command expansion rules defined in the next section. If the command returns 0, then the test passed, and Mutt uses this entry. If the command returns non-zero, then the test failed, and Mutt continues searching for the right entry. Note: the content-type must match before Mutt performs the test. For example:

text/html; netscape -remote 'openURL(%s)' ; test=RunningX
text/html; lynx %s

In this example, Mutt will run the program RunningX which will return 0 if the X Window manager is running, and non-zero if it isn't. If RunningX returns 0, then Mutt will call netscape to display the text/html object. If RunningX doesn't return 0, then Mutt will go on to the next entry and use lynx to display the text/html object.

When searching for an entry in the mailcap file, Mutt will search for the most useful entry for its purpose. For instance, if you are attempting to print an image/gif, and you have the following entries in your mailcap file, Mutt will search for an entry with the print command:

image/*;        xv %s
image/gif;      ; print= anytopnm %s | pnmtops | lpr; 
                nametemplate=%s.gif

Mutt will skip the image/* entry and use the image/gif entry with the print command.

In addition, you can use this with autoview to denote two commands for viewing an attachment, one to be viewed automatically, the other to be viewed interactively from the attachment menu. In addition, you can then use the test feature to determine which viewer to use interactively depending on your environment.

text/html;      netscape -remote 'openURL(%s)' ; test=RunningX
text/html;      lynx %s; nametemplate=%s.html
text/html;      lynx -dump %s; nametemplate=%s.html; copiousoutput

For autoview, Mutt will choose the third entry because of the copiousoutput tag. For interactive viewing, Mutt will run the program RunningX to determine if it should use the first entry. If the program returns non-zero, Mutt will use the second entry for interactive viewing.

If you ever lose track of attachments in your mailboxes, Mutt's attachment-counting and -searching support might be for you. You can make your message index display the number of qualifying attachments in each message, or search for messages by attachment count. You also can configure what kinds of attachments qualify for this feature with the attachments and unattachments commands.

In order to provide this information, mutt needs to fully MIME-parse all messages affected first. This can slow down operation especially for remote mail folders such as IMAP because all messages have to be downloaded first regardless whether the user really wants to view them or not.

The syntax is:

attachments   {+|-}disposition mime-type
unattachments {+|-}disposition mime-type
attachments   ?

Disposition is the attachment's Content-disposition type -- either "inline" or "attachment". You can abbreviate this to I or A.

Disposition is prefixed by either a + symbolor a - symbol. If it's a +, you're saying that you want to allow this disposition and MIME type to qualify. If it's a -, you're saying that this disposition and MIME type is an exception to previous + rules. There are examples below of how this is useful.

Mime-type is, unsurprisingly, the MIME type of the attachment you want to affect. A MIME type is always of the format "major/minor", where "major" describes the broad category of document you're looking at, and "minor" describes the specific type within that category. The major part of mim-type must be literal text (or the special token "*"), but the minor part may be a regular expression. (Therefore, "*/.*" matches any MIME type.)

The MIME types you give to the attachments directive are a kind of pattern. When you use the attachments directive, the patterns you specify are added to a list. When you use unattachments, the pattern is removed from the list. The patterns are not expanded and matched to specific MIME types at this time -- they're just text in a list. They're only matched when actually evaluating a message.

Some examples might help to illustrate. The examples that are not commented out define the default configuration of the lists.

## Removing a pattern from a list removes that pattern literally. It
## does not remove any type matching the pattern.
##
##  attachments   +A */.*
##  attachments   +A image/jpeg
##  unattachments +A */.*
##
## This leaves "attached" image/jpeg files on the allowed attachments
## list. It does not remove all items, as you might expect, because the
## second */.* is not a matching expression at this time.
##
## Remember: "unattachments" only undoes what "attachments" has done!
## It does not trigger any matching on actual messages.


## Qualify any MIME part with an "attachment" disposition, EXCEPT for
## text/x-vcard and application/pgp parts. (PGP parts are already known
## to mutt, and can be searched for with ~g, ~G, and ~k.)
##
## I've added x-pkcs7 to this, since it functions (for S/MIME)
## analogously to PGP signature attachments. S/MIME isn't supported
## in a stock mutt build, but we can still treat it specially here.
##
attachments   +A */.*
attachments   -A text/x-vcard application/pgp.*
attachments   -A application/x-pkcs7-.*

## Discount all MIME parts with an "inline" disposition, unless they're
## text/plain. (Why inline a text/plain part unless it's external to the
## message flow?)
##
attachments   +I text/plain

## These two lines make Mutt qualify MIME containers.  (So, for example,
## a message/rfc822 forward will count as an attachment.)  The first
## line is unnecessary if you already have "attach-allow */.*", of
## course.  These are off by default!  The MIME elements contained
## within a message/* or multipart/* are still examined, even if the
## containers themseves don't qualify.
##
#attachments  +A message/.* multipart/.*
#attachments  +I message/.* multipart/.*

## You probably don't really care to know about deleted attachments.
attachments   -A message/external-body
attachments   -I message/external-body

"attachments ?" will list your current settings in Muttrc format, so that it can be pasted elsewhere.