Email form results as PDF

Thread Solved

Join Date: Nov 2007
Posts: 119
Reputation: Roebuc is an unknown quantity at this point 
Solved Threads: 2
Roebuc's Avatar
Roebuc Roebuc is offline Offline
Junior Poster

Email form results as PDF

 
0
  #1
Jul 22nd, 2008
Can someone tell me how I can accomplish this. I want the results of a form to be placed in a pdf and then added as an attachment. I am not sure if this is possible, but if someone knows how, please let me know. Thanks.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 189
Reputation: martin5211 is an unknown quantity at this point 
Solved Threads: 14
martin5211 martin5211 is offline Offline
Junior Poster

Re: Email form results as PDF

 
2
  #2
Jul 23rd, 2008
To generate a PDF into PHP, there are two techniques: PDFLib or FPDF. PDFLib has some restrictions on licencing (commercial applications), I encourage to use FPDF because is free and Open Source, although PDFLib is rough and more complete (use your criterion). At http://fpdf.org there are a place to download the class and good tutorials. Use $_GET or $_POST variables to get the form data depending the form method.

To send the email, mail() function or PHPMailer are commons methods to perform these assignments. Please take care to encode the attachment and the headers, set multipart content type first, then the proper content type and encoding in each part, we could send plain or html text and multiple attachments too.

In this example I've used FPDF and mail() techniques. I don't know if it is better move this code to snippets section.

  1. <?php
  2. // download fpdf class (http://fpdf.org)
  3. require("fpdf153/fpdf.php");
  4.  
  5. // fpdf object
  6. $pdf=new FPDF();
  7.  
  8. // generate a simple PDF (for more info, see http://fpdf.org/en/tutorial/)
  9. $pdf->AddPage();
  10. $pdf->SetFont("Arial","B",14);
  11. $pdf->Cell(40,10, "this is a pdf example");
  12.  
  13. // email stuff
  14. $to = "target@domain.com";
  15. $from = "me@domain.com";
  16. $subject = "send email with pdf attachment";
  17. $message = "Please see the attachment.";
  18.  
  19. // a random hash will be necessary to send mixed content
  20. $separator = md5(time());
  21.  
  22. // carriage return type (we use PHP end of line constant)
  23. $eol = PHP_EOL;
  24.  
  25. // attachment name
  26. $filename = "example.pdf";
  27.  
  28. // encode data (put attachment in proper format)
  29. $pdfdoc = $pdf->Output("", "S");
  30. $attachment = chunk_split(base64_encode($pdfdoc));
  31.  
  32.  
  33. // main header (multipart mandatory)
  34. $headers = "From: ".$from.$eol;
  35. $headers .= "MIME-Version: 1.0".$eol;
  36. $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
  37. $headers .= "Content-Transfer-Encoding: 7bit".$eol;
  38. $headers .= "This is a MIME encoded message.".$eol.$eol;
  39.  
  40. // message
  41. $headers .= "--".$separator.$eol;
  42. $headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"".$eol;
  43. $headers .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
  44. $headers .= $message.$eol.$eol;
  45.  
  46. // attachment
  47. $headers .= "--".$separator.$eol;
  48. $headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
  49. $headers .= "Content-Transfer-Encoding: base64".$eol;
  50. $headers .= "Content-Disposition: attachment".$eol.$eol;
  51. $headers .= $attachment.$eol.$eol;
  52. $headers .= "--".$separator."--";
  53.  
  54. // send message
  55. mail($to, $subject, "", $headers);
  56.  
  57. ?>
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 569
Reputation: ryan_vietnow is an unknown quantity at this point 
Solved Threads: 71
ryan_vietnow's Avatar
ryan_vietnow ryan_vietnow is offline Offline
Posting Pro

Re: Email form results as PDF

 
0
  #3
Jul 23rd, 2008
yeah,you can move that to the snippets page.It will be useful to many!
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 189
Reputation: martin5211 is an unknown quantity at this point 
Solved Threads: 14
martin5211 martin5211 is offline Offline
Junior Poster

Re: Email form results as PDF

 
0
  #4
Jul 23rd, 2008
I've added the source at code snippets, using HTML message format at this time.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 119
Reputation: Roebuc is an unknown quantity at this point 
Solved Threads: 2
Roebuc's Avatar
Roebuc Roebuc is offline Offline
Junior Poster

Re: Email form results as PDF

 
0
  #5
Jul 23rd, 2008
Martin, thank you. That is perfect!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC