public
Last active — forked from /Contact.php

helping out markblackler at http://css-tricks.com/forums/discussion/23467

  • Download Gist
markblackler_contact_form.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php
session_start();
$_SESSION['token'] = $token = md5( $_SERVER['HTTP_USER_AGENT'].microtime( true ) );
?>
<form action="markblackler_contact_submit.php" method="post">
<input type="hidden" name="token" value="<?php echo $token; ?>">
<!-- don't use <font>. it's very, very dead. use CSS stylesheets. -->
<label for="cf_name">Your name: </label><br>
<input type="text" name="cf_name"><br>
<label for="cf_email">Your e-mail: </label><br>
<input type="text" name="cf_email"><br>
<label for="cf_message">Message: </label><br>
<textarea name="cf_message" rows="12" cols="60"></textarea><br><br>
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>
markblackler_contact_submit.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
<?php
session_start();
if(
! empty( $_POST['token'] )
&& ! empty( $_SESSION['token'] )
&& $_POST['token'] === $_SESSION['token']
){
unset( $_SESSION['token'] );
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
 
$mail_to = 'mail@markblackler.com';
$subject = 'Message from a site visitor '.$field_name;
 
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
 
# This:
# $headers = 'From: '.$field_email."\r\n";
# Will eventually get your email caught in spam traps.
# The email is "From" _you_ (your website, that is), _not_ the guy who filled out the form.
$headers = 'From: no-reply@my-website.com'."\r\n";
# This is the right idea:
$headers .= 'Reply-To: '.$field_email."\r\n";
 
$mail_status = mail($mail_to, $subject, $body_message, $headers);
 
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. I will contact you as soon as possible.');
document.location = 'http://www.markblackler.com'
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to mail@markblackler.com');
document.location = 'http://www.markblackler.com'
</script>
<?php
}
}else{
/* no form submission, and/or no session token. do something else instead. */
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.