Joseph Longo
asked on
FPDF & PHP Mailer - Save PDF & Attach In An Email
Hello Experts,
Perhaps, I am going about this incorrectly. I am open to ideas on how to do this.
I have a PHP program that generates a PDF file, dynamically using FPDF. Once the user clicks the submit button, at run time, I need to be able to do the following nearly simultaneously:
Save the PDF file in a specified location;
Grab the freshly generated PDF;
Attach it to an email; and
Send the email to specified users.
First, is this even possible?
I was thinking about using PHP Mailer in combination with FPDF.
Any ideas?
Perhaps, I am going about this incorrectly. I am open to ideas on how to do this.
I have a PHP program that generates a PDF file, dynamically using FPDF. Once the user clicks the submit button, at run time, I need to be able to do the following nearly simultaneously:
Save the PDF file in a specified location;
Grab the freshly generated PDF;
Attach it to an email; and
Send the email to specified users.
First, is this even possible?
I was thinking about using PHP Mailer in combination with FPDF.
Any ideas?
Here is a working sample that uses FPDF and PHPMailer
<?php
// PUT CONFIG INFORMATION IN THIS FILE
require_once('config.php');
// INCLUDE THE FPDF LIBRARIES
require_once('fpdf/fpdf.php');
// INCLUDE THE PHPMAiler LIBRARIES
require_once('PHPMailer/phpmailer.class.php');
require_once('PHPMAiler/class.smtp.php');
// WHERE DO YOU WANT TO SAVE THE FILE
$dest = 'sample.pdf';
// CREATE AND SAVE THE PDF DOCUMENT
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
// SEND TO A LOCAL FILE
$pdf->Output('F', $dest);
// CREATE THE MAIL
$mail = new PHPMailer(true);
try {
// SETUP FOR SMTP
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = $host; // 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
//Recipients
$mail->setFrom($from, $fromname);
$mail->addAddress($to, $toname); // Add a recipient
$mail->addReplyTo($from, $fromname);
//Attachments
$mail->addAttachment($dest);
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = 'PDF Attached';
$mail->AltBody = 'PDF Attached';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
Sample config.php<?php
$to = 'recipient@someaddress.com';
$from = $to;
$username = $to;
$password = 'password';
$host = 'your.smtphost.com';
$fromname = 'Your Name';
$toname = $fromname;
$subject = 'EE Test';
ASKER
Braving Hurricane Irma at the moment, as I live in Tampa, FL. Once life returns back to normal, I'll test these solutions out.
Take your time - hoping for the best for you and all those caught in Irma's path.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
ref:http://www.fpdf.org/en/doc/output.htm
Then use sendmail or similar email system installed on your server to send file as an attachment.