Contact me

Fields marked with * are mandatory.









Source code

<?php
    
////////////////////////////////////////////////////////
    // This script was made by andreas lagerkvist
    // it may be used and modified freely
    // copyright © andreas lagerkvist
    // www.exscale.se
    ////////////////////////////////////////////////////////

    // A message is being sent
    
if(isset($_POST['docontact']))
    {

        
///////////////////////////////////////////
        // Settings
        ///////////////////////////////////////////
        // Who is the message being sent to?
        
$to "your@email.com";

        
// Default subject
        
$def_subject "From the website!";

        
// Minimum length for name
        
$min_name_len 2;

        
// Minimum length for message
        
$min_message_len 5;

        
///////////////////////////////////////////
        // Send message
        ///////////////////////////////////////////
        // Check so everything is filled out
        
if(
        isset(
$_POST['name']) and 
        
strlen($_POST['name']) >= $min_name_len and 
        isset(
$_POST['message']) and 
        
strlen($_POST['message']) >= $min_message_len and 
        isset(
$_POST['email']) and 
        
preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i"$_POST['email'])
        )
        {
            
$subject = (isset($_POST['subject'])) ? $_POST['subject'] : $def_subject;
            
$message $_POST['message'] ."\n==================================================\n" .$_POST['name'] ." | " .$_POST['email'];
            
$header "From: " .$_POST['name'] ." <" .$_POST['email'] .">\r\n";

            
mail($to$subject$message$headers);

            
// You need to call ob_start() first in the document for this to work
            
header("location: ?" .$_SERVER['QUERY_STRING'] ."&sent");
        }
        else
        {
            
// You need to call ob_start() first in the document for this to work
            
header("location: ?" .$_SERVER['QUERY_STRING'] ."&fillall");
        }
    }
?>

<style type="text/css">
/*
    You should move this CSS to your CSS-document
*/
body, input, textarea {
    font-family: Verdana;
    font-size: 10px;
    color: #111;
}

code {
    font-size: 12px;
    font-family: Courier New;
}

input, textarea {
    margin-bottom: 10px;
}

.success {
    background: #EFF8E1;
    padding: 5px;
    border: 1px solid #BDC9A7;
    color: #222F0A;
}

.error {
    background: #F9E9E8;
    padding: 5px;
    border: 1px solid #C47A79;
    color: #290100;
}

#contact em {
    color: red;
}

#contact fieldset {
    border: 0;
    margin: 0;
    padding: 0;
}

    #contact fieldset legend {
        padding: 0;
        margin: 0 0 10px 0;

        font-family: Trebuchet MS;
        font-size: 24px;
        color: #369;
    }
</style>

<div id="contact">

    <fieldset>

        <legend>Contact me</legend>

        <?php
            
if(isset($_GET['sent']))
            {
                echo 
"<p class=\"success\">Thank you, your message was sent successfully.</p>";
            }
            if(isset(
$_GET['fillall']))
            {
                echo 
"<p class=\"error\">Please fill out all mandatory fields. This error may also occur if your email address is invalid.</p>";
            }
        
?>

        <p>Fields marked with <em>*</em> are mandatory.</p>

        <form method="post" action="">
            <p>
                <input type="hidden" name="docontact" value="1" />

                <label for="name"><em>*</em> Name</label><br />
                <input type="text" name="name" id="name" size="30" /><br />
                <label for="email"><em>*</em> E-mail</label><br />
                <input type="text" name="email" id="email" size="30" /><br />
                <label for="email">Subject</label><br />
                <input type="text" name="subject" id="subject" size="40" /><br />
                <label for="email"><em>*</em> Message</label><br />
                <textarea name="message" id="message" rows="8" cols="40" /></textarea><br />
                <input type="submit" value="Send" id="send" />
            </p>
        </form>

    </fieldset>

</div>

<?php
    
// This shows the source
    
echo "<h1>Source code</h1>";
    
highlight_file(__FILE__);
?>