My code below does send out an email correctly but it is only attaching the first attachment even though if i've selected more files than one, all files have been uploaded to the correct place. For example, if i've uploaded two files, file1.txt and file2.txt, these are the headers that I am getting even though only file1.txt is sent to the recipient. I'm assuming that i'm ising something in the headers content or am putting in something twice that I shouldn't be?
From: lee@swiftinter.net Reply-To: lee@swiftinter.net MIME-Version: 1.0 Content-Type: multipart/mixed; boundary=CC299C1B65FCBDC913B9913A1E42CAC0 --CC299C1B65FCBDC913B9913A1E42CAC0 Content-Type: text/plain Content-Transfer-Encoding: 8bit Dear message Yours Sincerely Lee Ward --CC299C1B65FCBDC913B9913A1E42CAC0 Content-Type: text/plain; name="file1.txt" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="file1.txt" dGhpcyBpcyBkYXRhIGZvciBmaWxlIDE= --CC299C1B65FCBDC913B9913A1E42CAC0--From: lee@swiftinter.net Reply-To: lee@swiftinter.net MIME-Version: 1.0 Content-Type: multipart/mixed; boundary=1AF2FD48AE670721BFD2BB1D6528DAA6 --1AF2FD48AE670721BFD2BB1D6528DAA6 Content-Type: text/plain Content-Transfer-Encoding: 8bit Dear message Yours Sincerely Lee Ward --1AF2FD48AE670721BFD2BB1D6528DAA6 Content-Type: text/plain; name="file2.txt" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="file2.txt" dGhpcyBpcyBkYXRhIGZvciBmaWxlIDI= --1AF2FD48AE670721BFD2BB1D6528DAA6--
The code is:
$nooffiles = $POST['noofuploads'];
$headers = "";
$uploaddir = "../temp";
function attachfiles($text, $from, $file, $type, $name, $dir) {
$content = fread(fopen($dir."/".$file,"rb"),filesize($dir."/".$file));
$content = chunk_split(base64_encode($content));
$uid = strtoupper(md5(uniqid(time())));
$header = "From: ".$from."\nReply-To: ".$from."\n";
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: multipart/mixed; boundary=".$uid."\n";
$header .= "--".$uid."\n";
$header .= "Content-Type: text/plain\n";
$header .= "Content-Transfer-Encoding: 8bit\n\n";
$header .= $text."\n";
$header .= "--".$uid."\n";
$header .= "Content-Type: ".$type."; name=\"".$file."\"\n";
$header .= "Content-Transfer-Encoding: base64\n";
$header .= "Content-Disposition: attachment; filename=\"".$file."\"\n\n";
$header .= $content."\n";
$header .= "--".$uid."--";
return $header;
}
$consultant = mysql_query("SELECT * FROM consultants WHERE consultantID='".$SESSION['consultantID']."'", $link_id) or die(mysql_error());
$cons_data = mysql_fetch_array($consultant);
foreach($_POST["email"] As $val){
$candidates = mysql_query("SELECT email FROM personal WHERE candidateID='$val'", $link_id) or die(mysql_error());
$cand_data = mysql_fetch_array($candidates);
for($i=0;$i<$nooffiles;$i++){
copy($_FILES['attachments']['tmp_name'][$i], "$uploaddir/".$_FILES['attachments']['name'][$i]);
$filename = $_FILES['attachments']['name'][$i];
$filename_name = $_FILES['attachments']['tmp_name'][$i];
$filename_type = $_FILES['attachments']['type'][$i];
$message = "Dear ".$cand_data['firstnames']."\n\n";
$message .= $_POST['message']."\n\n";
$message .= "Yours Sincerely\n\n";
$message .= $cons_data['firstname']." ".$cons_data['surname'];
$headers .= attachfiles($message, $cand_data['email'], $filename, $filename_type, $filename_name, $uploaddir);
}
mail($cand_data['email'],$_POST['subject'],"",$headers);
}