I've applied Jacek's tutorial to what I have and this is the final result
<?php
if(empty($_POST) === false){
$errors = array();
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$file = $_FILES['filename'];
if(empty($name) === true || empty($email) === true || empty($message) === true){
$errors[] = 'Name, email and message are required!';
} else {
if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
$errors[] ='Not a valid email';
}
if(ctype_alpha($name) === false){
$errors[] ='Name must only contain letters';
}
}
if(empty($errors) === true){
$boundary = md5(rand());
$headers = array(
'MIME-Version: 1.0',
"Content-type: multipart/mixed; boundary=\"{$boundary}\"",
"From: {$email}"
);
$message = array(
"--{$boundary}",
'Content-type: text/html',
'Content-Transfer-Encoding: 7bit',
'',
chunk_split($message),
"--{$boundary}",
"Content-type: {$file['type']}; name=\"{$file['filename']}\"",
"Content-Disposition: attachment; filename=\"{$file['filename']}\"",
'Content-Transfer-Encoding: base64',
'',
chunk_split(base64_encode(file_get_contents($file['tmp_name']))),
"--{$boundary}--"
);
//send email
mail($email, 'Contact form', implode("\r\n", $message), implode("\r\n", $headers));
//redirect user
header('Location: index.php?sent');
exit();
}
}
?>
but i'm having a problem. I need to get the name of the file and the extension to send the to the user.
If I send a jpg or a pdf file they send fine and If I send a php file for instance it doesn't show a thing. It simply shows a "noname" file.
What I need to do next, after fixing this, is to make a little verification for file types and do a size limit verification.
I also need to find a way to if the user doesn't want to send any attachment it just sends the mail without any problem. Now its gving me a
Warning: file_get_contents() [function.file-get-contents]: Filename cannot be empty in
/home/a3876128/public_html/testcontact.php on line
41