Search code examples
emailpdfattachmentphpmailerfpdf

FPDF PDF file cannot be opened after email using PHPMailer


I am trying to create a PDF file using FPDF and send it via e-mail using PHPMailer. It did send the file, but for some reasons I cannot open the file, either from my email or using Adobe Reader (which gives me something like the file is damaged).

This is my code

require('fpdf.php');
require 'PHPMailerAutoload.php';
header("Access-Control-Allow-Origin: *");
$pdf = new FPDF();
$pdf->AddPage();

$pdf->SetFont('Arial','UB',28);
$pdf->Cell(0,10,"My Profile",0,1,'C');
$pdf->Ln();

$mail = new PHPMailer();
$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 = 'example@gmail.com';                 // SMTP username
$mail->Password = 'password';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
// a random hash will be necessary to send mixed content
$separator = md5(time());

// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;

// main header
$headers  = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";

// no more headers after this, we start the body! //

$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;

// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;

// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";

$pdfdoc = $pdf->Output('','S');
$attachment = chunk_split(base64_encode($pdfdoc));
//Set who the message is to be sent from
$mail->setFrom('example@gmail.com', 'Baby Diary');
//Set who the message is to be sent to
$mail->addAddress('example2@gmail.com', 'haha');
//Set the subject line
$mail->Subject = 'PHPMailer mail() test';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AddStringAttachment($attachment, 'doc.pdf', 'base64', 'application/pdf');

if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

It does give me output when I use $pdf->Output(), which hints to me that the problem should not be in FPDF side. Any suggestion will be much appreciated.


Solution

  • Your problem is with phpmailer and not with fpdf. Try using the standard PHPMailer class methods.

    If I was you I'd try to rework your code from the attachement example included in the PHPMailer doc samples. And particularly I'd stick to using the PHPMailer methods rather that doing my own smtp formating.

    So first save your pdf to file, and then try attaching the file with $mail->addAttachment() . And let PHPMailer handle the formating of the mail.

    So you might end up with something like this code below (I haven't run it, you'll probably need to fine tune it) :

    require('fpdf.php');
    require 'PHPMailerAutoload.php';
    header("Access-Control-Allow-Origin: *");
    $pdf = new FPDF();
    $pdf->AddPage();
    
    $pdf->SetFont('Arial','UB',28);
    $pdf->Cell(0,10,"My Profile",0,1,'C');
    $pdf->Ln();
    
    $pdfoutputfile = '/some-tmp-dir/temp-file.pdf';
    $pdfdoc = $pdf->Output($pdfoutputfile, 'F');
    
    // We're done with the pdf generation
    // now onto the email generation
    
    $mail = new PHPMailer();
    $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 = 'example@gmail.com';   // SMTP username
    $mail->Password = 'password';            // SMTP password
    $mail->SMTPSecure = 'tls';       // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;
    $separator = md5(time());        // a random hash will be necessary to send mixed content
    
    //Set who the message is to be sent from
    $mail->setFrom('example@gmail.com', 'From Test address');
    
    //Set who the message is to be sent to
    $mail->addAddress('example2@gmail.com', 'to test address');
    
    //Set the subject line
    $mail->Subject = 'Test message with attachement';
    $mail->Body    = 'Test message with attached files.';
    
    $mail->AddAttachment($pdfoutputfile, 'my-doc.pdf');
    
    if (!$mail->send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message sent!";
    }