Mike Ezzati
Mike Ezzati

Reputation: 3166

Email TCPDF Generated PDF file with php mail function

PDF file is being generated by tcpdf library but I cant attach it to email. It sends Email with an empty 1kb PDF file.

$to = '[email protected]';
$subject = 'Receipt';
$repEmail = '[email protected]';

$fileName = 'receipt.pdf';
$fileatt = $pdf->Output($fileName, 'S');
$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)){
echo "Email sent";
}

else {
echo "Email failed";
}

I know its much easier with phpmailer but I'm required to do it with mail function and dont use any library. Any help is appreciated.

Upvotes: 1

Views: 8848

Answers (1)

developer
developer

Reputation: 41

The solution is on link that vodich sent.

Your code is correct. You have to change one line:

$fileatt = $pdf->Output($fileName, 'S');

with

$fileatt = $pdf->Output($fileName, 'E');

Upvotes: 0

Related Questions