add some security to mail script

This commit is contained in:
David Miller 2016-02-09 13:33:08 -05:00
parent 7d7d64472a
commit a9222141c1

View File

@ -1,26 +1,26 @@
<?php <?php
// Check for empty fields // Check for empty fields
if(empty($_POST['name']) || if(empty($_POST['name']) ||
empty($_POST['email']) || empty($_POST['email']) ||
empty($_POST['phone']) || empty($_POST['phone']) ||
empty($_POST['message']) || empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)) !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{ {
echo "No arguments Provided!"; echo "No arguments Provided!";
return false; return false;
} }
$name = $_POST['name']; $name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = $_POST['email']; $email_address = strip_tags(htmlspecialchars($_POST['email']));
$phone = $_POST['phone']; $phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = $_POST['message']; $message = strip_tags(htmlspecialchars($_POST['message']));
// Create the email and send the message // Create the email and send the message
$to = 'yourname@yourdomain.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to. $to = 'yourname@yourdomain.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form: $name"; $email_subject = "Website Contact Form: $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message"; $email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: noreply@yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com. $headers = "From: noreply@yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address"; $headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers); mail($to,$email_subject,$email_body,$headers);
return true; return true;
?> ?>