Search code examples
phpfpdf

FPDF generates a ".dat" file when sending as attachment


I am using fPDF to generate tickets for booking. however when I attach the file using PHPmailer Plugin. I am receiving a ".dat" file.

How do I exactly get the pdf file that was generated by fPDF..

thanks

BTW below is my code.

 $pdf_filename = tempnam(sys_get_temp_dir(), "pdf");
 $pdf = new PDF();
 $pdf->AliasNbPages();
 $pdf->AddPage();
 $pdf->Ln(5);
 $pdf->Cell(10);
 $pdf->SetFont('Times','B',10);
 $pdf->Cell(45,5,"Project Reference Number:",0,0,'L');
 $pdf->SetFont('Times','',10);
 $pdf->Cell(45,5,"$refno",0,0,'L');
 $pdf->SetFont('Times','B',10);
 $pdf->Cell(45,5,"Date Booked:",0,0,'R');
 $pdf->SetFont('Times','',10);
 $pdf->Cell(35,5,"$datetoday",0,0,'R');  
 .
 .
 .
 .                                                     
 $pdf->Cell(10);
 $pdf->SetFont('Times','B',9);
 $pdf->SetTextColor(255,0,0);
 $pdf->MultiCell(170,5,"$remarks",1,'L',0);
 $pdf->SetTextColor(0,0,0);

 $pdf->Output();
 $pdf->Output($pdf_filename,"F");
 $pdfpath = $pdf_filename;




                                                    //send mail to
  require 'phpmailer/PHPMailerAutoload.php';

  $mail = new PHPMailer;
  $mail->Host = 'smtp.gmail.com'; //smtp2.example.com';
  $mail->SMTPAuth = true;                               
  $mail->Username = 'email@gmail.com';              
  $mail->Password = 'password';                    
  $mail->SMTPSecure = 'ssl';                           
  $mail->Port = 465;                                   
  $mail->From = 'site-admin@the-inspection-company.com';
  $mail->FromName = 'The Inspection Company Ltd';
  $mail->addAddress('email2@gmail.com','Receiver');  

  $qry=$handler->prepare("SELECT * FROM uploads WHERE upload_bookid = ? AND upload_compid = ?");
   $qry->execute(array($intrno,$id));
   $row = $qry->rowCount();

   while($row = $qry->fetch(PDO::FETCH_ASSOC)){
   $path = $row['upload_path'];
   $mail->addAttachment($path);        
   }

   $mail->addAttachment($pdfpath);                             
   $mail->isHTML(true);
   $mail->Subject = "TIC - New Booking Request";
   $mail->Body = "This is a test email message"
   $mail->send();

Solution

  • This code works now, after giving the pdf a temporary name and generating it, I renamed the file and attach it to phpmailer

    $pdf_filename = tempnam(sys_get_temp_dir(), "pdf");
    $pdf = new PDF();
    $pdf->AliasNbPages();
    $pdf->AddPage();
    $pdf->Ln(5);
    $pdf->Cell(10);
    $pdf->SetFont('Times','B',10);
    $pdf->Cell(45,5,"Project Reference Number:",0,0,'L');
    $pdf->SetFont('Times','',10);
    $pdf->Cell(45,5,"$refno",0,0,'L');
    .
    .
    .
    .                                                     
    $pdf->Cell(10);
    $pdf->SetFont('Times','B',9);
    $pdf->SetTextColor(255,0,0);
    $pdf->MultiCell(170,5,"$remarks",1,'L',0);
    $pdf->SetTextColor(0,0,0);
    $pdf_filename.='.pdf';
    $pdf->Output();
    $pdf->Output($pdf_filename,"F");
    $pdfpath = $pdf_filename;
    
    
    
    
      //send mail to
     require 'phpmailer/PHPMailerAutoload.php';
    
     $mail = new PHPMailer;
     $mail->Host = 'smtp.gmail.com'; //smtp2.example.com';
     $mail->SMTPAuth = true;                               
     $mail->Username = 'email@gmail.com';              
     $mail->Password = 'password';                    
     $mail->SMTPSecure = 'ssl';                           
     $mail->Port = 465;                                   
     $mail->From = 'site-admin@the-inspection-company.com';
     $mail->FromName = 'The Inspection Company Ltd';
     $mail->addAddress('email2@gmail.com','Receiver');  
    
     $qry=$handler->prepare("SELECT * FROM uploads WHERE upload_bookid = ? AND upload_compid = ?");
     $qry->execute(array($intrno,$id));
     $row = $qry->rowCount();
    
     while($row = $qry->fetch(PDO::FETCH_ASSOC)){
     $path = $row['upload_path'];
     $mail->addAttachment($path);        
     }
    
     $mail->addAttachment($pdfpath);                             
     $mail->isHTML(true);
     $mail->Subject = "TIC - New Booking Request";
     $mail->Body = "This is a test email message"
     $mail->send();