3
votes

m new to php. I need to send the email with the pdf attachment.I m able to send an email with the attachment. But unable to open the pdf. I get the some error like this

Store information to the database(done), Sends the staff an email with the new customer information (done), and Send the customer a "thank you message" email with a pdf file attachment (not working). I mean, the customer does receive an email, but when he/she opens the pdf file, I get the following error message:

"Acrobat could not oen 'file_name' because it is either not a supported file type or because the file has been damaged(for example, it was sent as an email attachment and wasn't correctly decoded)..."

If someone could help me resolve this problem, that would be great. Thanks!

Here is my code:

$to = '[email protected], ' . $Email;
$subject = 'ABC :: Admission Form Details';
$repEmail = '[email protected]';

$fileName = 'ABC-Admission.pdf';
$fileatt = $pdf->Output($fileName, 'E');
$attachment = chunk_split($fileatt);
$eol = PHP_EOL;
$separator = md5(time());

$headers = 'From: Principal abc <'.$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 .= "Thanks for filling online application form. Your online admission registration number is E0000". mysql_insert_id() . "." .$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";
2
Try to replace $attachment = chunk_split($fileatt); with $attachment = chunk_split(base64_encode($fileatt));. Other than that, use PHPMailer, it is much much much much ... much better and easier than php mail() function. - dkasipovic
How to use PHPMailer. - user3377635

2 Answers

0
votes

I would suggest using a framework to do the heavy lifting for you. Take a look at Zend framework 2. What you are trying to achieve can be done with a few lines of code.

The documentation is pretty thorough. Have a look at the quick start guide on this page.

http://framework.zend.com/manual/2.3/en/modules/zend.mail.message.html

As others have suggested using the mail functions directly is cumbersome and error prone and bound to just cause you more issues.

0
votes

only change application/pdf to application/octet-stream