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: *'.$break.$sol;
$s .= '<input name="name" type="text" value="'.$_POST['name'].'" tabindex="1" size="50" />'.$break.$sol;
$s .= 'E-Mail Address: *'.$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: *'.$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" /> <input name="reset" type="reset" value="Reset" tabindex="6" /> <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.
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: *'.$break.$sol;
$s .= '<input name="name" type="text" value="'.$_POST['name'].'" tabindex="1" size="50" />'.$break.$sol;
$s .= 'E-Mail Address: *'.$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: *'.$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" /> <input name="reset" type="reset" value="Reset" tabindex="6" /> <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.