Generate a PDF and send as attachment in Email

One way:

In PHP

// download fpdf class (http://fpdf.org)
require("/fpd/fpdf.php");

// fpdf object
$pdf = new FPDF();

// generate a simple PDF (for more info, see http://fpdf.org/en/tutorial/)
$pdf->AddPage();
$pdf->SetFont("Arial","B",14);
$pdf->Cell(40,10, "this is a pdf example");

// email stuff (change data below)
$to = "target@domain.com";
$from = "me@domain.com";
$subject = "send email with pdf attachment";
$message = "<p>Please see the attachment.</p>";

// a random hash will be necessary to send mixed content
$separator = md5(time());

// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;

// attachment name
$filename = "example.pdf";

// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));

// main header (multipart mandatory)
$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."--";

// send message
mail($to, $subject, "", $headers);



Second way:

$pdf_name = str_replace(" ", "_", $employee_type).'_'.time();

$query_emi_form = "insert query";
$resultset_emi_queryform = mysql_query($query_emi_form);
$last_id = mysql_insert_id();
$saved = "Query Form Submitted Successfully";
$result1=mysql_query("select * from tbl where id=".$last_id);
$data = '<div align="center"><table width="500" border="1"><tr>
<th scope="col" width="240">&nbsp;Field Name</th>
<th scope="col" width="240">&nbsp;Value</th>
</tr>';
while($row=mysql_fetch_field($result1)){
$f[] = $row;
}
$rows = mysql_fetch_assoc($result1);
for($i=0;$i<count($f);$i++){
$fname = ucfirst(str_replace("_", " ", $f[$i]->name));
$fname_original = $f[$i]->name;
$ff[]=$rows;
$data .="<tr><td>$fname</td><td>$rows[$fname_original]</td></tr>";
}
$data .="</table></div>";
$stringData = '<html><body>'.$data.'</body></html>';

//use this http://www.macronimous.com/resources/Converting_HTML2PDF_using_PHP.asp
require('html2pdf_sample/html2fpdf.php');
$pdf=new HTML2FPDF();
$pdf->AddPage();

$pdf->WriteHTML($stringData);
$pdf->Output("emi_pdf/".$pdf_name.".pdf");

if($emi_query_id){
$result2="select * from tbl_queries where query_id=".$emi_query_id;
$resultset_result = mysql_query($result2);

$resultset = mysql_fetch_array($resultset_result);

$query_no = $resultset['query_no'];

$message = '<html><body>';
$message .='Dear Sir/Mam,<br><br>';
$message .='Greetings from !!!<br><br><br>';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr><td><strong>Query No:</strong> </td><td>" .$query_no. "</td></tr>";
$message .= "</table>";
$message .='Sincerely,<br><br>';
$message .='Name<br>';
$message .= "</body></html>";

$to = 'email@gmail.com';
$from = "noreply@domainname.com";
$subject = "QUERY NO.-".$query_no;

// a random hash will be necessary to send mixed content
$separator = md5(time());

// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;

// attachment name
$filename = $pdf_name.".pdf";
$fileatt ="emi_pdf/".$filename;
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$attachment = chunk_split(base64_encode($data));

// main header (multipart mandatory)
$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, $message, $headers);
}

1 thought on “Generate a PDF and send as attachment in Email”

Comments are closed.