Mutt

From WTFwiki
Jump to navigation Jump to search

Render HTML email when there's no text/plain alternative

If there's no text/plain version of an email component, mutt defaults to just rendering the html, regardless of the mailcap file.

To make it do something more useful:

.mailcap

text/html; lynx -force_html -dump %s ; copiousoutput

.muttrc

auto_view text/html
alternative_order text/plain

One downside of this, is that lynx no longer can run interactively (you can't follow links from a html email). Instead lynx will print out a references section at the bottom with all the embedded links.

More info can be found at http://www.mutt.org/doc/manual/manual-5.html

Open HTML mails in a browser on another machine

Note: a more general approach is described in the next section

1. On your mutt machine:

1.1. Generate passwordless ssh key (don't worry we'll limit the access):

ssh-keygen -f ~/.ssh/html_mail_key

1.2. Add to .muttrc:

macro pager o "v/html<enter>|ssh -i ~/.ssh/html_mail_key MACHINE<enter>q" \
        "View HTML mail in Firefox on MACHINE"

where MACHINE is your browser machine name. I've chosen o as the keybinding here.

2. On your browser machine:

2.1. Add to $HOME/.ssh/authorized_keys:

command="/home/yourname/bin/open_html_mail.sh" <key>

Where <key> is the stuff from ~/.ssh/html_mail_key.pub on the mutt machine.

2.2. Create $HOME/bin/open_html_mail.sh:

#!/bin/sh

cat >/tmp/last_html_mail.html
DISPLAY=:0 firefox /tmp/last_html_mail.html

2.3. chmod +x $HOME/bin/open_html_mail.sh

That's it. Add also "unset wait_key" to your .muttrc if the extra keypress annoys you.

Alternative solutions for those behind NAT

So you think the above is a great idea, but you're behind NAT? Or you're switching machines too often? Here's a solution, changes are only minor:

  1. Use "ssh -R2022:localhost:22 ..." to log into the mutt machine - change port numbers if needed.
  2. The mutt macro changes to this
macro pager o "v/html<enter>|ssh -o \"NoHostAuthenticationForLocalhost yes\" -i ~/.ssh/html_mail_key -p2022 localhost<enter>q" \
       "View HTML mail in Firefox"

Open attachments on remote machine

This is very similar to the recipe above, but for attachments of arbitrary types. The goal is to have them open on the remote machine in much the same way as if you'd double-clicked them in your desktop's default file browser.

1. On your mutt machine.

1.1. Generate passwordless ssh key (don't worry we'll limit the access):

ssh-keygen -f ~/.ssh/mail_attachment_key

1.2. Add to .muttrc:

macro attach A "<save-entry><bol>/home/yourname/attachments/<eol>_ma<enter><shell-escape>/home/yourname/bin/open_attachment_remotely.sh<enter>" \
       "Open attachment remotely"

1.3. open_attachment_remotely.sh:

#!/bin/sh

cd ~/attachments
latest="$(ls -rt|tail -1)"
uniq_latest="$(date +%s)_${latest%_ma}"
mv "$latest" "$uniq_latest"
latest="$uniq_latest"
fixname=$(trydecode "$latest")
if [ $? -eq 0 ]
then
        mv "$latest" "$fixname"
        latest="$fixname"
fi

tar cf - "$latest" | ssh -i ~/.ssh/mail_attachment_key desktop_host
rm "$latest"

1.4. (Optional) trydecode:

#!/usr/bin/perl

use Encode qw(decode encode);

$input = $ARGV[0];
$decoded = decode("MIME-Header", $input);

if ($decoded eq $input) {
        $abbrev = $input . "?=";
        $decoded = decode("MIME-Header", $abbrev);

        if ($decoded eq $abbrev) {
                print "Can't decode\n";
                exit 1;
        }
}

$s = encode("utf-8", $decoded) or die;
print "$s\n";

1.5. chmod +x bin/trydecode bin/open_attachment_remotely.sh; mkdir attachments

1.6. Restart mutt

2. On your desktop machine:

2.1. Add to $HOME/.ssh/authorized_keys:

command="/home/yourname/bin/open_mail_attachment.sh" <key>

2.2. open_mail_attachment.sh:

#!/bin/sh

cd $HOME/.last_attachment.d/
OPEN="$(tar xv)"

logger "[open_mail_attachment.sh] Opening $OPEN"

DISPLAY=:0 nohup xdg-open "$OPEN" </dev/null >/dev/null 2>&1 &

2.3. chmod +x bin/open_mail_attachment.sh; mkdir .last_attachment.d

You may want to unset wait_key in your .muttrc to avoid having to click through process exits.

That should be it, NAT is left as an exercise to the reader. Now you open the attachments menu (v) in a mail and select the attachment you want opened and press A.

Caveat

One use case this doesn't work for is opening HTML attachments with inline images and whatever in separate attachments. Apparently you can easily build something to solve that using https://www.blacktrash.org/hg/muttils/

Render "text/calendar" type

See here: nickmurdoch: Autoview .ics iCalendar (text/calendar) attachments in mutt

Be aware, when copy/pasting his script, you may get bit by funny whitespace issues and have to more or less remove them and replace with your own tabs.