Juned Ansari
Juned Ansari

Reputation: 5275

Generate PDF output and send it to email using phpmailer

I need to generate pdf output and send it to email, email i am getting is without attachment I need to generate pdf output and send it to email, email i am getting is without attachment

require('lib/FPDF/fpdf.php'); 
require 'lib/PHPMailer/PHPMailerAutoload.php';
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'hello india');

$mail = new PHPMailer;
$mail->isSMTP();                                // Set mailer to use SMTP
$mail->Host = SmtpServer;                       // SMTP server
$mail->SMTPAuth = true;                         // Enable SMTP authentication
$mail->Username = SmtpUsername;                 // SMTP username
$mail->Password = SmtpPassword;                 // SMTP password
$mail->SMTPSecure = 'tls';                      // Enable TLS encryption, `ssl` also accepted
$mail->From = FromEmail;
$mail->Port = 587;                              // SMTP Port
$mail->FromName  = 'testing';

$mail->Subject   = $subject;
$mail->Body      = $body;
$mail->AddAddress($emails);
$mail->AddAttachment($pdf->Output("Test Invoice.pdf","F"), '', $encoding = 'base64', $type = 'application/pdf');
return $mail->Send();

Upvotes: 2

Views: 11164

Answers (1)

Juned Ansari
Juned Ansari

Reputation: 5275

require('lib/FPDF/fpdf.php'); 
require 'lib/PHPMailer/PHPMailerAutoload.php';
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'hello india');
$pdf->Output("F",'./uploads/OrderDetails.pdf'); 
$mail = new PHPMailer;
$mail->isSMTP();                                // Set mailer to use SMTP
$mail->Host = SmtpServer;                       // SMTP server
$mail->SMTPAuth = true;                         // Enable SMTP authentication
$mail->Username = SmtpUsername;                 // SMTP username
$mail->Password = SmtpPassword;                 // SMTP password
$mail->SMTPSecure = 'tls';                      // Enable TLS encryption, `ssl` also accepted
$mail->From = FromEmail;
$mail->Port = 587;                              // SMTP Port
$mail->FromName  = 'testing';

$mail->Subject   = $subject;
$mail->Body      = $body;
$mail->AddAddress($emails);
$mail->AddAttachment("./uploads/OrderDetails.pdf", '', $encoding = 'base64', $type = 'application/pdf');
return $mail->Send();

if You dont want to save file to server, you can directly send pdfoutput to email like this

    require('lib/FPDF/fpdf.php'); 
    require 'lib/PHPMailer/PHPMailerAutoload.php';
    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',16);
    $pdf->Write(5,'Hello India');

    $mail = new PHPMailer;
    $mail->isSMTP();                                // Set mailer to use SMTP
    $mail->Host = SmtpServer;                       // SMTP server
    $mail->SMTPAuth = true;                         // Enable SMTP authentication
    $mail->Username = SmtpUsername;                 // SMTP username
    $mail->Password = SmtpPassword;                 // SMTP password
    $mail->SMTPSecure = 'tls';                      // Enable TLS encryption, `ssl` also accepted
    $mail->From = FromEmail;
    $mail->Port = 587;                              // SMTP Port
    $mail->FromName  = 'testing';

    $mail->Subject   = $subject;
    $mail->Body      = $body;
    $mail->AddAddress($emails);
    $mail->addStringAttachment($pdf->Output("S",'OrderDetails.pdf'), 'OrderDetails.pdf', $encoding = 'base64', $type = 'application/pdf');
    return $mail->Send();

Upvotes: 5

Related Questions