Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Using applications to view attachments from Mutt UNIX
Mutt uses a .mailcap file to decide how to view attachments. I tried many different ways of specifying the handlers but nothing seemed to work. Even an example that worked with pine did not work. The first problem was that the temporary file would be deleted before the application spawned by open could read it. Another problem is that some file types could not be discerned from the file alone. Some files even have extensions missing. This solution solves all of those problems. It allows for any appropriate application to handle any type of file. The key is to have a program that does several things:
  • Move the file to a slightly more permanent location.
  • Discern the file type and extension if not given.
  • Don't do anything with the type if told to ignore it.
  • Allow for an application to be specified.
  • Open the appropriate application for the file
Luckily the open command does a good job if given enough hints about what type of file it's dealing with. That usually means a good file extension. Do man open for more details. There was still a fair amount of file handling needed to accomplish all of these goals. I wrote this bash script and called it view_attachment and placed it in /usr/local/bin. Make sure it's got execute permissions with chmod +x view_attachment. The program is more comment than code -- but that's a good thing.

The next step is to edit your ~/.mailcap file so mutt can use the new program. The easiest entries are for images and pdfs, because their types are easily discernable. The mailcap entry for jpeg looks like this:
 Image/JPEG;view_attachment %s
All this entry does is call the view_attachment program with the name of the temporary file that mutt wrote. view_attachment will discern the type and add or change the file extension so the Preview application will know what to do with it.

The next problem I had was with HTML files. Frequently, HTML mail does not have an extension. It is also not always discernable with the file command that does so well with images and lots of other file types. The mailcap file helps with this. It knows that the attachment is HTML. The solution is to tell view_attachment what the type is. That keeps it from guessing wrong. So the mailcap entry for HTML looks like this.
 text/html; view_attachment %s html
The html after the %s tells view_attachment that the file type is html. view_attachment uses the type for the file extension. That ensures the proper application will handle it. Most likely that will be Safari.

The last problem was using a specific application for a specific file type. I use Open Office for a lot of different file types including Excel, Word, and PowerPoint files. Open Office is really good at figuring out what the file is, and doing the right thing. view_attachment doesn't need to do anything with the type. It could mess things up if it did. It still needs to move the file, though. The only other thing it should do is an open -a command with the application we asked for. That means two things.
  • Type discerning needs to be turned off completely.
  • There needs to be a third argument to specify the application
The file typing can be turned off by giving a - as the type. Here's what the majority of my Mailcap entries look like. The only difference is mime-type at the beginning of each line. Note this is shown on two lines, but it's one long line with a space instead of a line break:
 Application/vnd.ms-excel; /usr/local/bin/view_attachment %s "-" 
  '/Applications/OpenOffice.org1.1.2/Start_OpenOffice.org.app'
I changed the name of Start OpenOffice.org.app to Start_OpenOffice.org.app in my Applications -> OpenOffice.org1.1.2 directory. The space could have stayed, but it has to be escaped with a \, and that was more pain than I wanted to deal with. Here's my entire .mailcap file as an example to work with.
    •    
  • Currently 3.33 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[22,205 views]  

Using applications to view attachments from Mutt | 8 comments | Create New Account
Click here to return to the 'Using applications to view attachments from Mutt' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
two suggestions
Authored by: mzs on Dec 02, '04 12:41:26PM
Great script thanks, but here are two little suggestions. In this part:

# Copy the file to our new spot so mutt can't delete it
# before the app has a chance to view it.
cp $1 $newfile
Maybe the cp would be better as a ln. This would create a hard link (as long as the files were on the same file system) instead of copying the file.

The second thing, wouldn't be better if you had at least a $USER in the tmpdir used? But probably you are the only one using this script.

