PDA

View Full Version : Simple PHP Contact Form



iBrightDev
July 3rd, 2008, 16:13
I keep getting requests from people for my email script, and I always see people asking for simple contact forms on here. So, I am just going to post my code here for everyone to use. Hope this is helpful.

create a file named contact.php


<?php #contact.php

// Set a couple varialbes to call easier
$break = "<br/>";
$eol .= "\r\n";
$sol .= "\n";

// Check if the form has been submitted.
if (isset($_POST['submitted'])) {

$errors = array(); // Initialize error array.

// Check for a name
if (empty($_POST['name'])) {
$errors[] = 'You forgot to enter your name.';
}

// Check for a valid email address
if (!preg_match("/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)*\.([a-zA-Z]{2,6})$/", $_POST['email'])) {
$errors[] = 'You need to enter a valid email address.';
}

// Check for a message
if (empty($_POST['msg'])) {
$errors[] = 'You forgot to enter a message.';
}

if (empty($errors)) { // If everything is OK.

// Send E-Mail
$mailTo = '***PUT YOUR EMAIL ADDRESS HERE***';
$siteName = '***PUT YOUR SITE TITLE HERE***';
$senderName = $_POST['name'];
$senderMail = $_POST['email'];
$headers .= 'From: '.$senderName.' <'.$senderMail.'>'.$eol;
$headers .= 'Date: '.date("r").$eol;
$headers .= 'Sender-IP: '.$_SERVER["REMOTE_ADDR"].$eol;
$headers .= 'X-Mailser: MCT Adv.PHP Mailer v1.1'.$eol;
$headers .= 'MIME-Version: 1.0'.$eol;
$headers .= 'Content-Type: text/html; charset="iso-8859-1"'.$eol;
$subject = $siteName;

$msg .= '<font face=arial size=2>';
$msg .= 'You have a new message from ***PUT YOUR SITE NAME HERE***.'.$break.$break;
$msg .= 'Please review the information below.'.$break.$break;
$msg .= '<strong>Name:</strong> '.$_POST['name'].$break;
$msg .= '<strong>E-Mail:</strong> '.$_POST['email'].$break;
$msg .= '<strong>Phone:</strong> '.$_POST['phone'].$break;
$msg .= '<strong>Message:</strong> '.$_POST['msg'].$break;
$msg .= $break;
$msg .= '<hr>'.$break;
$msg .= 'Powered by: <a href="http://www.methodcomptech.com/">MCT Adv.PHP Mailer v1.1</a>.';
$msg .= '</font>';

// Mail it
mail($mailTo, $subject, $msg, $headers);

echo '<div style="padding: 10px;">Thank you!<br><br>Your message has been received. We will be in touch with you soon.</div></p>';
echo '<meta http-equiv="refresh" content="3;url=http://' . $_SERVER['HTTP_HOST'] . '/contact.php">';

} else { // Report the errors.

echo '<strong>Error!</strong><br/><br/>
The following error(s) occured.<br/>';
foreach ($errors as $message) { // Print each error.
echo " - $message<br/>\n";
}
echo 'Please go <a href="Javascript:history.go(-1)">back</a> and try again.</p>';

} // End of is (empty($errors)) IF.

} else { // Display the form.

$s = '<form action="contact.php" method="post">'.$sol;
$s .= 'Name: &nbsp; *'.$break.$sol;
$s .= '<input name="name" type="text" value="'.$_POST['name'].'" tabindex="1" size="50" />'.$break.$sol;
$s .= 'E-Mail Address: &nbsp; *'.$break.$sol;
$s .= '<input name="email" type="text" value="'.$_POST['email'].'" tabindex="2" size="50" />'.$break.$sol;
$s .= 'Phone Number:'.$break.$sol;
$s .= '<input name="phone" type="text" value="'.$_POST['phone'].'" tabindex="3" size="50" />'.$break.$sol;
$s .= 'Message: &nbsp; *'.$break.$sol;
$s .= '<textarea name="msg" cols="55" rows="8" tabindex="4" />'."$msg".'</textarea>'.$sol.$break.$break.$sol;
$s .= '<input name="submit" type="submit" value="Submit" tabindex="5" /><input type="hidden" name="submitted" value="TRUE" /> &nbsp; <input name="reset" type="reset" value="Reset" tabindex="6" /> &nbsp; <em>* = required</em>'.$sol;
$s .= '</form>'.$sol;

echo $s;

} //Close the main IF-ELSE.

?>


i do ask that if you use it, please leave the reference to MCT in the script.

BrandonTheG
July 3rd, 2008, 21:55
Not sure if I am just blind, but on the 2nd line were $msg is set, were does $printSite come from?

Corazu
July 4th, 2008, 00:45
Interesting. MCT dropped the ball! (not the soap..sorry Ben)

iBrightDev
July 4th, 2008, 01:08
Brandon;1004242']Not sure if I am just blind, but on the 2nd line were $msg is set, were does $printSite come from?
thanks for catching that. i missed it. on my CMS, i pull the site name from the DB, and that is why that was there. forgot to remove it. thanks for letting me know/. fixed now.

Interesting. MCT dropped the ball! (not the soap..sorry Ben)
lol, i sure did. all better now though :D

Corazu
July 4th, 2008, 18:21
thanks for catching that. i missed it. on my CMS, i pull the site name from the DB, and that is why that was there. forgot to remove it. thanks for letting me know/. fixed now.

lol, i sure did. all better now though :D

Well, I figured it was just an issue of pulling the script from an integrated environment. I wanted to use that joke though.. :P

TSO
July 4th, 2008, 21:34
Thanks! :)

iBrightDev
July 4th, 2008, 21:54
no problem. i will be posting more soon probably. and re-writing ibright now. going to be way better than before, and a whole new UI.

Corazu
July 5th, 2008, 00:12
Have you released a version of it yet? I want to muck around with it :P I could also..like...help a bit..and stuff..

New UI sounds sexy...because the 'current' one looked pretty decent.

iBrightDev
July 5th, 2008, 02:00
not sure which UI you saw, but, if it was the blue orange and white one with the tab menu, that is the new UI, just getting it to work now. i will let you know if i need any help though. so far so good. re-writing classes and functions and breaking things out better. better organization for the admin controld too, and more flexibility with plugins.