Friday, December 31, 2010

PHP Script to send PDF file as attachment (requires FPDF object)

function mail_pdf_as_attachment($pdfobj,$faxnumber) {
$pdfdoc = $pdfobj->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
$to = "destinationaddress@destination.com";
$from = "sourceaddress@source.com";
$subject = $faxnumber;
$message = "See attached pdf file";
$separator = md5(time());
$eol = PHP_EOL;
$filename = "medfax6.pdf";
//headers
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;
// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$headers .= $message.$eol.$eol;
// attachment
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";
mail($to, $subject, "", $headers);
}

No comments:

Post a Comment