[ Reply to This | # ]

Use Links for text/html
Authored by: googoo on Dec 02, '04 02:12:37PM
If you would rather use links (or another terminal-based browser) for text/html messages, just use this line in your .mailcap file.

text/html; links -dump %s; needsterminal; copiousoutput;

Substitute the executable browser of your choice for links above.

-Mark

[ Reply to This | # ]

Use Links for text/html
Authored by: bluehz on Dec 03, '04 08:22:28AM

Similar to what I use...

text/html; open -a Safari '%s'; needsterminal; description=HTML Text; nametemplate=%s.html
image/gif; open -a Preview %s
image/jpg; open -a Preview %s; needsterminal; description=JPEG Image; nametemplate=%s.jpg
image/jpeg; open -a Preview %s; needsterminal; description=JPEG Image; nametemplate=%s.jpg
text/text; open -a BBEdit '%s'; needsterminal; description=Text Text; nametemplate=%s.txt
application/pdf; open -a Preview '%s' -; copiousoutput

although I might give the unified script a try too...



[ Reply to This | # ]
Using applications to view attachments from Mutt
Authored by: bjast on Dec 07, '05 06:15:35AM

How would these .mailcap entries be modified to launch Microsoft products instead of OpenOffice?

Thanks,
Bill J.



[ Reply to This | # ]
Using applications to view attachments from Mutt
Authored by: qwertyuiop on May 30, '06 04:42:19PM
This is great, thank you, but images in HTML attachments aren't 
displayed when they are called by strings that begin

"img src="cid:".

The closest remedy I found for this was something here 
(Just search for the word "perl"), but I'm not a programmer at all, and don't 
know how to appropriate it or something similar.  So if anyone has an idea 
how one would modify the bash script to accomplish viewing with these 
tags, I'd appreciate it very much.

If it matters in the slightest, I'm using OS X 10.3.9, using Safari 1.3.2 
(v312.6) to view HTML attachments when I so choose in Mutt.  I've applied 
all the recent Software Updates for Panther, Safari, and the Apple Webkit.  I 
tried Camino 1.0.1, as well, just in case, with the same results.


Thanks,
Stan


[ Reply to This | # ]
Using applications to view attachments from Mutt
Authored by: indulekha on Oct 06, '08 01:21:47PM

Thank you so much for posting this one. I love using mutt but felt forced to run a graphical client the last couple of years due to all the html and images in modern email. I followed your directions and used your view_attachment script, and am now happily reading all my email in mutt again. That two years of using various graphical MUAs was so hellish! I will no longer shudder when my mom sends me the latest pictures of her cat -- well okay, maybe I will a bit, but not because I have to use Thunderbird. ;)



[ Reply to This | # ]
Changes for 10.6
Authored by: TokyoJimu on Mar 31, '10 04:28:40PM

In 10.6 (Snow Leopard), the output of "file --mime" has changed, breaking the view_attachment script. I have fixed it by replacing:

if [ -z "$type" ]; then
type=`file -b --mime "$1" | cut -d"/" -f2`
fi

with:

# Get the OS version number and strip out the decimal points so we can
# use integer comparison, and so it will work for future versions.
syssubver=`sw_vers -productVersion | sed s/\\.//g`

# if the type is empty then try to figure it out.
if [ -z "$type" ]; then
if [ $syssubver -lt.1060 ]; then #OS X 10.5 or lower
type=`file -b --mime "$1" | cut -d"/" -f2`
else # For 10.6, you need the following instead
type=`file -b --mime-type "$1" | cut -d"/" -f2`
fi
fi



[ Reply to This | # ]
10.6 compatibility
Authored by: TokyoJimu on Mar 31, '10 05:51:28PM

[revised version after further testing]

The output of the --mime option to file(1) has changed in 10.6 (Snow Leopard), necessitating changing these lines in view_attachment:

# if the type is empty then try to figure it out.
if [ -z "$type" ]; then
type=`file -b --mime "$1" | cut -d"/" -f2`
fi

to:

# Get the OS version number and strip out the decimal points so we can
# use integer comparison, and so it will work for future versions.
osxversion=`sw_vers -productVersion | sed s/\\\.//g`
echo osxversion is ${osxversion}

# if the type is empty then try to figure it out.
if [ -z "$type" ]; then
if [ ${osxversion} -lt 1060 ]; then #OS X 10.5 or lower
type=`file -b --mime "$1" | cut -d"/" -f2`
else # For 10.6, you need the following instead
type=`file -b --mime-type "$1" | cut -d"/" -f2`
fi
fi



[ Reply to This | # ]