Here is the source code for sending mail with Image as an attachment. In my example, I have used Linux -Debian -PHP 5 version to check mail attachment. I tried with sending Gif and PNG image. Its works well. I hope it works for other too.. Change the content type image/gif to image/png for PNG and image/jpeg for JPG images.
$subject = ‘PHP Mail Attachment Test’;
$bound_text = “shiva123″;
$bound = “–”.$bound_text.”\r\n”;
$bound_last = “–”.$bound_text.”–\r\n”;
$filename=”logo.png”;
$file_path=”/tmp/”.$filename;
/* //////////From and To Address ////////////////*/
$to =”from@domain.com”;
$headers =”From: to@domain.com\r\n”;
/* /////////////////////NON-EDITABLE PART /////////////////*/
$headers .= “MIME-Version: 1.0\r\n”
.”Content-Type: multipart/mixed; boundary=\”$bound_text\”";
$message .= $bound;
$message .= “Content-Type: text/html; charset=\”iso-8859-1\”\r\n”
.”Content-Transfer-Encoding: 7bit\r\n\r\n”;
$message.=”Hai this is example of sending Mail with Attachments\r\n”.$bound;
$file = file_get_contents($file_path);
$message .= “Content-Type: image/gif; name=\”".$filename.”\”\r\n”
.”Content-Transfer-Encoding: base64\r\n”
.”Content-disposition: attachment; file=\”".$filename.”\”\r\n”
.”\r\n”
.chunk_split(base64_encode($file))
.$bound_last;
/*////////////////////////////// NON-EDITABLE ENDS HERE ////////// */
mail($to, $subject, $message, $headers); // sending mail
Thanks Jim Plush!