[neomutt-users] Selectively pipe emails to w3m

Eric Blake eblake at redhat.com
Fri Jun 25 23:37:05 CEST 2021


On Fri, Jun 25, 2021 at 04:23:12PM +0000, Kenneth Flak wrote:
> Hi list,
> 
> Is there a way to selectively pipe emails to w3m or display the plain text version, based on a regexp pattern? I subscribe to the mailing list version of a discord forum, which sends mails in both plain text and html, and I would like to get the plain text version.

Absolutely!  Here's how I do it.  You're right that you need .mailcap
settings with a copiousoutput entry for text/html and any other format
you want neomutt to display inline; I have:

text/x-patch; cat; copiousoutput;
text/x-log; cat; copiousoutput;
text/x-diff; cat; copiousoutput;
text/html; firefox '%s' &; test=test -n "$DISPLAY"; needsterminal;
text/html; w3m -I %{charset} -O UTF-8 -T %t -s -o display_link_number=1 \
 -dump; copiousoutput;
application/postscript; ps2ascii %s; copiousoutput
application/vnd.openxmlformats-officedocument.wordprocessingml.document; pandoc --from docx --to markdown %s; copiousoutput
application/vnd.oasis.opendocument.text; pandoc --from odt --to markdown %s; copiousoutput
application/pdf; pdftotext -layout %s -; copiousoutput;

(If you google, you can even find cases where people have utilized
image-to-ascii-art converters to view renderings of .jpg inline - but
that's going a bit beyond your question)

Then in my neomuttrc, I have rules that control which view I want to
see for a given message.  As written here, I default to reading the
text/plain (since I'm subscribed to more mailing lists where the plain
text is usually okay) and then bad-list the outliers, but you could
instead default to text/html and good-list the mailing lists.

message-hook . 'unalternative_order *; \
  alternative_order text/plain text text/enriched text/html'
message-hook '%f bad-multipart' \
  'unalternative_order *; alternative_order text/html multipart/related'
mime_lookup application/octet-stream
set mailcap_path = $my_mutt/mailcap
auto_view text/html text/x-patch text/x-log text/x-diff application/postscript\
 application/vnd.openxmlformats-officedocument.wordprocessingml.document\
 application/pdf

The key things to note: alternative_order is what controls which style
(text/plain or text/html) to prefer for a given email regardless of
the original order the sender used, auto_view is necessary to get
neomutt to open that portion of the messaege in your pager (assuming
it found a matching copiousoutput entry in mailcap), mime_lookup
catches a few more cases where you want auto_view to kick in, and
using message-hook then controls on a per-message basis which setting
of alternative_order I prefer (the '%f bad-multipart' hook lets me
utilize the group feature instead of having to write a lot of
duplicate hooks).

Then I have a BUNCH of lines like:

group -group bad-multipart -rx @.*cigna.com

to explicitly list the senders where I know that their text/plain
portion is broken.  And given that this is neomutt you could instead
have a separate file that contains a list of one regex per line (in my
case, I also allow # comments), and then in your neomuttrc do:

group -group bad-multipart `sed -n < path/to/sender_list '/#/d;H;${x;s/\n/ -rx /gp}'`

where the sed script appends every non-comment line with a preceding
newline into hold space, then on the last line of input, exchanges the
hold space back into pattern space, and converts those newlines into
'-rx' tokens, to give the requisite one-line output that neomutt then
executes as the desired:

group -group bad-multipart -rx line1 -rx line2 ...

And once you do that, a next cool hack is to write a macro that any
time you visit an email from an unknown sender, you type the macro
key(s) to trigger an automatic addition of that sender's email into
the current group definition as well as modifying your sender_list
file, so that future visits to mail from that sender will use the
preferred format.

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org



More information about the neomutt-users mailing list