Better Attachment Handling with Mutt

The Mutt email client is famed for its extensive configuration options, but since it’s text-based, certain things are more challenging to do when compared to its graphical brethren. Viewing attachments is one such annoyance; fortunately, as with most things, Mutt is extensively configurable!

By default, Mutt does fine with most plain text documents, and depending on your installation may also handle HTML documents in some fashion. Attachments that Mutt doesn’t recognize can of course be downloaded and viewed manually, but we can do better.

To tell Mutt that it should handle a new attachment type, or its “MIME type”, we associate it with Mutt’s “auto_view” parameter. For example, add this to your ~/.muttrc (and restart Mutt):

 auto_view application/zip
 auto_view text/x-patch
 auto_view text/x-diff
 auto_view application/pgp-signature
 auto_view application/pgp
 auto_view text/html
 auto_view text/calendar

Note: if you plan to add a number of file types, you may wish to put these in their own config file (e.g. ~/.mutt/auto-views), and include a line in ~/.muttrc like the following:

source $HOME/.mutt/auto-views

A typical Linux system will include a pretty broad range of MIME type definitions (see /etc/mime.types). If you need to augment that list, or override the default definitions, you can add custom types to a ~/.mime.types file, such as:

 application/pgp pgp
 application/pgp-signature pgp gpg sig
 text/x-diff diff
 application/x-gzip gz

To specify how Mutt should display these file types (and other text interface programs), add the following to ~/.mailcap:

text/x-diff; cat ; copiousoutput
 text/x-patch; cat ; copiousoutput
 application/x-gzip; zcat ; copiousoutput
 application/pgp; cat; copiousoutput
 application/pgp-keys; pgp -f < %s ; copiousoutput
 application/pgp-signature; cat ; copiousoutput
 text/calendar; icalview.py; copiousoutput

The ‘copiousoutput’ flag should be added for all commands that are likely to produce more than a handful of lines and that are display-only, requiring no interaction with the user. This will trigger the use of your desired pagination program (e.g. ‘more’) for scrolling through the output.

For mimetypes that are already a plain text format, requiring no further processing, the ‘cat’ command is sufficient. Some mimetypes are directly convertible into text, via commands like zcat that can accept a filename argument. For commands that only accept input through their STDIN, you can construct a shell command with the filename indicated by %s, for instance “pgp -f < %s”.

In some cases, the mimetype is not directly convertible to plain text, and you’ll need a more sophisticated command line sequence or script. For example, while text/calendar files are plain text, the raw data is rather dense and not always expressed in the time zone we care about, however it is easy enough to translate with a short python script:

 #!/usr/bin/env python
 import sys

 import ics # pip install ics
 import arrow # pulled in with ics, else pip install arrow

 cal = ics.Calendar(sys.stdin.read().decode('utf-8'))

 tz = arrow.now().tzinfo
 tzformat = 'ddd DD MMM HH:mm'

 for event in cal.events:
     extra = {}
     for item in event._unused:
         if not isinstance(item, ics.parse.ContentLine):
             continue
         try:
             extra[item.name.lower()] = item.value
         except:
             print item
             raise
     print "Event: ", event.name
     print "Status: ", extra['status'].title()
     print "Organiser:", extra['organizer']
     print "Starts: ", event.begin.to(tz).format(tzformat), "(local time)"
     print "Ends: ", event.end.to(tz).format(tzformat)
     print "Location: ", event.location
     print "Description:"
     print (event.description or u'').encode('utf-8')
     print

Save this to a file named icalview.py and change it to be executable:

$ chmod a+x icalview.py

Now, when viewing a calendar invitation you will see a nicely printed summary of the event with dates and times converted to your local timezone. These tips should help you get more out of Mutt, if you have any questions or tips of your own, feel free to post them in the comments below!

Author: Bryce Harrington

Bryce is a founder and developer of the Inkscape project, but began his career in the aerospace industry as a spacecraft propulsions engineer.

2 thoughts on “Better Attachment Handling with Mutt”

  1. Hi Bryce, for patch and diff files, try this in your .mailcap :

    text/x-diff; highlight -Sdiff -Oansi %s; copiousoutput

Comments are closed.