Mutt mailer: Show Messages With Attachments (Shallow Thoughts)

Akkana's Musings on Open Source Computing and Technology, Science, and Nature.

Sat, 22 May 2021

Mutt mailer: Show Messages With Attachments

A discussion on the Mutt-Users list a while back involved someone trying to configure mutt to show on the index screen which messages had attachments.

I had no idea that was possible! But it's something I've wanted for ages. Normally, mutt shows a list of attachments after the end of the email message. That was useful back in the day when people trimmed their email messages; but now, when most people append the contents of an entire message thread going back several weeks, scrolling down to the end of an email message is barely even possible.

What I'd really like is to see in the message view whether the message has attachments -- up at the top of the message, along with the headers. But showing it in the folder index would be a great start.

What Constitutes an Attachment?

First you have to define what attachments you care about. Most normal email messages have attachments just for the text. At minimum, there's a text/plain attachment for a text message; but these days, most people are sending two copies of every message, one as HTML and one as text, wrapped in a multipart/alternative package. For instance, here's what mutt's attachments view (what you get if you type v) shows me on a recent message from a friend, which contained just text, no formatting or attached documents or anything else:

  I     1                      [multipa/alternativ, 7bit, 9.8K]
  I     2 ├─>                 [text/plain, quoted, utf-8, 3.8K]
  I     3 └─>                  [text/html, quoted, utf-8, 5.8K]

There's a text message that's 3.8 kilobytes, plus an alternative HTML copy of the same text that's 5.8 kilobytes. The MIME type "ultipart/alternative" means that the messages contain the same text, and it will show one or the other but not both.

I don't want messages like that to show up as having attachments; I just want the messages where I have to do something special to view an attached image, or a PDF or (ugh) a MS Word document.

Fortunately, Mutt lets you configure that. I decided to allow anything that wasn't text/plain or text/html. So I enabled all formats, and then excluded text formats:

attachments +A */.*
attachments -A text/.*

This didn't work right off; every message showed up as having attachments. It turned out I also had to disallow inline attachments:

attachments -I */.*

Except then it didn't see attached images. So I removed the previous line and disallowed inline text attachments, but allowed image ones:

attachments -I text/.*
attachments +I image/.*

And I want to know about attachments even if they're attached inside an HTML message:

set count_alternatives=yes

Show Number of Attachments in the Index

The folder index -- the page that shows all the messages in a folder, one line per message -- is defined by the variable index_format. With a little experimentation with the examples from that email thread, I found that %?X?%X& ? gave me what I wanted: the number of attachments if it was greater than one, otherwise a blank space. Adding that to the index_format I already had gave me:

set index_format="%4C %Z  %?X?%X& ?  %{%b %d} %-15.15F %s"

Colorize Those Index Lines

That was neat. But realistically, I knew I wasn't going to notice that subtle number in the sea of message lines. What I really wanted was color. I already use colors to highlight messages that are new, or flagged, or deleted. Was there a way to add a color for messages with attachments?

There was! Mutt has a list of things it calls Pattern Modifiers including things like ~N to indicate a New message, ~F for Flagged messages, etc. And the list includes ~X [MIN]-[MAX] for messages with MIN to MAX attachments. So the pattern "~X 1-" should work. I added that to my muttcolors file:

color index        red           white    "~X 1-"

And it worked! Awesome.

The only problem: it conflicted with my other flags. For instance, a message that was both flagged and had attachments could show only one of those attributes. So if I gave it the red-on-white color for attachments, I'd lose the blue-on-default I used for new messages. Could I combine them?

Alas, the mutt manual is silent on that. There's a discussion of Complex patterns, which discusses how to do pattern1 OR pattern2, but there's no mention of how to specify something that has pattern1 AND pattern2, a message that's new AND has attachments.

An obvious thing to try was color index brightblue white "~N ~X 1-" -- but that gave a syntax error, as did ~N ~X 1- and "~N" "~X 1-".

But along the line, experimenting, I made a typo and accidentally discovered a syntax that works:

color index brightblue white "~N ~X 1-

That's right: a single, mismatched double-quote at the beginning of the string, with no matching close quote, is the only syntax I found that worked. Go figure!

Tags: , , ,
[ 10:46 May 22, 2021    More linux | permalink to this entry | ]

Comments via Disqus:

blog comments powered by Disqus