matt86 Posted January 8, 2010 Share Posted January 8, 2010 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. 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! Link to comment https://forums.phpfreaks.com/topic/187652-email-image-attachment-with-mail/ Share on other sites More sharing options...
JAY6390 Posted January 8, 2010 Share Posted January 8, 2010 Change the encoding to base64 encoding Link to comment https://forums.phpfreaks.com/topic/187652-email-image-attachment-with-mail/#findComment-990732 Share on other sites More sharing options...
matt86 Posted January 8, 2010 Author Share Posted January 8, 2010 In the loop, I'm already using base64 encoding. Did you notice something else? Link to comment https://forums.phpfreaks.com/topic/187652-email-image-attachment-with-mail/#findComment-990769 Share on other sites More sharing options...
matt86 Posted January 8, 2010 Author Share Posted January 8, 2010 Change the encoding to base64 encoding Jay, I love you! I figured out what you meant and changed my "Content-transfer-encoding" to base64. Worked perfectly. Thanks so much! Link to comment https://forums.phpfreaks.com/topic/187652-email-image-attachment-with-mail/#findComment-990774 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.