I am using FPDF and Phpmailer to generate a PDF file and sending it as an email attachment.
My script for PDF generation and phpmailer are working perfectly when I use them as independent scripts.
Now, when I combine both scripts to generate and display the PDF form (without saving it to to the filesystem) and send this PDF document as an attachment, it is not working thouht it generates the PDF and display it on browser, but does not sent it by mail.
My code is:
<?php declare(strict_types=1); use PHPMailer\PHPMailer\PHPMailer; require 'vendor/autoload.php'; $pdf = new mypdf(); $pdf->AliasNbPages(); $pdf->AddPage('P', 'A4', 0); $pdf->Header(); $pdf->headerTable(); $pdf->viewTable(); $pdf->footer(); $pdf->setMargins(20, 20, 20); $pdf->output(); $pdfString = $pdf->output(); $mail = new PHPMailer; // getting post values $first_name = $_POST['fname']; $to_email = "[email protected]"; $subject = "stationery issued"; $message = "Dear sir your requested stationery has been issued by Stationery store "; $mail->isSMTP(); // Set mailer to use SMTP $mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = '[email protected]'; // SMTP username $mail->Password = 'password'; // SMTP password $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted $mail->Port = 25; // TCP port to connect to $mail->setFrom('[email protected]', 'Your_Name'); $mail->addReplyTo('[email protected]', 'Your_Name'); $mail->addAddress($to_email); // Add a recipient $mail->addStringAttachment((string)$pdfString, 'name file.pdf'); $mail->isHTML(true); // Set email format to HTML $mail->Subject = $subject; $mail->Body = 'Dear ' . $first_name . '<p>' . $message . '</p>'; $mail->send();