I'm using html2pdf to convert a html page to a pdf and I want to send it as a mail. Using the code below, I was able to create the pdf and display it properly on the page (using Output('name.pdf')). I found that in order to send this pdf as attachment in a mail, I need to add a second argument as true or as 'S', so here is the code:
$pdf = '';
try
{
// init HTML2PDF
$html2pdf = new HTML2PDF('P', 'A4', 'en', true, 'UTF-8', array(0, 0, 0, 0));
// display the full page
$html2pdf->pdf->SetDisplayMode('fullpage');
// get the HTML
ob_start();
include('invoice_html.php');
$content = ob_get_clean();
// convert
$html2pdf->writeHTML($content);
// send the PDF
$pdf = $html2pdf->Output('', true);
}
catch(HTML2PDF_exception $e) {
echo $e;
exit;
}
$mail = new PHPMailer();
$mail->setFrom('senderSMTP@yahoo.com', 'sender');
$mail->addAddress('test@gmail.com', 'test');
$mail->Subject = 'TestMail';
$mail->addAttachment($pdf, 'file.pdf');
$mail->Body = 'TestMessage';
if($mail->send())
{
echo 'success';
}
else
{
echo $mail->ErrorInfo;
}
Also, to add, I set the sendmail on wamp. Whenever I access the page, I get success and the mail is sent successful, now my problem is the attachment part, because I'm getting the email with body and subject, but there is no attachment pdf to it. Did I do something wrong on the output part? (both true and 'S' give the same thing). Thanks. PS: I'm using yahoo as smtp to send email and gmail to receive.