PH

« Home

Access Mutt attachments remotely (SSH)

Mutt is hard to adopt, once-you-learn-you-love-it, and very flexible email clients. Through remote (e.g. SSH) connections is time-consuming to open the attachments. It needs to save and download the attachment to remote host. One way to get around this is to serve the attachment through a webserver on a specific port and open up the browser on the remote host to view the attachment on that port. This can be easily done by a simple bash script and using netcat. Below is the script that you need to save in your home directory (e.g. ~/bin/muttattach.sh).

#!/bin/sh
# muttattch.sh
# This script handle all the attachment type in mutt and forward to
# a netcat on port 8083 to view on the remote/local system
# in mutt open attachment and in remote system open localhost.
TEMP="/tmp/muttattch"
PORT=8083
rm -f $TEMP
mkfifo --mode=600 $TEMP
# netcat is the fun part of this script.
# -l: listen for an incoming connection
# -q 1: wait 1s after EOF and quit
nc -q 1 -l $PORT < $TEMP &> /dev/null &
# send the HTTP headers, followed by a blank line.
echo "HTTP/1.1 200 OK" >> $TEMP
echo "Server: pi3ch/netcat (mutt)" >> $TEMP
echo "Cache-Control: no-cache" >> $TEMP
echo "Pragma: no-cache" >> $TEMP
echo -n "Content-type: " >> $TEMP
file -bni $1 2> /dev/null >> $TEMP
echo -n "Content-Length: " >> $TEMP
wc -c<$1 >> $TEMP
echo >> $TEMP
echo "Pragma: no-cache" >> $TEMP
echo -n "Content-type: " >> $TEMP
file -bni $1 2> /dev/null >> $TEMP
echo -n "Content-Length: " >> $TEMP
wc -c<$1 >> $TEMP
echo >> $TEMP
# Get started sending the file...
cat $1 >> $TEMP &
# Progress bar
# Fire up your browser now!
sleep 7
view raw muttattach.sh hosted with ❤ by GitHub

Make it executable (e.g. chmod +x muttattach.sh). Modify ~/.mutt/mailcap and the following lines:

text/; ~/bin/muttattch.sh %s
application/; ~/bin/muttattch.sh %s
image/; ~/bin/muttattch.sh %s
audio/; ~/bin/muttattch.sh %s
 
Now open up an attachment in mutt, browse to http://localhost:8083 on remote host.
the above script is the modified version of http://www.linuxjournal.com/article/6511