After googling several posts about how to add and send multiple email attachments using PHP, I found it was uneasy to accomplish it by using the methods from top 20 results on google, either they can show the attachments properly nor can be recognized correctly by gmail. Finally the following codes can correctly add multiple attachments in email.
1. If you want to use array, use the following codes in your html file:
<input type='file' name='uploadedFile[]’ />
2.in the upload function:
for($i=0; $i < count($_FILES['uploadedFile']['name']); $i++){ if(is_uploaded_file($_FILES['uploadedFile']['tmp_name'][$i]) && !empty($_FILES['uploadedFile']['size'][$i]) && !empty($_FILES['uploadedFile']['name'][$i])){ $attach ='yes'; $end =''; $handle =fopen($_FILES['uploadedFile']['tmp_name'][$i], 'rb'); $f_contents =fread($handle, $_FILES['uploadedFile']['size'][$i]); $attachment[]=chunk_split(base64_encode($f_contents)); fclose($handle); $ftype[] =$_FILES['uploadedFile']['type'][$i]; $fname[] =$_FILES['uploadedFile']['name'][$i]; } } $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x"; $headers = "From: xxx@xxxx.com rn" . "MIME-Version: 1.0rn" . "Content-Type: multipart/mixed;rn" . " boundary="{$mime_boundary}""; $message = "This is a test”; $message = "This is a multi-part message in MIME format.nn" . "--{$mime_boundary}n" . "Content-Type: text/plain; charset="iso-8859-1"n" . "Content-Transfer-Encoding: 7bitnn" . $message . "nn"; for($j=0;$j<count($ftype); $j++){ $message .= "--{$mime_boundary}n". "Content-Type: {$ftype[$j]};n" . " name="{$fname[$j]}"n" . //"Content-Disposition: attachment;n" . //" filename="{$fileatt_name}"n" . "Content-Transfer-Encoding: base64nn" . $attachment[$j] . "nn"; } $message .= "--{$mime_boundary}n"; mail($to, $subject, $message, $headers);