title: Emailing a dynamically created image
body: <p>I need to create an image dynamically then email it to the person. Here is the image creation script:</p>
  
  <pre><code>//Get the blank coupon
  $rImg = ImageCreateFromJPEG('coupon.jpg');
  
  $color = imagecolorallocate($rImg, 0, 0, 0);
  
  imagestring($rImg,5,135,140,'$first_name', 'black');
  imagestring($rImg,5,135,160,'$last_name', 'black');
  imagestring($rImg,5,135,180,'$email_address', 'black');
  </code></pre>
  
  <p>From there I want to take that image and email it to the person.  Can I just include it in attachment of the email or do I actually have to write it to the server?  </p>
  
  <p>I have tried doing this: </p>
  
  <pre><code>function email_coupon($first_name, $last_name, $email_address){
  //Get the blank coupon
  $rImg = ImageCreateFromJPEG('coupon.jpg');
  
  $color = imagecolorallocate($rImg, 0, 0, 0);
  
  imagestring($rImg,5,135,140,'$first_name', 'black');
  imagestring($rImg,5,135,160,'$last_name', 'black');
  imagestring($rImg,5,135,180,'$email_address', 'black');
  
  //define the receiver of the email
  $to = '$email_address';
  //define the subject of the email
  $subject = 'Your coupon';
  //create a boundary string. It must be unique
  //so we use the MD5 algorithm to generate a random hash
  $random_hash = md5(date('r', time()));
  //define the headers we want passed. Note that they are separated with \r\n
  $headers = "From: joe@joesomebody.com\r\nReply-To: joe@joesomeboy.com";
  //add boundary string and mime type specification
  $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
  //read the atachment file contents into a string,
  //encode it with MIME base64,
  //and split it into smaller chunks
  ob_start();
  imagejpeg($rImg);
  $i = ob_get_clean();
  $attachment = chunk_split(base64_encode($i));
  //define the body of the message.
  ob_clean();
  ob_start(); //Turn on output buffering
  ?&gt;
  --PHP-mixed-&lt;?php echo $random_hash; ?&gt; 
  Content-Type: multipart/alternative; boundary="PHP-alt-&lt;?php echo $random_hash; ?&gt;"
  
  --PHP-alt-&lt;?php echo $random_hash; ?&gt; 
  Content-Type: text/plain; charset="iso-8859-1"
  Content-Transfer-Encoding: 7bit
  
  Hello World!!!
  This is simple text email message.
  
  --PHP-alt-&lt;?php echo $random_hash; ?&gt; 
  Content-Type: text/html; charset="iso-8859-1"
  Content-Transfer-Encoding: 7bit
  
  &lt;h2&gt;Hello World!&lt;/h2&gt;
  &lt;p&gt;This is something with &lt;b&gt;HTML&lt;/b&gt; formatting.&lt;/p&gt;
  
  --PHP-alt-&lt;?php echo $random_hash; ?&gt;--
  
  --PHP-mixed-&lt;?php echo $random_hash; ?&gt; 
  Content-Type: image/jpeg; name="coupon.jpg" 
  Content-Transfer-Encoding: base64 
  Content-Disposition: attachment 
  
  &lt;?php echo $attachment; ?&gt;
  --PHP-mixed-&lt;?php echo $random_hash; ?&gt;--
  
  &lt;?php
  //copy current buffer contents into $message variable and delete current output buffer
  $message = ob_get_clean();
  //send the email
  $mail_sent = @mail( $email_address, $subject, $message, $headers );
  //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
   if($mail_sent == true) {
      echo 'sent';
   } else {
      echo 'failed';
   };
  </code></pre>
  
  <p>};</p>
  
  <p>Anyone have any ideas?</p>
  
  <p>Thanks in advance.</p>