I'm using PHP to send an email with an attachment. The attachment could be any of several different file types (pdf, txt, doc, swf, etc).
First, the script gets the file using "file_get_contents".
Later, the script echoes in the header:
Content-Type: <?php echo $the_content_type; ?>; name="<?php echo $the_file_name; ?>"
How to I set the correct value for $the_content_type?
I really recommend using a Framework like "CodeIgniter" for seinding Emails. Here is a Screencast about "Sending Emails with CodeIgniter" in only 18 Minutes.
http://net.tutsplus.com/videos/screencasts/codeigniter-from-scratch-day-3/
Here's an example using finfo_open which is available in PHP5 and PECL:
Alternatively, you can use the deprecated mime_ content_ type function:
or use the OS's in built functions:
I am using this function, which includes several fallbacks to compensate for older versions of PHP or simply bad results:
It tries to use the newer PHP
finfo
functions. If those aren't available, it uses themime_content_type
alternative and includes the drop-in replacement from the Upgrade.php library to make sure this exists. If those didn't return anything useful, it'll try the OS'file
command. AFAIK that's only available on *NIX systems, you may want to change that or get rid of it if you plan to use this on Windows. If nothing worked, it triesexif_imagetype
as fallback for images only.I have come to notice that different servers vary widely in their support for the mime type functions, and that the Upgrade.php
mime_content_type
replacement is far from perfect. The limitedexif_imagetype
functions, both the original and the Upgrade.php replacement, are working pretty reliably though. If you're only concerned about images, you may only want to use this last one.With finfo_file: http://us2.php.net/manual/en/function.finfo-file.php
It very easy to have it in php.
Simply call the following php function
mime_content_type
PHP documentation of the function mime_content_type() Hope it helps someone
There is the function header:
Note that this function has to be called before any output. You can find further details in the reference http://php.net/header
Edit:
Ops, I've misunderstood the question: Since php 4.0 there is the function mime_content_type to detect the mimetype of a file.
In php 5 is deprecated, should be replaced by the file info set of functions.