TwistedPear has asked for the wisdom of the Perl Monks concerning the following question:

Greetings enligthened ones :)

I'm writing a script that is attempting to send and recieve emails containing file attachments.

The script is designed to run over a large collection of files and send and recieve each file individually (there is a mail server on the local machine that is handling the mail itself).

My question is this: is there an easy way to work out what kind of MIME type the file about to be sent is?

The collection contains all kinds of files; from text and html files to com and exe apps. It would be nice if I didn't have to write a rather large hash of extensions => MIME Types :)

I am using MIME::Tools to encode/send/decode the emails and Net::POP3 to recieve them.

Cheers

TwistedPear

Replies are listed 'Best First'.
(jeffa) Re: Finding out mime types...
by jeffa (Bishop) on May 21, 2003 at 06:12 UTC
Re: Finding out mime types...
by Jenda (Abbot) on May 21, 2003 at 19:01 UTC

    Since you seem to be running Windows you might ask the system what is the content type according to it:

    use Win32::Registry; use strict; { my %cache; sub GetContentType { my $ext = shift(); # well it's a filename at this point for ($ext) { s/^.*\.//; # now we extract the extension $_ = lc $_; } return $cache{$ext} if exists $cache{$ext}; my $key; $HKCR->Open('.'.$ext, $key) or return; my ($type, $value); $key->QueryValueEx('Content Type', $type, $value) or return; $cache{$ext} = $value; return $value; } } print "Foo.exe: " . GetContentType('Foo.exe') . "\n"; print "Foo.gif: " . GetContentType('Foo.gif') . "\n"; print "bar.exe: " . GetContentType('bar.exe') . "\n";
    (If the code doesn't work for you it might be because I tested it with Win32::Registry2 installed.)

    Jenda
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
       -- Rick Osborne

    Edit by castaway: Closed small tag in signature