I'm trying to email an image on my server as an attachment. To accomplish this task, I used the following PHP script which grabs a JPG (called "php.jpg") located in a directory called "screenshots" from my server and sends it as an attachment.
<?php
$path = "screenshots/php.jpg";
$fp = fopen($path, 'r');
do //we loop until there is no data left
{
$data = fread($fp, 8192);
if (strlen($data) == 0) break;
$content .= $data;
} while (true);
$content_encode = chunk_split(base64_encode($content));
$mime_boundary = "<<<--==+X[".md5(time())."]";
$headers .= "From: Automatic <[email protected]>\r\n";
$headers .= "To: SomeName <[email protected]>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed;\r\n";
$headers .= " boundary=\"".$mime_boundary."\"";
$message .= "This is a multi-part message in MIME format.\r\n";
$message .= "\r\n";
$message .= "--".$mime_boundary."\r\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n";
$message .= "\r\n";
$message .= "Email content and what not: \r\n";
$message .= "This is the file you asked for! \r\n";
$message .= "--".$mime_boundary."\r\n";
$message .= "Content-Type: image/jpeg;\r\n";
$message .= " name=\"php.jpg\"\r\n";
$message .= "Content-Transfer-Encoding: quoted-printable\r\n";
$message .= "Content-Disposition: attachment;\r\n";
$message .= " filename=\"php.jpg\"\r\n";
$message .= "\r\n";
$message .= $content_encode;
$message .= "\r\n";
$message .= "--".$mime_boundary."\r\n";
$ok = mail("[email protected]", "file by email", $message, $headers);
Overall, the script works. I receive an email in my inbox containing the message text specified above and a JPG attachment. Stack Overflow won't let me post a photo because I'm new, but a screenshot of the message is available here: http://i48.tinypic.com/xfuee0.png
My problem occurs when I try to view the attachment. Clicking the attachment simply opens a new browser window and displays a missing image icon.
Do you see any problems with my script that would prevent the image from appearing?
Any info would be great. Thanks!