Page 1 of 1

mail() attachments

Posted: Fri Sep 26, 2008 10:37 am
by ahs10
i get the email, the html links inside the message work, the file attaches with the correct name and filesize, but when you try to open the attachment it says it's corrupt. can someone help me please?

Code: Select all

$email_body = nl2br(html_entity_decode($_POST['email_body']));
        
    $message = "--mimeBoundary\n"
        . "Content-Type: text/html; charset=UTF-8; format=flowed\n"
        . "Content-Transfer-Encoding: 8bit\n"
        . "<base href=http:// />" . $email_body;
        
    if ($num_attachments != 0) {
        $message .= "\n";
        while ($row = mysql_fetch_array($attachment_query)) {
            $message .= "--mimeBoundary\n"
                . "Content-Type: " . $row['ftype'] . "; name=" . $row['fname'] . "\n"
                . "Content-Transfer-Encoding: base64\n"
                . "Content-Disposition: attachment\n"
                . base64_encode($row['fdata']);
        }
        $message .= "\n" . "--mimeBoundary--";
    }
        
    $encoded_subject = "=?UTF-8?B?" . base64_encode($subject) . "?=\n";
    $headers = "From: " . $userEmail . "\n"
    //      . "Cc: " . $cc . "\n"
    //      . "Bcc: " . $bcc . "\n"
        . "Content-Type: multipart/mixed; boundary=\"mimeBoundary\"\n"
        . "MIME-Version: 1.0\n"
        . "X-Mailer: PHP\n";
    mail($to,$encoded_subject,$message,$headers);

Re: mail() attachments

Posted: Fri Sep 26, 2008 12:04 pm
by ahs10
i found a solution, but i don't know if it's a fix. if i change...

Code: Select all

. "Content-Type: " . $row['ftype'] . "; name=" . $row['fname'] . "\n"
to...

Code: Select all

. "Content-Type: "binary/octet-stream; name=" . $row['fname'] . "\n"
and this....

Code: Select all

. base64_encode($row['fdata']);
to this...

Code: Select all

. chunk_split(base64_encode($row['fdata']));
it all works properly.

basically see that i am changing the file type of all files to one generic type and i'm splitting the data of the file up into smaller parts. how does this help? why didn't the first way work?