0
votes

This is php code for sending just attachment

 $attachment = chunk_split(base64_encode(file_get_contents($_FILES['file_to_upload']['tmp_name'])));
 $filename = $_FILES['file_to_upload']['name'];
 $boundary =md5(date('r', time())); 
 $headers = "From: $from \r\nReply-To: $from ";
 $headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";
 $body.="This is a multi-part message in MIME format.

--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"

--_2_$boundary
Content-Type: text/html; charset=\"UTF-8\"
Content-Transfer-Encoding: 7bit



--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 





$attachment
--_1_$boundary--";

It working when attachment is send but when user not wont to select any attachment it send some Part2.bin file in email. How to overcome this? Not send anything when file isn't selected

1

1 Answers

0
votes

Form controls can be set in HTML to have be required when marked up with the required attribute.

When a form control (e.g. HTML element) is mark up as required, the form isn't submitted for the action unless the user sets a value.

The above should be helpful to prevent the form being submitted in the client without the user selecting a file.

On the server side aspect of things, the $_FILES global for the file will contain an "error" key that is set to 0 (UPLOAD_ERR_OK) when there is no error. Error codes that can be checked for are documented here

You can check this by writing:

if ($_FILES["file_to_upload"]["error"] === UPLOAD_ERR_OK) {
    // send email
} else {
    // don't send email
}

The specific case of the condition where no file was uploaded also rings true.

if ($_FILES["file_to_upload"]["error"] === UPLOAD_ERR_NO_FILE) {
    // don't send email
} else {
    // send email
}