views:

5612

answers:

7

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?

+3  A: 

With finfo_file: http://us2.php.net/manual/en/function.finfo-file.php

David Dorward
Getting the mime type of a file in PHP still is quite a pain in the a** ... ;-)
Philippe Gerber
A: 

There is the function header:

 header('Content-Type: '.$the_content_type);

Note that this function has to be called before any output. You can find further details in the reference http://www.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.

Eineki
I think this isn't the question... ed.talmage needs to determine the mimetype/content-type of the attached file, the header is already taken care of.
JorenB
Caught! I was just modifying the answer, by the way ;)
Eineki
A: 

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/

Yes, I plan to use that in future. Thanks for the link.
+2  A: 

Here's an example using finfo_open which is available in PHP5 and PECL:

$mimepath='/usr/share/magic'; // may differ depending on your machine
// try /usr/share/file/magic if it doesn't work
$mime = finfo_open(FILEINFO_MIME,$mimepath);
if ($mime===FALSE) {
 throw new Exception('Unable to open finfo');
}
$filetype = finfo_file($mime,$tmpFileName);
finfo_close($mime);
if ($filetype===FALSE) {
 throw new Exception('Unable to recognise filetype');
}

alternatively, you can use the depreciated mime_ content_ type function:

$filetype=mime_content_type($tmpFileName);

or use the OS's in built functions:

ob_start();
system('/usr/bin/file -i -b ' . realpath($tmpFileName));
$type = ob_get_clean();
$parts = explode(';', $type);
$filetype=trim($parts[0]);
Richy C.
A: 

... You can make the simplest email script within 10min

or just use PHPMailer to send emails, which is even faster since all the functions you'll ever need to send emails is ready-made for you, including adding attachments with 1 line of code.

JJJ
A: 

Hi. try this:

function ftype($f) {
                    curl_setopt_array(($c = @curl_init((!preg_match("/[a-z]+:\/{2}(?:www\.)?/i",$f) ? sprintf("%s://%s/%s", "http" , $_SERVER['HTTP_HOST'],$f) :  $f))), array(CURLOPT_RETURNTRANSFER => 1, CURLOPT_HEADER => 1));
                        return(preg_match("/Type:\s*(?<mime_type>[^\n]+)/i", @curl_exec($c), $m) && curl_getinfo($c, CURLINFO_HTTP_CODE) != 404)  ? ($m["mime_type"]) : 0;

         }
echo ftype("http://img2.orkut.com/images/medium/1283204135/604747203/ln.jpg"); // print image/jpeg
Jet
Holy unreadable ternary operator, Batman! ;-P
deceze
+2  A: 

I am using this function, which includes several fallbacks to compensate for older versions of PHP or simply bad results:

function getFileMimeType($file) {
    if (function_exists('finfo_file')) {
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        $type = finfo_file($finfo, $file);
        finfo_close($finfo);
    } else {
        require_once 'upgradephp/ext/mime.php';
        $type = mime_content_type($file);
    }

    if (!$type || $type == 'application/octet-stream') {
        $secondOpinion = exec('file -b --mime-type ' . escapeshellarg($file), $foo, $returnCode);
        if ($returnCode == '0' && $secondOpinion) {
            $type = $secondOpinion;
        }
    }

    return $type;
}

It tries to use the newer PHP finfo functions. If those aren't available, it uses the mime_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.

deceze