$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(); }
source share