PHP PDF attachment doesn't open in outlook

rivkamak
rivkamak used Ask the Experts™
on
I have a code to generate a PDF and then attach it in a PHP mail script.
The PDf comes through just fine when I open gmail in my browser.

If I open my gmail account through outlook, I see the little attachment symbol, but the email comes through totally blank and I can't access the PDF.
Is there something else I am supposed to change?


    $name        = "Devorah";
    $email       = "ffrid@****";
    $to          = "$name <$email>";
    $from        = "info@*****";
    $subject     = "Application information";
    $mainMessage = "Hi, here's the file.";
    $fileatt     = "application.pdf";
    $fileatttype = "application/pdf";
    $fileattname =  $_POST['lastName'] . "_" . $id .".pdf";
    $headers = "From: $from";

    // File
    $file = fopen($fileatt, 'rb');
    $data = fread($file, filesize($fileatt));
    fclose($file);
   
    // This attaches the file
    $semi_rand     = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
    $headers      .= "\nMIME-Version: 1.0\n" .
    "Content-Type: multipart/mixed;\n" .
    " boundary=\"{$mime_boundary}\"";
    $message = "This is a multi-part message in MIME format.\n\n" .
    "-{$mime_boundary}\n" .
    "Content-Type: text/plain; charset=\"iso-8859-1\n" .
    "Content-Transfer-Encoding: 7bit\n\n" .
    $mainMessage  . "\n\n";
       
    $data = chunk_split(base64_encode($data));
    $message .= "--{$mime_boundary}\n" .
    "Content-Type: {$fileatttype};\n" .
    " name=\"{$fileattname}\"\n" .
    "Content-Disposition: attachment;\n" .
    " filename=\"{$fileattname}\"\n" .
    "Content-Transfer-Encoding: base64\n\n" .
    $data . "\n\n" .
    "-{$mime_boundary}-\n";
   
    // Send the email
    mail($to, $subject, $message, $headers);
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ganesh AnandLead Technical Consultant

Commented:
What happens when you download the file from gmail and open it locally is it opening?
NorieAnalyst Assistant

Commented:
Can you save the attachment from the Outlook email?

Author

Commented:
In out look I see the paperclip but the email is blank and doesn't show any attachement.
In gmail, I can download and open it as a pdf, or open it directly from the gmail account.
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
Suggestion: why don't you use the PHPMailer library (https://github.com/PHPMailer/PHPMailer)
It will handle attachments and anything else you can think of with emails without having to mess about with message boundaries and the nuts and bolts of sending emails. It is tried and tested code used by many PHP developers.

I would suggest that you at least try sending your email with PHPMailer and see if you get different results - if you want to go with your own solution this will help to narrow down whether the problem is your implementation or something else.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial