How to use TCPDF with PHP mail function

$to = ' my@email.ca '; $subject = 'Receipt'; $repEmail = ' rep@sales.ca '; $fileName = 'receipt.pdf'; $fileatt = $pdf->Output($fileName, 'E'); $attachment = chunk_split($fileatt); $eol = PHP_EOL; $separator = md5(time()); $headers = 'From: Sender <'.$repEmail.'>'.$eol; $headers .= 'MIME-Version: 1.0' .$eol; $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\""; $message = "--".$separator.$eol; $message .= "Content-Transfer-Encoding: 7bit".$eol.$eol; $message .= "This is a MIME encoded message.".$eol; $message .= "--".$separator.$eol; $message .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol; $message .= "Content-Transfer-Encoding: 8bit".$eol.$eol; $message .= "--".$separator.$eol; $message .= "Content-Type: application/pdf; name=\"".$fileName."\"".$eol; $message .= "Content-Transfer-Encoding: base64".$eol; $message .= "Content-Disposition: attachment".$eol.$eol; $message .= $attachment.$eol; $message .= "--".$separator."--"; if (mail($to, $subject, $message, $headers)){ $action = 'action=Receipt%20Sent'; header('Location: ../index.php?'.$action); } else { $action = 'action=Send%20Failed'; header('Location: ../index.php?'.$action); } 

I use TCPDF for a short time to create PDF files from forms. It works quite well, and part of PHP has not changed. Now I want to send these PDF files to my email account.

Email really works with this encoding and attaches a PDF. The problem is that it is just an empty PDF of about 100 bytes in size. Which, of course, is not a valid PDF and has nothing to do with form responses.

I am really not familiar with attaching files to an email in PHP, and any help in solving this problem would be greatly appreciated.

Update

Since it seems like a few people are looking at this, I will post my current solution. This involves loading PHPMailer, as suggested below. I started with the output line for TCPDF.

 $attachment = $makepdf->Output('filename.pdf', 'S'); SENDmail($attachment); function SENDmail($pdf) { require_once('phpmailer/class.phpmailer.php'); $mailer = new PHPMailer(); $mailer->AddReplyTo(' reply@to.ca ', 'Reply To'); $mailer->SetFrom(' sent@from.ca ', 'Sent From'); $mailer->AddReplyTo(' reply@to.ca ', 'Reply To'); $mailer->AddAddress(' send@to.ca ', 'Send To'); $mailer->Subject = 'Message with PDF'; $mailer->AltBody = "To view the message, please use an HTML compatible email viewer"; $mailer->MsgHTML('<p>Message contents</p>')); if ($pdf) {$mailer->AddStringAttachment($pdf, 'filename.pdf');} $mailer->Send(); } 
+4
source share
2 answers

You have two options. You can save the PDF to a file and attach the file or output it as a string. I find the output of the string to be preferable:

 $pdfString = $pdf->Output('dummy.pdf', 'S'); 

The file name is ignored, as it simply returns the encoded string. Now you can include the string in your email address. I prefer to use PHPMailer when dealing with such attachments. Use the AddStringAttachment method for PHPMailer to accomplish this:

 $mailer->AddStringAttachment($pdfString, 'some_filename.pdf'); 
+13
source

I tried several alternatives. The only way that worked was that I saved the PDF file in a folder and then sent it by email.

 $pdf->Output("folder/filename.pdf", "F"); //save the pdf to a folder require_once('phpmailer/class.phpmailer.php'); //where your phpmailer folder is $mail = new PHPMailer(); $mail->From = "email.com"; $mail->FromName = "Your name"; $mail->AddAddress(" email@yahoo.com "); $mail->AddReplyTo(" email@gmail.com ", "Your name"); $mail->AddAttachment("folder/filename.pdf"); // attach pdf that was saved in a folder $mail->Subject = "Email Subject"; $mail->Body = "Email Body"; if(!$mail->Send()) { echo "Message could not be sent. <p>"; echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent"; } echo 'sent email and attachment'; 
0
source

All Articles