Hi i have code that sends email with multiple attachments, but i have problems with one piece of that code below.
Whenever i submit image, i get “Unsuported file format!”. If i delete that part of code, it works great, but then user can send every type of file (that is dangerous, i know). Filesize check works great. What is wrong? :S
//*** Attachment ***//
for($i=0;$i<count($_FILES["fileAttach"]["name"]);$i++)
{
$filesize = $_FILES["fileAttach"]["size"][$i]/1024;
if($_FILES["fileAttach"]["name"][$i] != "" && $filesize < 50 && preg_match("image/", $_FILES["fileAttach"]["type"][$i]))
{
$strFilesName = $_FILES["fileAttach"]["name"][$i];
$strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"][$i])));
$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n";
$strHeader .= "Content-Transfer-Encoding: base64\n";
$strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";
$strHeader .= $strContent."\n\n";
}
elseif(!preg_match("image/", $_FILES["fileAttach"]["type"][$i])){
die('<div id="warning">Unsuported file format!</div>');
}
elseif($filesize > 50){
die('<div id="warning">Filesize must be less than 2 Mb!!!</div>');
}
}
should just be
with no filename portion. That goes onto the content-disposition line.
In the bigger picture view, don’t build your own mime emails. As you’re finding out, it’s painful and ugly. Use PHPMailer or Swiftmailer to do it for you – that whole attachment business can be reduced to a SINGLE function call with either library.