| You can just copy and paste all of this code into a blank PHP document. PHP must be enabled on your server for this to work for you. Some servers require SMTP configurations in order to send using the PHP mail function. ini_set() functions and php.ini file help with adding that information if your server requires SMTP validation when using PHP mail(). Watching the video that goes with this may give extra insight into how it works if needed. In the video I discuss more security measure you can place into it besides filtering. |
<?php
/* Script Created By Adam Khoury @ www.flashbuilding.com - March 17, 2010 */
$formMessage = "";
$senderName = "";
$senderEmail = "";
$senderMessage = "";
if ($_POST['username']) { // If the form is trying to post value of username field
// Gather the posted form variables into local PHP variables
$senderName = $_POST['username'];
$senderEmail = $_POST['email'];
$senderMessage = $_POST['msg'];
// Make sure certain vars are present or else we do not send email yet
if (!$senderName || !$senderEmail || !$senderMessage) {
$formMessage = "The form is incomplete, please fill in all fields.";
} else { // Here is the section in which the email actually gets sent
// Run any filtering here
$senderName = strip_tags($senderName);
$senderName = stripslashes($senderName);
$senderEmail = strip_tags($senderEmail);
$senderEmail = stripslashes($senderEmail);
$senderMessage = strip_tags($senderMessage);
$senderMessage = stripslashes($senderMessage);
// End Filtering
$to = "PUT_YOUR_EMAIL_ADDRESS_HERE";
$from = "contact@your_website_here.com";
$subject = "You have a message from your website";
// Begin Email Message Body
$message = "
$senderMessage
$senderName
$senderEmail
";
// Set headers configurations
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "Content-type: text\r\n";
$headers .= "From: $from\r\n";
// Mail it now using PHP's mail function
mail($to, $subject, $message, $headers);
$formMessage = "Thanks, your message has been sent.";
$senderName = "";
$senderEmail = "";
$senderMessage = "";
} // close the else condition
} // close if (POST condition
?>
<html>
<body>
<form id="form1" name="form1" method="post" action="contact.php">
<?php echo $formMessage; ?>
<br />
<br />
Your Name:<br />
<input name="username" type="text" id="username" size="36" maxlength="32" value="<?php echo $senderName; ?>" />
<br />
<br />
Your Email:
<br />
<input name="email" type="text" id="email" size="36" maxlength="32" value="<?php echo $senderEmail; ?>" />
<br />
<br />
Your Message:
<br />
<textarea name="msg" id="msg" cols="45" rows="5"><?php echo $senderMessage; ?></textarea>
<br />
<br />
<br />
<input type="submit" name="button" id="button" value="Send Now" />
</form>
</html>
</body>