Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //generate PDF file for bill and send it in email
- public function sendInvoiceInEmail($register_no, $html_string, $guest_email_id)
- {
- //create mailer class
- $mail = new PHPMailer;
- //remove old invoice file if exist at public/doc
- // here i have used my path you have to use your path
- if(file_exists(__DIR__.'/../../public/docs/invoice.pdf')){
- unlink(__DIR__.'/../../public/docs/invoice.pdf');
- }
- // You can pass a filename, a HTML string, an URL or an options array to the constructor
- $pdf = new Pdf([
- 'commandOptions' => [
- 'useExec' => false,
- 'escapeArgs' => false,
- 'procOptions' => array(
- // This will bypass the cmd.exe which seems to be recommended on Windows
- 'bypass_shell' => true,
- // Also worth a try if you get unexplainable errors
- 'suppress_errors' => true,
- ),
- ],
- ]);
- $globalOptions = array(
- 'no-outline'
- );
- $pdf->setOptions($globalOptions);
- $pdf->addPage($html_string);
- //for Windows in my system i have setup wkhtmltopdf here
- $pdf->binary = '"C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe"';
- // for linux when you will host you have to setp on linux abd comment windows path and uncomment below line
- //$pdf->binary = '/home/username/wkhtmltox/bin/wkhtmltopdf';
- if (!$pdf->saveAs(__DIR__.'/../../public/docs/invoice.pdf')) {
- throw new Exception('Could not create PDF: '.$pdf->getError());
- }
- //now send email and attach invoice.pdf file from public/doc/invoice.pdf
- //$mail->SMTPDebug = 3; // Enable verbose debug output
- $mail->isSMTP(); // Set mailer to use SMTP
- $mail->Host = 'hostname'; // Specify main and backup SMTP servers
- $mail->SMTPAuth = true; // Enable SMTP authentication
- $mail->Username = 'username'; // SMTP username
- $mail->Password = 'password'; // SMTP password
- $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
- $mail->Port = 587; // TCP port to connect to
- $mail->setFrom('from_email_address', 'From Name',FALSE);
- $mail->addAddress('to_email_address'); // Add a recipient
- $mail->addAttachment(__DIR__.'/../../public/docs/invoice.pdf'); // Add attachments
- $mail->isHTML(true); // Set email format to HTML
- $mail->Subject = 'Subject';
- $mail->Body = 'email message body';
- $is_email_sent = false;
- if(!$mail->send()) {
- // echo 'Message could not be sent.';
- // echo 'Mailer Error: ' . $mail->ErrorInfo;
- $is_email_sent = false;
- } else {
- // echo 'Message has been sent';
- $is_email_sent = true;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement