Skip to content

Instantly share code, notes, and snippets.

@crazyboy24
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crazyboy24/0dc44d3440cee8de6150 to your computer and use it in GitHub Desktop.
Save crazyboy24/0dc44d3440cee8de6150 to your computer and use it in GitHub Desktop.
<?php phpinfo();// Initialize variables to null.
$name =""; // Sender Name
$email =""; // Sender's email ID
$message =""; // Sender's Message
$subject =implode($subject, "\n"); // Sender's Subject
$subjectError ="";
$nameError ="";
$emailError ="";
$messageError ="";
$successMessage =""; // On submitting form below function will execute.
if(isset($_POST['submit'])) { // Checking null values in message.
if (empty($_POST["name"])){
$nameError = "Name is required";
}
else
{
$name = test_input($_POST["name"]); // check name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name))
{
$nameError = "Only letters and white space allowed";
}
} // Checking null values in the message.
if (empty($_POST["email"]))
{
$emailError = "Email is required";
}
else
{
$email = test_input($_POST["email"]);
} // Checking null values in message.
if (empty($_POST["message"]))
{
$messageError = "Message is required";
}
else
{
$message = test_input($_POST["message"]);
}
// Checking null values in the message.
if (empty($_POST["subject"]))
{
$subjectError = "Subject is required";
}
else
{
$message = test_input($_POST["subject"]);
}
// Checking null values in the subject.
if( !($name=='') && !($email=='') &&!($message=='') && !($subject=='') )
{ // Checking valid email.
if (preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
{
$header= $name."<". $email .">".$subject;
$headers = "My header here";
if(mail($email, $headers, $msg ) && mail("myemailid", $header, $msg1 ))
{
$successMessage = "Message sent successfully. You will be contacted soon.";
}
}
else
{ $emailError = "Invalid Email";
}
}
} // Function for filtering input values. function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment