+
AMDG
Goodman Coat of Arms

Goodman's Oak

Miscellaneous Thoughts, Projects, and Ruminations on Life, the Universe, and Everything

Mutt and Friends: Tools in the Email Toolchain

Donald P. Goodman III 26 Dec 1200 (30 Dec 2016)

You know what mutt is, don't you? If not, you should.

Mutt is a text-interface email client which strings together with all your favorite Unix tools (whatever those might be) to create exactly the email toolchain that will be most productive for you. It does one job and does it well, rather than many jobs poorly.

Now, because it does that one thing and does it well, mutt doesn’t do other things that are useful to have alongside an email program. E.g., when someone sends me an email containing a URL, I don’t want to have to type it manually into my address bar; I want to be able to follow it from my message. But mutt’s not a mail parser; it’s a mail user client. So I need to string it with another tool to do this.

This article shows my favorite tools which are strung together with mutt. Note that it does not include mail transfer clients and the like, because that’s been done better elsewhere; these are simply helper tools, not tools that are necessary to do email at all.

That’s mutt; all these different tools, each designed to do one job and do it well, strung together so that they can each be used effortlessly. Beautiful!

1.) urlview This little tool just processes the text you give it for URLs, then returns any URLs it finds to you; you can then pick the one(s) you want and hit “enter,” whereupon said URLs will be passed to your favorite browser for opening.

All you have to do is tell mutt that when you hit a certain key combination, you want it to send the email you’ve got open to urlview for processing; you can do that with a line like the following in your .muttrc file:

macro pager \cb |urlview\n

Note that many mail clients, including many webmail clients, fantastically mangle their text, such that URLs are improperly broken across lines, making it difficult for urlview to get the whole thing. We should campaign against such foolishness in mail clients; URLs should not contain newlines.

To prevent it causing us problems, however, create a .urlview file in your home directory which contains the following lines:

WRAP yes

This will prevent poorly formatted emails from wrecking your lovely system.

2. w3m We’re constantly getting emails that are bloated with totally needless HTML. At least, I am. I’ve never yet received one where that HTML served any useful purpose; I suppose that the bulk of it is boilerplate added by the email client or webmail client the sender is using, and very often it’s simply advertising. Very troublesome. Fortunately, there are text-only browsers which are useful for dealing with such nonsense; w3m is one such.

Simply inform mutt that, when it opens an email which identifies itself as “text/html”, it should send that email through w3m before it shows it to you; w3m will then dump it to plain text for you to read without all the gobbledygook. You do this with a file in your home directory called “.mailcap”; simply include in it the following lines:

text/html; w3m %s; nametemplate=%s.html
text/html; w3m -dump %s; nametemplate=%s.html; copiousoutput

Then you have to tell mutt to consult .mailcap before sending you your messages; you do this by adding the following to your .muttrc:

auto_view text/html
alternative_order text/plain text/enriched text

You’ll now get your emails filtered from all their HTML bloat without having to think about it at all. The only downside to this method is that links are stripped out; if you think there’s a link you’re missing, though, hit “v” in the normal way to view attachments, then open the attachment in the message listed as type “text/html” with your favorite browser; this should provide you with the HTML formatted complete with its links.

3. abook mutt only has rudimentary address book capabilities, basically just enough to get it by. That’s as it should be; it’s a mail user client, not an address book program. So get an address book program! I recommend abook; it’s simple, extremely lightweight, and yet quite powerful. It also integrates very well with mutt.

So install it (all of these programs must be installed before use, obviously). You’ll want to set it up to work with mutt, of course, which you can do with a few lines in your .muttrc:

set query_command= "abook --mutt-query '%s'"
macro index,pager A "abook --add-email-quiet" "add the sender address to abook"

The second line means you can stop pretty much anywhere and hit “A” (capital “A”), and it will add the sender’s address to your abook address book. The first line means that “queries” will be run through abook first. Let me ‘splain.

When you start your mutt email, you hit “m.” (Or “r,” to reply; or “g,” to reply to all; or “f,” to forward; but this applies to all.) It then pops up a line at the bottom of your screen asking to whom you want to send the email. (Ha! Too verbose; it just gives you the part of the header you’re filling in, “To:”.) You can type it in directly; you can type in an alias from your .muttrc. But these are difficult, as either way you have to remember something, whether it’s an alias or an email address. (Of course, replying or replying to all already have some addresses in there for you; you can add addresses to these lists with this method, too.) What you want to do is pick people’s names off a list and have the computer put in the addresses, right?

So when that “To:” line pops up, hit “CTRL-t.” You will then be brought to a list of names, with email addresses, sorted however you tell abook to sort them. Select the ones you want to send to by hitting “t” when their names are highlighted; when you’re done, hit “enter.” It’ll then ask for a subject line, then take you to your favorite text editor so you can write your email.

It’s that simple. Toolchaining is a beautiful thing.

(Note that you can make abook and mutt even more powerful in combination if you want, letting abook take over email list management, as well: Using Mutt and Abook with Email Groups.)

4. muttprint Even in this fascinating and modern world, sometimes you want to print your mails. Well, problem solved! Install LaTeX (you’ve got that installed already, don’t you?), install muttprint, and you’re ready to go. Add the following to your .muttrc:

set print_command="muttprint"
set print="yes"
set print_split
macro index,pager \cp "<display-toggle-weed> <print-message> <display-toggle-weed> <exit>"

This magical mojo makes it so that when you hit “CTRL-p,” your email will be formatted beautifully, by LaTeX, and sent to the printer.

muttprint’s output can be customized a lot, and it’s beyond the scope of this article to detail that. But explore and figure it out! You’ll love it!

5. mairix mutt is a mail user client; it’s not a text processing engine. It’s got full regex capabilities, of course, which enables searching emails in a pretty fine-grained way; but that’s limited to individual mailboxes. Because one of mutt’s many strengths is its ability to automatically manage your mail in its many mailboxes, some pan-mailbox solution is needed. That’s what mairix is for.

Mairix is a full-featured text indexing and searching program, designed specifically for emails. Its index is shockingly tiny, and its searches are shockingly quick; it requires no external libraries or other assistance. Just install mairix, and you’re ready to roll.

I prefer to keep my mairix stuff in my mail directory; on the other hand, mairix indexes everything in my mail directory, and mairix rightly refuses to deposit search results or its index in a file that it’s actually indexed. (Doing that would be a Bad Idea; it is good that mairix refuses to.) So here’s the step-by-step of how to set up mairix on your system (assuming you’re using mbox mailboxes; if you’re using maildir, there are other sites which will help you more than this will):

5a.) Make a directory in your mail directory. Call it whatever you like; I call it “mfolder.”

5b.) Within your mfolder directory, create the following files (they don’t need to contain anything; just use “touch”): .mairixdb (this is where the index will go), and .mh_sequences (this is where your search results will go). Make sure that their permissions are restricted enough for your privacy needs.

5c.) Create a .mairixrc file in your home directory. Fear not; this file is pretty simple. Fill it with the following contents (change what must be changed, of course, depending on how your email is set up):

base=~/mail/
mfolder=~/mail/mfolder/.mh_sequences
mformat=mbox
mbox=~/mail/*
database=~/mail/mfolder/.mairixdb

And that’s it; it’s mostly self-explanatory. Now, you need to set up mutt to work with mairix. There are many different ways to do this; here’s how I chose to (the following in, you guess it, the .muttrc file):

macro index <F7> "mairix\n" "update mairix index"
macro index <F8> "mairix " "search via mairix"
macro index <F9> "mutt -f ~/mail/mfolder/.mh_sequences\n" "view mairix results"

Each of these should be one line; the total number of lines should be three. This is mostly self-explanatory. Hit F7 to build or update the mairix index; this is surprisingly quick, but if you’ve got a bunch of emails, be prepared to wait a little bit. (Updates will be very quick, if you do them regularly.) Hit F8 to search mairix; hit enter when you’re done entering your search terms. Then, hit F9 to view your search results. This will open those results as a new mailbox; go through them, do what you want, and when you’re done, just hit “q” as though quitting mutt, and you will find yourself back where you were when you first hit F9. Easy-peasy, quick, powerful. And no need for behemoth search programs!

mairix is a lot more full-featured than this; for example, it works even on your archived gzipped or bzip2ed mailboxes. When you do your search, if you want to add your results to those of a previous search, hit “-a” before entering your search terms. Hit “-t” to return not only the matched messages, but also their entire threads. If you want to send your search results to a different file, hit “-o” and then name the file, with path if necessary. You also get pretty fine-grained control over what mairix will try to match; you can specify that you only want messages with a certain keyword in the subject line, for example, not those where it only appears in the body. If you’re searching for a message in which someone sent you an email address, but you don’t want those messages you’ve received from that email address, you can specify that you only want those messages that matched the search terms in the message body, not in the header (e.g., not in the From:) line. You can request messages which are within a certain date range. The syntax is quite powerful; once you’ve installed mairix, you can read all about it in the man page.

So once again we’ve experienced the glories of stringing tools together into a single, harmonious whole, each part doing its own part well.

Praise be to Christ the King!