Thanks in advance for your answers. I am looking to add a file upload feature to my PHP contact form script. So a user is able to upload a CV for example.
The PHP script below works very well and helps to reduce spam as well as being really easy to understand. So we would like to keep this format and not complicate it much more.
How would I go about editing the existing script to include this function?
Any help appreciated.
/**
*
* Function to validate an email address
*
**/
function validate_email($email)
{
//Perform a basic syntax-Check
//If this check fails, there's no need to continue
$email = filter_var($email, FILTER_SANITIZE_STRING);
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
return false;
//extract host
list($user, $host) = explode("@", $email);
//check, if host is accessible
if (!checkdnsrr($host, "MX") && !checkdnsrr($host, "A"))
return false;
return true;
}
/**
*
* Get/set variables
*
**/
$name = isset($_POST['name']) ? htmlspecialchars(stripslashes($_POST['name']), ENT_QUOTES, "UTF-8") : '';
$email = isset($_POST['email']) ? htmlspecialchars(stripslashes($_POST['email']), ENT_QUOTES, "UTF-8") : '';
$confirm = isset($_POST['confirm']) ? htmlspecialchars(stripslashes($_POST['confirm']), ENT_QUOTES, "UTF-8") : '';
$message = isset($_POST['message']) ? htmlspecialchars(stripslashes($_POST['message']), ENT_QUOTES, "UTF-8") : '';
$SPAM = $_POST["spam-check"];
/**
*
* Check no empty values
*
**/
if (empty($name) OR empty($email) OR empty($confirm) OR empty($message) OR !empty($SPAM) OR !validate_email($email) OR ($email !== $confirm))
{
exit("There was an error whilst processing your request, please call us or try again later – Thank You");
}
else
{
$subject = "Website Contact Form Submit";
$to = "email@myemail.co.uk";
$body = "Name: " . $name . "\n";
$body .= "Email: " . $email . "\n\n";
$body .= "Message:\n" . $message;
$header = "From: " . $name . " <" . $email . ">\n";
$header .= "Reply-To: " . $name . " <" . $email . ">\n";
if ( @mail($to, $subject, $body, $header) )
{
echo json_encode( array("sent"=>"success", "feedback"=>"Your query has been sent successfully") );
exit;
}
}```