This sounds a job for
CGI.pm. It will give you a nifty hashref in the header() for this kind of stuff. Also, you may want to look into the UNIX
file(1) command which has a special place in its heart for MIME types. If you're dealing with images ONLY (it doesn't look like it, though), you should use
Image::Magick which will cough up the MIME type for images. In any case, there is no need (and it's safer) to use a tried and true module of program (file) for your MIMEing needs.
Q: If Outlook automatically opens certain MIME types, and no one is around to see it, does the embedded virus remain dormant?