SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jul 2013
    Posts
    4
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Corrupted PDF email attachment using PHP mail script

    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:

    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
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,329
    Mentioned
    191 Post(s)
    Tagged
    4 Thread(s)
    Welcome to Sitepoint Forums, @mediumblue ;
    There are innumerable PHP experts here and I can guarantee someone will be able to help you with this issue.

    After solving your problem, please be sure to continue to browse the vast collection of useful knowledge and information we have here and add your knowledge and experience.

    At all times, though, be aware of our Rules of Conduct.
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  3. #3
    SitePoint Addict bronze trophy vectorialpx's Avatar
    Join Date
    Dec 2012
    Location
    Bucharest
    Posts
    247
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    I think it may be the Content-Transfer-Encoding header.
    Try to remove it and only use Content-Type: application/pdf
    Or try to have it on 8 bits
    Source

    However, it is a good idea to use PHPMailer for this, it's easy and safe.
    I will give it a try latter and check some real code.
    Be nice to nerds. Chances are you'll end up working for one - Bill Gates
    > photos | admin panel


Tags for this Thread

Bookmarks

Posting Permissions

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