PHP Form Mailer

A typical contact form consists of two things, an HTML form and a server side script to send the mail.

Here is an extremely generic form:

<div id='formcontainer'>
<form method='post'>
Full Name: <input type='text' id='fullname' name='fullname' /><br>
E-Mail: <input type='text' id='email' name='email' /><br>
Message: <textarea id='message' name='message'></textarea><br>
<input type='submit' value='Send Message' onclick='courierRun()' />
</form>
</div>

Here is some javascript to place in the head of your site

<script type='text/javascript'>
function courierRun()
{
var fullname = document.getElementById('fullname').value;
var emailaddress = document.getElementById('email').value;
var messagecontents = document.getElementById('message').innerHTML;

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
responseText = xmlhttp.responseText;
if(responseText == 'Failed To Send')
{
document.getElementById().innerHTML = "<p style='color:red;'>" + responseText + '</p><br>' + document.getElementById().innerHTML;
}
else
{
document.getElementById().innerHTML = "<p style='color:green;'>" + responseText + "</p>";
}

}
};
xhttp.open("POST", "mailsending.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("fullname=" + fullname + "&email=" + emailaddress + "&message=" + messagecontents);
}
</script>

And here is the php that processes and sends the information we can call this mailsending.php:

<?php
//your email address
$youremail = 'youremail@address.com';
//your subject line
$subject = 'Mail From Your Website';
//success message
$successmessage = 'Thank you for contacting us!';

$error = "";

//if the post request has fullname, email, & message then we continue on
if(isset($_POST['fullname']) && isset($_POST['email'])  && isset($_POST['message']))
{

//and sanitize apply the values to variables
$fullname = filter_var($_POST['fullname'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);

//check to make sure email address is valid
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false)
{

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= 'From: <' . $email . '>' . "\r\n";

if(mail($youremail,$subject,$message,$headers))
{
echo $successmessage;
}
else
{
echo 'Failed To Send';
}

} else {
//do nothing no valid email address

echo 'Failed To Send';
}

}
else
{
//do nothing no valid data

echo 'Failed To Send';
}
?>

Notice anything off about the code let me know !

Here is the finished product with the simple HTML, obviously you will want to add some styling to this:

generic