#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Posts
    34
    Rep Power
    8

    Send mail with attachment - file getting corrupted!


    Hi all, I am using a send mail script I found on a tutorial as I have never had to deal with attaching files in PHP.

    It seems to be working mostly as it should - I am getting the email and there is an attachment (A PDF file) in the email. However, the attached PDF is smaller than the one on the server, as far as file size - I.g. 437k on server, 427k in the email attachment.

    When I download the file from my email and attempt to open it, adobe reader says the file is corrupted and cannot be downloaded. I have checked the file on the server and it works just fine.

    Here is the code I am using, and I hope someone can possibly help me figure out why the files are being corrupted:

    PHP Code:
    while ($row=mysql_fetch_array($result2))
            {
            
    $email "xxxxx@xxxxxxxxxxxxx.com";
            
    $to $row['email'];
            
    $from "xxxxxxxxx.com ";
            
    $subject "Your Daily Puzzle";
            
    $fileatt "../../puzzles/".$attach;
            
    $fileatttype "application/pdf";
            
    $fileattname date('Y-m-d').".pdf";
            
    $headers "From: $from";
            echo 
    "<br />File Attached: <a href='../../puzzles/$fileatt'>$fileatt</a><br />";
            
    // File
            
    $file fopen($fileatt'rb');
            
    $data fread($filefilesize($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" .
            
    $message .= "\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
    if(mail($to$subject$message$headers)) {

        echo 
    "The email was sent.";

    }
    else {

        echo 
    "There was an error sending the mail.";

           }

  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Wow. I know that PHP people for some strange reason love to fumble with the low-level internals of emails. But I've never seen anybody actually generate their own mutlipart boundaries.

    I mean, maybe you have your reasons for doing that. But in the rest of the world, people use mail libraries like PHPMailer. I wouldn't be surprised if this also fixed your problem.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo