title: how to send an HTML email with an inline attached image with PHP
body: <p>I have a PHP script which sends an HTML email with an attached image. It works beauifully, however, I can't get the attachment to display in an <code>&lt;img&gt;</code> tag in the email body. The attached file is called <code>postcard.png</code> and the original filename on the server is <code>4e60348f83f2f.png</code>. I've tried giving the image URL as various things: <code>cid:postcard.png</code>, <code>cid:4e60348f83f2f.png</code>, <code>postcard.png</code>, and <code>4e60348f83f2f.png</code>. Nothing works. </p>
  
  <p>I think the key part that I'm doing wrong is here, because this makes it a separated attachment instead of an inline attachment that I can use:</p>
  
  <pre><code>Content-Transfer-Encoding: base64
  Content-Disposition: attachment;
      filename="$fname" // i.e.: "postcard.png"
  </code></pre>
  
  <p>I've tried changing it to use a CID but I don't really know how to do that, and this didnt' work at all:</p>
  
  <pre><code>Content-Transfer-Encoding: base64
  Content-ID: &lt;$fname&gt; // i.e.: postcard.png
  </code></pre>
  
  <p>Here's the full code: (It's based on <a href="http://www.php.net/manual/en/function.mail.php#83491" rel="noreferrer">this code</a> from a comment in the php <code>mail()</code> page.)</p>
  
  <pre><code>&lt;?php
  $to      = "recipient@email.com";
  $email   = "sender@email.com";
  $name    = "Namename";
  $subject = "An inline image!"; 
  $comment = "Llookout &lt;b&gt;Llary&lt;/b&gt; it's &lt;br&gt; the &lt;b&gt;Ll&lt;/b&gt;andllord!&lt;br&gt;&lt;img src='cid:postcard.png'&gt;&lt;br&gt;&lt;img src='cid:4e60348f83f2f.png'&gt;&lt;img src='postcard.png'&gt;&lt;br&gt;&lt;img src='4e60348f83f2f.png'&gt;";
  
  $To          = strip_tags($to);
  $TextMessage =strip_tags(nl2br($comment),"&lt;br&gt;");
  $HTMLMessage =nl2br($comment);
  $FromName    =strip_tags($name);
  $FromEmail   =strip_tags($email);
  $Subject     =strip_tags($subject);
  
  $boundary1   =rand(0,9)."-"
      .rand(10000000000,9999999999)."-"
      .rand(10000000000,9999999999)."=:"
      .rand(10000,99999);
  $boundary2   =rand(0,9)."-".rand(10000000000,9999999999)."-"
      .rand(10000000000,9999999999)."=:"
      .rand(10000,99999);
  
  $filename1 = "4e60348f83f2f.png"; //name of file on server with script
  $handle      =fopen($filename1, 'rb'); 
  $f_contents  =fread($handle, filesize($filename1)); 
  $attachment=chunk_split(base64_encode($f_contents));
  fclose($handle); 
  
  $ftype       ="image/png";
  $fname       ="postcard.png"; //what the file will be named
  
  
  $attachments='';
  $Headers     =&lt;&lt;&lt;AKAM
  From: $FromName &lt;$FromEmail&gt;
  Reply-To: $FromEmail
  MIME-Version: 1.0
  Content-Type: multipart/mixed;
      boundary="$boundary1"
  AKAM;
  
  $attachments.=&lt;&lt;&lt;ATTA
  --$boundary1
  Content-Type: $ftype;
      name="$fname"
  Content-Transfer-Encoding: base64
  Content-Disposition: attachment;
      filename="$fname"
  
  
  $attachment
  
  ATTA;
  
  
  $Body        =&lt;&lt;&lt;AKAM
  This is a multi-part message in MIME format.
  
  --$boundary1
  Content-Type: multipart/alternative;
      boundary="$boundary2"
  
  --$boundary2
  Content-Type: text/plain;
      charset="windows-1256"
  Content-Transfer-Encoding: quoted-printable
  
  $TextMessage
  --$boundary2
  Content-Type: text/html;
      charset="windows-1256"
  Content-Transfer-Encoding: quoted-printable
  
  $HTMLMessage
  
  --$boundary2--
  
  $attachments
  --$boundary1--
  AKAM;
  
  // Send email
  $ok=mail($To, $Subject, $Body, $Headers);
  echo $ok?"&lt;h1&gt; Mail sent!&lt;/h1&gt;":"&lt;h1&gt; Mail not sent!&lt;/h1&gt;";
  ?&gt;
  </code></pre>