Avatar of rivkamak
rivkamak
Flag for United States of America asked on

PHP PDF attachment doesn't open in outlook

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);
OutlookPHP

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon
Ganesh Anand

What happens when you download the file from gmail and open it locally is it opening?
Norie

Can you save the attachment from the Outlook email?
rivkamak

ASKER
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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.