Sending multiple word documents as attachments in PHP
<p>I have a function to send an email with multiple word documents as attachments. Only problem is that no matter how many documents I send, only 1 shows up in the email, and also, it shows up as corrupted. The file path name is correct, I have tested this, but when I try to open the document from the email, Microsoft word complains about corrupted file. Somebody please tell me what I am doing wrong?</p>

<pre><code>function mail_attachment_multiple($to, $subject, $message, $attachment, $from){//sends email as an attachment. attachment parameter is the path to file

$fileatt_type = "application/msword"; // File Type
$email_from = $from; // Who the email is from
$email_subject =  $subject; // The Subject of the email
$email_txt = $message; // Message that the email has in it
$email_to = $to; // Who the email is to
$headers = "From: ".$email_from;
$msg_txt="\n\n You have recieved a new attachment message from $from";

$email_message = ""; 

//loop through array: $key = filename; $value = filenamePATH
foreach($attachment as $key =&gt; $value){
    $fileatt_name = $key; //name of file
    $fileatt_path = $value; // file path (http://www.example.com/filePath)

    $file = fopen($fileatt_path,'rb');
        $temp = get_headers($fileatt_path, 1);
    $file_size = $temp['Content-Length'];           
    $data = fread($file, $file_size); //filesize($fileatt_path)
    fclose($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}\"";
    $email_txt .= $msg_txt;
    $email_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" .
    $email_txt . "\n\n";
    $data[$key] = chunk_split(base64_encode($data));        

    $email_message .= "--{$mime_boundary}\n" .
              "Content-Type: {$fileatt_type};\n" .
              " name=\"{$fileatt_name}\"\n" .
              //"Content-Disposition: attachment;\n" .
              //" filename=\"{$fileatt_path}\"\n" .
              "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" ."--{$mime_boundary}--\n";
}

return @mail($email_to, $email_subject, $email_message, $headers);?&gt;
</code></pre>

<p>Thanks for the help!</p>
