I read a lot here and there, but somehow I could'n find a solution.
I am trying to send a PDF, generated with TCPDF, as attachment using with PHPMailer.
As soon as I try I get the message:
Warning: base64_encode() expects parameter 1 to be string, object given in C:\xampp\htdocs\pap KK\Root\phpmailer-master\src\PHPMailer.php on line 3179"", but the mail is Always send. Unfortunatelly the PDF is brocken. It is not empty. As soon as I use:
$pdf->Output('e-tickets.pdf', 'D');
it saves a perfect PDF copy,
Help, what the heck am I doing wrong... V
Here is the Code
<?php
// Include the main TCPDF library (search for installation path).
require_once('path.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('KK Abt HC');
$pdf->SetTitle('Zuweiserbrief');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
// set default header data
$pdf->SetHeaderData('');
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
//remove header footer
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
// set font
$pdf->SetFont('helvetica', '', 10);
// add a page
$pdf->AddPage();
// define some HTML content with style
require_once('print.php');
// output the HTML content
$pdf->writeHTML($html, true, false, true, false, '');
//Close and output PDF document
//$pdf->Output('no_name.pdf', 'I');
$pdf->Output('e-tickets.pdf', 'S');
//set random filename to pdf
$file_name = md5(rand()) . '.pdf';
//add mailer manually
require '../phpmailer-master/src/Exception.php';
require '../phpmailer-master/src/PHPMailer.php';
require '../phpmailer-master/src/SMTP.php';
//create new PHPMailer class
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$email = new PHPMailer(TRUE);
//create new PHPMailer object
$mail = new PHPMailer();
//SMTP configuration
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '*******';
$mail->Password = '*******';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
//PHPMailer headers
$mail->setFrom('mail.1@gmail.com', 'Robot');
$mail->addReplyTo('mail.2@gmail.com', 'Robot');
$mail->addAddress('mustermann@mail.de', 'Mustermann');
$mail->addCC('someone@yahoo.com', 'Yahoo');
$mail->addBCC('', '');
//HTML Format
$mail->isHTML(true);
$bodyContent = '<p>Hallo zusammen,</p>';
$bodyContent .= '<p>Im Anhang finden Sie bitte ein Brief</p>';
$bodyContent .= '<p>Mit freundlichen Grüßen</p>';
$bodyContent .= '<p><br></p>';
$bodyContent .= '<p>Das ist einen automatischen Mail</p>';
//Attachment
$mail->AddStringAttachment($pdf, $file_name);
//Subject
$mail->Subject = 'Email from Localhost Testing with YAHOO!';
$mail->Body = $bodyContent;
//Debugging:
/*
$mail->SMTPDebug = N; where N is (1) = client; (2) = client and server; (recommended); (3) = client, server, and connection; (4) = low-level information.
*/
$mail->SMTPDebug = 3;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
It’s because you are passing the PDF object and not the string representation of it.
Do this:
$pdfdata = $pdf->Output('e-tickets.pdf', 'S');
$mail->AddStringAttachment($pdfdata, $file_name);