Editing ~/.mailcap

Lesson 1 - PDF files

To use these scripts with your favorite mail client, web browser, etc. you must edit your ~/.mailcap file. The ~/.mailcap file contains lines that describe how to handle different content-types with helper applications. For example, from my ~/.mailcap file come these lines:

application/pdf;gv '%s'; test=test -n "$DISPLAY"
application/pdf;pdfcat '%s'; copiousoutput

The first line says that to view a document of type application/pdf (a PDF attachment) you should use the gv program. The %s is a place-marker that contains the name of the pdf-file to view (generally this is some kind of temporary file). The last part of the line gives a test to indicate when this rule applies: if the DISPLAY variable is non-empty (i.e. when working under X11). In plain English, this line means that in graphical environments, gv will open PDF files.

If the DISPLAY variable happens to be empty, the first rule fails, and the second rule is tried. The second rule indicates that the file should be processed by pdfcat. The final term on the line, copiousoutput, indicates that the output of pdfcat may be a large amount of text and may need to be "paged". For example, when reading a PDF in mutt, this tells mutt to use its built-in pager to view the text.

Lesson 2 - HTML files

The rules for html attachments that I use are:

text/html; mozilla '%s'; edit=gvim -f '%s'; test=test -n "$DISPLAY"
text/html; w3m -T text/html '%s' ; needsterminal
text/html; w3m -T text/html -dump '%s' ; copiousoutput

According to the first line, HTML attachments will be displayed using Mozilla when working in graphical environments. Failing that, the second line invokes the text web browser w3m to view the attachment. Notice the term needsterminal. This indicates that the program requires a console window, xterm, or similar to function (this is typically the case that applies when working over the phone).

The third rule has a special relationship to mutt. The -dump mode of w3m works like a filter to dump the text of a web page to the console (as opposed to browsing it interactively). In this mode, copiousoutput is appropriate. But more importantly, in your ~/.muttrc you can add a line to automatically view HTML attachments. This is particularly convenient for interacting with those who send HTML emails. When auto-viewing, mutt will always choose a copiousoutput rule, even when it is placed later in the ~/.mailcap file. To enable this, add to your ~/.muttrc:

auto_view text/html

As always in mutt, you have the option of pressing v to view the attachments and selecting an HTML attachment for interactive viewing (according to the first or second rule as appropriate). The effect is that you get automatic text rendering of the HTML in your mail (which can be quoted when replying, etc.) and the ability to have a more interactive or graphical rendering if you desire.

If you wish, you can set any mime-types to be auto-viewed. For example, if you turn it on for application/pdf, then the rule above will ensure that the text of PDF attachments is automatically extracted in the mails you read (and you can choose to view them specifically by pressing v and selecting the appropriate attachment).

Finishing up

To round out the ~/.mailcap file, I include a few Microsoft Word rules, and a few generic rules:

application/msword; abiword '%s'; test=test -n "$DISPLAY"
application/msword; wordcat '%s'; copiousoutput

text/*; gview -Mf '%s' ; edit=gvim -f '%s'; compose=gvim -f '%s'; test=test -n "$DISPLAY"
text/*; view -Mf '%s' ; edit=vim -f '%s'; compose=vim -f '%s'; needsterminal

The first two should require no further comment. The last are generic rules that match any text mime-type that doesn't match a more specific rule. The first line specifies that in graphical environments gview/gvim (a graphical Vi program) is used to view/edit/compose as appropriate. Otherwise, text versions of Vi are used.