tags:

views:

53

answers:

3

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!

A: 

I can see one possible reason why you're not seeing your image. (There may be more (!).)

Try changing:

$message .= "--".$mime_boundary."\r\n"; 

to

$message .= "--".$mime_boundary."--\r\n"; 

For the last line before the call to mail (ie the "epilogue" boundary).

martinr
A: 

Three things jump out:

One is that the first append to variables $content and $message and $headers doesn't explicitly set a new value. That is, why not

$headers = "From: Automatic <[email protected]>\r\n";

instead of like you have:

$headers .= "From: Automatic <[email protected]>\r\n";

That eliminates the possibility that some leftover stuff is hanging out in the variables.

The second is that there is \r\n instead of plain \n which should work on every system, even Windows. I doubt this is a problem though.

Third is the closing mime boundary isn't the same as the open.

wallyk
According to RFC 822, \r\n is the correct way to end header fields.
Mikael S
+1  A: 

To anyone who comes across this post in the future, the problem came from the "Content-Transfer-Encoding" which should have been set to base64.

$message .= "Content-Transfer-Encoding: quoted-printable\r\n";

becomes:

$message .= "Content-Transfer-Encoding: base64\r\n";
Matt