www.webdeveloper.com
Results 1 to 3 of 3

Thread: Corrupted PDF email attachment using PHP mail script and multi-part MIME

  1. #1
    Join Date
    Jul 2013
    Posts
    3

    Corrupted PDF email attachment using PHP mail script and multi-part MIME

    Hi all,

    I am hoping to find an experienced PHP developer to help me troubleshoot an attachment issue with my email script. The user enters his/her email address as part of a form input and this script is supposed to attach a remotely-hosted file (PDF or ZIP) along with some body text. The same script works great for the ZIP file attachment but NOT for PDF. The script does deliver the PDF and the file properly shows up in the email as an attachment -- the problem is when the user attempts to 'open' the PDF. The Adobe Acrobat Reader populates an error stating that the file is corrupted -- which could be do to an encoding issue while sent as an attachment. However, I'm using base64_encoding and I'm using 'application/pdf' as the file type. I admit I'm a bit of a beginner at discerning PHP code -- so I hope that if the community here takes a look at my code line by line -- they can advise me where I am going wrong. I have been doing all kinds of research on this for the past week -- and nothing I've changed has seemed to make a difference. Any help would be greatly appreciated. I'm going to try and include the code below:

    PHP Code:
    <?php
    $file_path 
    "http://www.mitchellmetals.net/images/drawings_walkway/w1.pdf"// server path where file is placed
    $file_path_type "application/pdf"// File Type
    $file_path_name "w1.pdf"// this file name will be used at reciever end 
    $headers .= 'Content-type: text/html; charset=utf-8' "\r\n"
    $from "noreply@mitchellmetals.net"// E-mail address of sender
    $to $_POST['email']; // E-mail address of reciever
    $subject "Drawing from Mitchell Metals"// Subject of email
    $message "Thank you for downloading the <b>Walkway Canopy with Beams PDF file</b> from mitchellmetals.net. For a free estimate or budget number on a canopy, please email us at <a href='mailto:sales@mitchellmetals.net'>sales@mitchellmetals.net</a>. Please call 770-431-7300 if Mitchell Metals can assist you with your next project."
     
    $headers "From: ".$from
     
    $file fopen($file_path,'rb');
    $data fread($file,filesize($file_path));
    fclose($file); 
     
    $rand md5(time());
    $mime_boundary "==Multipart_Boundary_x{$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/html; charset=\"iso-8859-1\"\n" .
    "Content-Transfer-Encoding: 7bit\n\n" .
    $message .= "\n\n"


    $data chunk_split(base64_encode($data)); 
     
    $message .= "--{$mime_boundary}\n" .
    "Content-Type: {$file_path_type};\n" .
    " name=\"{$file_path_name}\"\n" .
    "Content-Disposition: attachment;\n" .
    " filename=\"{$file_path_name}\"\n" .
    "Content-Transfer-Encoding: base64\n" .
    $data .= "\n\n" .
    "--{$mime_boundary}--\n";  
     
    if(@
    mail($to$subject$message$headers)) {
    echo 
    "File sent!";
     
    } else {
    echo 
    'Failed';
    }
    ?>

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,912
    I'd like to help you, but that fiddly stuff always drives me crazy, so I just use phpMailer or SwiftMailer and call the applicable methods to attach a file.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jul 2013
    Posts
    3
    Quote Originally Posted by NogDog View Post
    I'd like to help you, but that fiddly stuff always drives me crazy, so I just use phpMailer or SwiftMailer and call the applicable methods to attach a file.
    Thanks, NogDog. I have a unique circumstance where I am adding this as custom code on a mobile site creation platform. So the page where this script is called is hosted on a different server (mobile software's cloud server) than where the script and PDF live (client-server) -- so I wasn't sure how to integrate a library like phpMailer or SwiftMailer -- but thank you for the information. I'm stuck with debugging this manual code, I'm afraid. I have a total of 40 possible PDF files that can be selected on the mobile site that folks can email to themselves -- but I am certain if I tweak this one 'template' file, I can make the necessary adjustments to the remaining 39 files on the client's server. Certainly, not my first choice. Thanks again for your response.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles