Skip to content

Instantly share code, notes, and snippets.

@avclark
Created November 14, 2012 15:24
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 avclark/4072742 to your computer and use it in GitHub Desktop.
Save avclark/4072742 to your computer and use it in GitHub Desktop.
Simple PHP Contact Script
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "email@yourdomain.com";
$email_subject = "Email Subject line";
function died($error) {
?>
<h2>Oops! Something's not right.</h2>
<p><?php echo $error."<br /><br />"; ?></p>
<?php
die();
}
$name = $_POST['name'];
$email_from = $_POST['email'];
$message = $_POST['message'];
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
$error_message .= 'The name you entered does not appear to be valid.<br />';
}
if(strlen($message) < 2) {
$error_message .= 'The message you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n";
mail($email_to, $email_subject, $email_message, $headers);
?>
<p>Thank you for contacting us. We will be in touch with you very soon.</p>
<?php
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment