I have a simple contact script that collects basic user data and allows them to attach a file - either PDF, DOC, DOCX, JPG, JPEG, or PNG. I am running into two issues.
1) JPG, JPEG, and PNG files are not attached properly, the other formats come through just fine.
2) None of the body text is rendered. Every message comes up blank
For brevity I removed all the setting of variables through $_POST retrieval and simply tried adding "sample text" into the body of the message.
TIA!
<?php
// Get the uploaded file information
$attachment = basename($_FILES['attachment']['name']);
// Get the file extension and size
$type_of_attachment = substr($attachment,strrpos($attachment, '.') + 1);
$size = $_FILES["attachment"]["size"]/1024; //size in KBs
// Settings
$max_allowed_file_size = 250; // size in KB
$allowed_extensions = array("pdf", "doc", "docx", "jpg", "jpeg", "png");
// Validations
if($size > $max_allowed_file_size){
header("Location: /error.php?m=contact&ec=1");
}
//------ Validate the file extension -----
$allowed_ext = false;
for($i=0; $i<sizeof($allowed_extensions); $i++){
if(strcasecmp($allowed_extensions[$i],$type_of_attachment) == 0){
$allowed_ext = true;
}
}
if(!$allowed_ext){
header("Location: /error.php?m=contact&ec=2");
}
// Copy the temp. uploaded file to uploads folder
$path_of_uploaded_file = "tmpattach/" . $attachment;
$tmp_path = $_FILES["attachment"]["tmp_name"];
if(is_uploaded_file($tmp_path)){
copy($tmp_path,$path_of_uploaded_file);
}
// Submit the form
$to = "user@mydomain.com";
$from = "donotreply@mydomain.com";
$subject = "Form Submission";
$random_hash = md5(date('r', time()));
$headers = "From: $from" . "\r\n";
$headers .= "Content-Type: multipart/alternative; boundary='PHP-alt-$random_hash'";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";;
$attached = chunk_split(base64_encode(file_get_contents("$path_of_uploaded_file")));
$headers .= "Content-Type: application/$type_of_attachment; name=$attachment" . "\r\n";
$headers .= "Content-Transfer-Encoding: base64" . "\r\n";
$headers .= "Content-Disposition: attachment" . "\r\n";
$body = "sample text";
$body .= $attached;
echo @mail($to,$subject,$body,$headers);
?>