Skip to content

Instantly share code, notes, and snippets.

@lordmatt
Created November 17, 2015 16:07
Show Gist options
  • Save lordmatt/28992a99907a6d2929a2 to your computer and use it in GitHub Desktop.
Save lordmatt/28992a99907a6d2929a2 to your computer and use it in GitHub Desktop.
<?php
/**
* --------------------------------------------------------------
* Contact Form To Email Demo
* Copyright (C) 2015 Lord Matt <lordmatt.co.uk>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* (see nucleus/documentation/index.html#license for more info)
* ----------------------------------------------------------------
*
* It should go without saying that this is provided as is and it's up to you to
* be sure that it works.
*
* This was written as an example of a contact form. It's only a demo of how one
* might work. It is not necessarily best pracice all the way through.
*
* That said I am quite pleased with it.
*
* If you want to use it edit the $CONF values and include it where you want the
* form. Better yet put the conf values in a seperate file so you can have more
* than one form or other fancy stuff.
*
* You might want to include some other bits before and after the form but if
* you embed this form in another apge you will need to edit the redirect line
* as it will throw errors if headers are already sent (content has gone out).
*
* Provide some CSS to make it look nice.
*
* Everything is in span which is CSS inline but can be changed to display block
* if you want to float or whatever.
*
* This is a demo only. Test it for goodness sake.
*
*
*
* You can set a seperate FROM address. This can be handy if you want to filter
* contact form messages in your inbox. The subject prefix is also good for this
* too.
*
*
*
*/
$CONF=array();
$CONF['to']='myinbox@example.com';//where do you want it (put your email address here)
$CONF['from']='somespareinbox@eample.com';// who is sending this (put your (spare) email address here)
$CONF['subjectPrefix']='CONTACT FORM:';// put something at the front of contact form messages?
// a long jumbled string with lots of different types of characters
$CONF['salt']='##LONG+conmplicated-salt-values=keep::you::SAFE...//@@@@@@@@@@@@';
$CONF['thankyou']='';// URL of your thank you page
//$CONF['thankyou']='http://example.com/thankyou.html';// absolute
/* ------------- STOP EDITING ------------- *\
\* UNLESS YOU WANT TO GET YOUR HANDS DIRTY! */
// allways init vars
$from = '';
$subject='';
$message='';
// validation vars
$error=false;
$msg = array();
// only you can prevent unsightly error messages
// this function checks if the post value exists
// you get the value if dos or an empty string
function get_if_set($what){
if(isset($_POST[$what])){
return $_POST[$what];
}
return '';
}
// codes expire at midnight
function testCode(){
global $CONF;
return MD5(
date('Ymd').
$CONF['salt'].
date('MjDy')
);
}
// we only process the form if the code is in there
if(get_if_set('key')==testCode()){
// lets get our data and clean anything dangerouse away
$from = trim(strip_tags(get_if_set('from')));
$subject = trim(strip_tags(get_if_set('subject')));
$message = trim(strip_tags(get_if_set('message')));
// validate input
// assumes 5.2.0 or higher
// http://php.net/manual/en/function.filter-var.php
if (!filter_var($from, FILTER_VALIDATE_EMAIL)){
$error=true;
$msg[]='Are you sure that email address is correct?';
}
if($subject==''){
$error=true;
$msg[]='What is your message about?';
}
if($message==''){
$error=true;
$msg[]='Did you have something that you wanted to say?';
}
// if not error
if(!$error){
// this is where the magic is (it send the mail)
// see: http://php.net/manual/en/function.mail.php
$to = $CONF['to'];
$subject = $CONF['subjectPrefix'] . $subject;
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: {$CONF['from']}";
$headers[] = "Reply-To: {$from}";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();
mail($to, $subject, $message, implode("\r\n", $headers));
$from = '';
$subject='';
$message='';
//---now to the thankyou page--->
//if you have already outputted cotnent this will make a nasty error!!!!
//in which case change this to something else (like including a thankyou
//message or something.
header("Location: {$CONF['thankyou']}");
exit; // stop doing stuff
}
}
// If you are including this is where the before the form HTML include goes
// report errors
if($error){
echo "<ul class='form_error'>";
foreach($msg as $m){
echo "<li>{$m}</li>";
}
echo "</ul>";
}
?>
<form method="post">
<input type="hidden" name="key" value="<?php echo testCode(); ?>" />
<span class="formRow">
<span class="formlabel form_in"><label for="from">Your email address</label></span>
<span class="forminput form_in"><input type="text" name="from" value="<?php echo $from; ?>"/></span>
</span>
<span class="formRow">
<span class="formlabel form_in"><label for="subject">Subject (what's the contact about)</label></span>
<span class="forminput form_in"><input type="text" name="subject" value="<?php echo $subject; ?>"/></span>
</span>
<span class="formRow">
<span class="formlabel form_ta"><label for="message">Your message</label></span>
<span class="forminput form_ta"><textarea name="message"><?php echo $message; ?></textarea></span>
</span>
<span class="formRow">
<span class="forminput form_ta"><input type="submit" value="submit" /></span>
</span>
</form>
<?php
// debug and testing
// leave this off [false] unless you want a mess
if(false){
echo "<pre>";
print_r($_POST);
echo "</pre>";
}
// If you are including this is where the after the form HTML include goes
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment