Guest User

Untitled

a guest
Mar 14th, 2018
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.89 KB | None | 0 0
  1. <?php
  2.  
  3. $view_html = FALSE;
  4. $view_pdf = FALSE;
  5.  
  6. error_reporting(~E_STRICT);
  7. require_once( "vender/dompdf/dompdf/dompdf_config.inc.php" );
  8. require_once( "vender/swift/lib/swift_required.php" );
  9.  
  10. function renderTemplate( $file, array $scope_array )
  11. {
  12.     extract( $scope_array );
  13.     ob_start();
  14.     $old = error_reporting();
  15.     error_reporting( 0 );
  16.     require( $file );
  17.     error_reporting( $old );
  18.     $template_html = ob_get_contents();
  19.     ob_end_clean();
  20.     return $template_html;
  21. }
  22.  
  23. $template_html = renderTemplate( "template.php", $_GET );
  24.  
  25. if( $view_html ) {
  26.     print $template_html;
  27.     exit();
  28. }
  29.  
  30. $dompdf = new DOMPDF();
  31. $dompdf->load_html( $template_html );
  32. $dompdf->render();
  33.  
  34. $pdf_data = $dompdf->output();
  35.  
  36. //debugging
  37. if( $view_pdf ) {
  38.     header( "Content-Type: application/pdf" );
  39.     print $pdf_data;
  40.     exit();
  41. }
  42.  
  43. $from_info = array(
  44.     "email" => "softwareelves@gmail.com",
  45.     "host" => "smtp.gmail.com",
  46.     "port" => 465,
  47.     "use_ssl" => TRUE,
  48.     "username" => "softwareelves@gmail.com",
  49.     "password" => "*PASSWORD*"
  50. );
  51.  
  52. $to_array = array( "softwareelves@gmail.com" );
  53. $subject = "SUBJECT!";
  54. $body = <<<EOS
  55. BODY BODY BODY
  56. EOS;
  57. $content_type = "text/html";
  58. $attachment_file_name = "Results.pdf";
  59.  
  60. $transport = Swift_SmtpTransport::newInstance( $from_info["host"], $from_info["port"] )
  61.     ->setUsername( $from_info["username"] )
  62.     ->setPassword( $from_info["password"] );
  63.  
  64. if( $from_info["use_ssl"] ) {
  65.     $transport->setEncryption( "ssl" );
  66. }
  67. $mailer = Swift_Mailer::newInstance( $transport );
  68. $message = Swift_Message::newInstance( $subject, $body, $content_type );
  69. $message->setFrom( $from_info["email"] );
  70. $message->setTo( $to_array );
  71. $message->attach( new Swift_Attachment( $pdf_data, $attachment_file_name, "application/pdf" ) );
  72.  
  73. $result = $mailer->send( $message );
  74. if( $result !== ( count( $to_array ) ) ) {
  75.     throw new Exception( "Failed to send to all emails" );
  76. }
  77.  
  78. ?>
Add Comment
Please, Sign In to add comment