Advertisement
Guest User

Untitled

a guest
Jun 17th, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. //generate PDF file for bill and send it in email
  2. public function sendInvoiceInEmail($register_no, $html_string, $guest_email_id)
  3. {
  4. //create mailer class
  5. $mail = new PHPMailer;
  6. //remove old invoice file if exist at public/doc
  7. // here i have used my path you have to use your path
  8. if(file_exists(__DIR__.'/../../public/docs/invoice.pdf')){
  9. unlink(__DIR__.'/../../public/docs/invoice.pdf');
  10. }
  11.  
  12. // You can pass a filename, a HTML string, an URL or an options array to the constructor
  13. $pdf = new Pdf([
  14. 'commandOptions' => [
  15. 'useExec' => false,
  16. 'escapeArgs' => false,
  17. 'procOptions' => array(
  18. // This will bypass the cmd.exe which seems to be recommended on Windows
  19. 'bypass_shell' => true,
  20. // Also worth a try if you get unexplainable errors
  21. 'suppress_errors' => true,
  22. ),
  23. ],
  24. ]);
  25. $globalOptions = array(
  26. 'no-outline'
  27. );
  28.  
  29. $pdf->setOptions($globalOptions);
  30.  
  31. $pdf->addPage($html_string);
  32. //for Windows in my system i have setup wkhtmltopdf here
  33. $pdf->binary = '"C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe"';
  34. // for linux when you will host you have to setp on linux abd comment windows path and uncomment below line
  35. //$pdf->binary = '/home/username/wkhtmltox/bin/wkhtmltopdf';
  36. if (!$pdf->saveAs(__DIR__.'/../../public/docs/invoice.pdf')) {
  37. throw new Exception('Could not create PDF: '.$pdf->getError());
  38. }
  39. //now send email and attach invoice.pdf file from public/doc/invoice.pdf
  40.  
  41.  
  42.  
  43. //$mail->SMTPDebug = 3; // Enable verbose debug output
  44. $mail->isSMTP(); // Set mailer to use SMTP
  45. $mail->Host = 'hostname'; // Specify main and backup SMTP servers
  46. $mail->SMTPAuth = true; // Enable SMTP authentication
  47. $mail->Username = 'username'; // SMTP username
  48. $mail->Password = 'password'; // SMTP password
  49. $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
  50. $mail->Port = 587; // TCP port to connect to
  51. $mail->setFrom('from_email_address', 'From Name',FALSE);
  52. $mail->addAddress('to_email_address'); // Add a recipient
  53.  
  54. $mail->addAttachment(__DIR__.'/../../public/docs/invoice.pdf'); // Add attachments
  55.  
  56. $mail->isHTML(true); // Set email format to HTML
  57.  
  58. $mail->Subject = 'Subject';
  59. $mail->Body = 'email message body';
  60.  
  61. $is_email_sent = false;
  62. if(!$mail->send()) {
  63. // echo 'Message could not be sent.';
  64. // echo 'Mailer Error: ' . $mail->ErrorInfo;
  65. $is_email_sent = false;
  66. } else {
  67. // echo 'Message has been sent';
  68. $is_email_sent = true;
  69. }
  70.  
  71. return true;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement