I have a PHP script which emails someone 2 images when they have completed a payment via paypal. Now 1 of these images has text added to it via the imagettftext function.
Now what I am looking to do, is instead of email the 2 images, I want the 2 images to be added to a PDF file on their own pages, and then have that PDF file emailed to them.
Heres my code for the email which sends the images:
$sql = mysql_query("select * from `bookings` where `id`='$order_id'");
while ($row = mysql_fetch_assoc($sql))
{
foreach ($row as $k => $v)
$$k = $v;
$img = imagecreatefrompng("ticket-blank.png");
if (!empty($lead_home_phone)) $phone = "Home Phone: $lead_home_phone\n";
else if (!empty($lead_work_phone)) $phone = "Work Phone: $lead_work_phone\n";
else if (!empty($lead_mobile_phone)) $phone = "Mobile Phone: $lead_mobile_phone\n";
$dets = "Full name: $lead_title $lead_name\n";
$dets .= "Address 1: $lead_address_1\n";
$dets .= "Postcode: $lead_postcode\n";
$dets .= "City/Town: $lead_city\n";
$dets .= "State/Province: $lead_state\n";
$dets .= "Country: $lead_country\n";
if (!empty($phone)) $dets .= "$phone\n";
$dets .= "Email Address: $lead_email";
imagettftext($img, 8, 0, 50, 115, imagecolorallocate($img, 0, 0, 0), 'arial.ttf', "BOOKING FOR ".date('F j, Y', $the_date)."\nORDER ID # $order_id");
imagettftext($img, 8, 0, 270, 115, imagecolorallocate($img, 0, 0, 0), 'arial.ttf', "$num_adults ADULTS $num_children CHILDREN\n$ticket_type");
imagettftext($img, 8, 0, 50, 165, imagecolorallocate($img, 0, 0, 0), 'arial.ttf', $dets);
imagettftext($img, 8, 0, 415, 165, imagecolorallocate($img, 0, 0, 0), 'arial.ttf', "Total: CZK $pay_total");
$img_name = time().'.png';
imagepng($img, "tickets/$img_name");
$files = array();
$files []= "tickets/$img_name";
$files []= "tickets/brochure.jpg";
// email fields: to, from, subject, and so on
$from = "info@mysite.com";
$subject ="Your Ticket";
$message = "Booking Complete!";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
for ($x=0; $x < count($files); $x++)
{
$file = fopen($files[$x],"rb");
$data = fread($file,filesize($files[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
}
// mail the emails with attachments
mail('me@myemail.com', $subject, $message, $headers);
}
How would I go about adding the 2 images into a PDF file, and then emailing that?
Take a look at a php pdf generation library like fpdf. I’ve used fpdf before and find it pretty easy and straight forward. Here is an example of how to use it from their site:
As you can see you basically just create a pdf, add a page, and then add images, text, tables etc. to that page and then you can output the pdf. In your case you would want to output it to some temp file, email that file, then probably delete that file to save room on your server (or delete them after some amount of days)
Hopefully this helps!