开发者

Sending email in php with inline images

I am using php's built-in mail-function to send emails. So far I've accomplished a lot by looking at other examples and stripping what I thought would be necessary.

My current code for sending emails with inline images looks like this. The content is generated by using a template which may or may not have some placeholders which are replaced by values from my database (this is omitted)

function sendEmail($to, $subject, $htmlmessage, $inline_images = array()){   
   define('RN', "\n");
   /* define mime boundaries */
   $mime_boundary_mix = "_mix_" . md5(time());

   /* set headers */
   $headers = "From: info@domain.nl" . RN;
   $headers .= "Reply-To: info@domain.nl" . RN;
   $headers .= "Return-Path: info@otherdomain.nl" . RN;
   $headers .= "Message-ID: <".time()."_DOMAIN@".$_SERVER['SERVER_NAME'].">" . RN;
   $headers .= "X-Mailer: PHP v".phpversion() . RN;

   $headers .= "Mime-Version: 1.0" . RN;
   $headers .= "Content-Type: multipart/mixed;" . RN;
   $headers .= " boundary=\"{$mime_boundary_mix}\"";

   /* start email body with mime_boundary */
   $msg = "--{$mime_boundary_mix}" . RN;
   /* include html */
   $msg .= "Content-Type: text/html; charset=\"iso-8859-15\"". RN;
   $msg .= "Content-Transfer-Encoding: 8bit". RN . RN;
   $msg .= $htmlmessage . RN;

   /* check if there are inline images */
   if(count($inline_images) > 0) {
      foreach($inline_images as $image) {
         $path_to_image = $image['path'];
         $image_filename = basename($path_to_image);
         $image_type = filetype($path_to_image);

         $file_handler = fopen($path_to_image, 'rb');
         $image_data = chunk_split(base64_encode(fread($file_handler, filesize($path_to_image))));
         fclose($file_handler);

         /* start mime boundary */
         $msg .= "--{$mime_boundary_mix}" . RN;
         /* add image data */
         $msg .= "Content-Type: {" . image_type_to_mime_type (exif_imagetype($path_to_image)) . "};" . RN;
         $msg .= " name=\"{$image_filename}\"" . RN;
         $msg .= "Content-Disposition: inline;" . RN;
         $msg .= " filename=\"{$image_filename}\"" . RN;
         $msg .= "Content-Transfer-Encoding: base64" . RN . RN;
         $msg .= $image_data . RN . RN;

      }
      /* end mime boundary */
      $msg .= "--{$mime_boundary_mix}--" . RN;

   }
   /* end mime boundary */
   $msg .= "--{$mime_boundary_mix}--" . RN;

   return mail($to, $subject, $msg, $headers);
}

$inline_images is an array which contains items with paths to images like: array([0] => array('path' => "myimage.png")) (if I'm not mistaken, but this is not important)

Can anybody tell me why using this code, Outlook puts an attachment icon with this e-mail and why gmail shows a warning: do you want to show all images and doesn't show any when I allow these? A few questions about this: - Receiving this e-mail in Outlook works fine, but I'd like to hide the paperclip icon if possible, afterall it's an embedded image and not really an attachment. - In GMail I receive the e-mail with the images as attachments and blanks (broken image-icon) at the place of all images in the e-mail, how do I fix t开发者_StackOverflow社区his? GMail asks me first if I want to allow the images, but after I allow the blanks show up. And can I also hide the images as attachments in GMail? - I also tried viewing the e-mail in Office Outlook Web Access, which presented the e-mail in the same manner GMail did: images not shown, but only as attachments.

Can anyone help me with this? If you need more details, please ask.


Why can't you go for libraries like phpmailer or switmailer.? They provide full support for html mails. Also you won't need to create all the headers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