Show DevBest Simple contact form

Weasel

👄 I'd intercept me
Nov 25, 2011
4,135
2,461
I have been working on a little contact script to intergrate with SmashCMS, however I don't have any time to do it. So here is a little, simple, contact script. Currently there is no anti-spam check, however if people would like to see this, I'll add it.

The core PHP file:
PHP:
<?php
session_start();
    $hour = date("H");
    $minute = date("i");
    $seconds = date("s");
    $day = date("d");
    $month = date("m");
    $year = date("Y");
$extratime = 30;
 
$current = mktime($hour,$minute,$seconds,$month,$day,$year);
$expire =  mktime($hour,$minute,$seconds+$extratime,$month,$day,$year);
 
    $firstname = $_POST['firstname'];
    $surname = $_POST['surname'];
    $email = $_POST['email'];
    $message = $_POST['message'];
 
    $to = '[email protected]';
    $subject = 'Message from '.$firstname.' '.$surname.' with IP '.$_SERVER['REMOTE_ADDR'].'';
 
    $headers = "MIME-version: 1.0\r\n";
    $headers .= "content-type: text/html;charset=utf-8\r\n";
 
if(isset($_POST['send']))
{
$check_status = '';
$errors = array();
 
if (isset($_SESSION['check']) && $_SESSION['check'] > $current)
{
$errors[] = 'Please wait '.$extratime.' seconds before sending another message..';
}
if(empty($firstname))
{
$errors[] = 'The field "First name" is empty!';
}
if(empty($surname))
{
$errors[] = 'The field "Surname" is empty!';
}
if(empty($email))
{
$errors[] = 'The field "E-mail address" is empty!';
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL) && !empty($email))
{
$errors[] = 'The given e-mail address is invalid. Please fill in a good one.';
}
if(empty($message))
{
$errors[] = 'The field "Message" is empty!';
}
 
if(count($errors) == 0)
{
$headers .= 'From: ' . $firstname . ' ' . $surname . '<' . $email . '>';
 
if(mail($to, $subject, nl2br($message), $headers))
{
$check_status = 'Your message has been succesfully send!!';
$_SESSION['check'] = $expire;
}
else
{
$errors[] = 'A system error occured.';
}
}
}
 
foreach($errors as $key => $keyvalue){
$check_status .= ''.$keyvalue.'<br>';
}
?>

The form:
Code:
 <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
First name<br />
<input type="text" name="firstname" /><br /><br />
Surname<br />
<input type="text" name="surname" /><br /><br />
E-mail address<br />
<input type="text" name="email" /><br /><br />
Your message<br />
<textarea cols="50" rows="12" name="message"></textarea><br /><br />
<input type="reset" value="Empty fields" /> <input type="submit" name="send" value="Send message" />
</form>

The PHP code to let the error/succes messages show. Place this piece of code on the spot you want this to show:
PHP:
<?php
if ($check_status != ''){
echo $check_status;
}
?>

Do not forget to set the $extratime variable to the amount of seconds you want people to wait before sending another email, and also change the $to variable with your own email.

If you have any errors, please say so. I'll try to fix it. Ideas are welcome to.
 

Weasel

👄 I'd intercept me
Nov 25, 2011
4,135
2,461
Updated version with anti-spam feature:
PHP:
<?php
session_start();
    $hour = date("H");
    $minute = date("i");
    $seconds = date("s");
    $day = date("d");
    $month = date("m");
    $year = date("Y");
$extratime = 30;
 
$current = mktime($hour,$minute,$seconds,$month,$day,$year);
$expire =  mktime($hour,$minute,$seconds+$extratime,$month,$day,$year);
 
    $firstname = $_POST['firstname'];
    $surname = $_POST['surname'];
    $email = $_POST['email'];
    $message = $_POST['message'];
   
    $to = '[email protected]';
    $subject = 'Message from '.$firstname.' '.$surname.' with IP '.$_SERVER['REMOTE_ADDR'].'';
   
    $headers = "MIME-version: 1.0\r\n";
    $headers .= "content-type: text/html;charset=utf-8\r\n";
 
if(isset($_POST['send']))
{
$check_status = '';
$errors = array();
  
if (isset($_SESSION['check']) && $_SESSION['check'] > $current)
{
$errors[] = 'Please wait '.$extratime.' seconds before sending another message..';
}
if(empty($firstname))
{
$errors[] = 'The field "First name" is empty!';
}
if(empty($surname))
{
$errors[] = 'The field "Surname" is empty!';
}
if(empty($email))
{
$errors[] = 'The field "E-mail address" is empty!';
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL) && !empty($email))
{
$errors[] = 'The given e-mail address is invalid. Please fill in a good one.';
}
if(empty($message))
{
$errors[] = 'The field "Message" is empty!';
}
 
if(count($errors) == 0)
{
$headers .= 'From: ' . $firstname . ' ' . $surname . '<' . $email . '>';
 
if(mail($to, $subject, nl2br($message), $headers))
{
$check_status = 'Your message has been succesfully send!!';
$_SESSION['check'] = $expire;
}
else
{
$errors[] = 'A system error occured.';
}
}
}
 
foreach($errors as $key => $keyvalue){
$check_status .= ''.$keyvalue.'<br>';
}
?>

Main post is also updated.
 

GangnamStyle

HTML Advanced Coder
Jan 8, 2012
121
8
Nice, I'm using my own contact.php code and using your mail.php code.
to connect I use this code:
HTML:
<form id="contactform" action="mail.php" method="post">
to connect to the Mail.php but whenever i try on my host i get the error:
Code:
Warning: Invalid argument supplied for foreach() in /home/unionind/public_html/mail.php on line 71
Can you help?
 

Users who are viewing this thread

Top