• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 84
  • Last Modified:

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);
0
rivkamak
Asked:
rivkamak
1 Solution
 
Ganesh Kumar ASr Infrastructure SpecialistCommented:
What happens when you download the file from gmail and open it locally is it opening?
0
 
NorieVBA ExpertCommented:
Can you save the attachment from the Outlook email?
0
 
rivkamakAuthor 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.
0
 
Julian HansenCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now