Ads

How to: Creating an contact form

Alright, now some of you know, how I like to start out with displaying the full code first then breaking it down, well here we go. Now we're going to make a simple and secure contact form today. Lets begin;

Here's the full code:


CODE
<?php 
if ( (!$emailtoname) || (!$emailtoaddr) ){ 

    
// Printing the form 
?> 

    <p>This is a quick script to show how to send a normal text email with PHP, plus to see what the output looks like. Once you receive it, I recommend you look at the raw email headers to see what's going on.</p> 
     
    </p>If you wish to run it, please enter your <b><u>real</u></b> email address. I promise we don't keep them, but there is no point in writing a false one.</p> 

    <form action="<?php print ($PHP_SELF); ?>" method="post"> 
    <p>What is your name?<br> 
    <input type="text" name="emailtoname"> 

    <p>What is your e-mail address?<br> 
    <input type="text" name="emailtoaddr"> 

    <p>Please enter a brief comment:<BR> 
    <textarea cols="30" rows="4" name="comment"></textarea> 
     
    <p><input type="submit" value="Send E-Mail"> 
    </form> 
<?php 

else { 

    
//ERROR CHECKING 

    // Don't worry about figuring this regular expression out quite yet... 
    // It will test for address@domainname and address@ip 
    
$regexp "^[0-9a-z_.-]+@(([0-9]{1,3}.){3}[0-9]{1,3}|([0-9a-z][0-9a-z-]*[0-9a-z].)+[a-z]{2,3})$"


    if (!
eregi$regexp$emailtoaddr )){ 
        echo 
"<p>The e-mail address you entered was incomplete or it contained invalid characters.<br> 
            <b>$emailtoaddr</b>"

        exit(); 
    } 

    
$emailtohostname substr$emailtoaddr, ( strrpos$emailtoaddr"@" ) +)); 

    
// Check's to see if the server has a functioning mail server. 
    
if (!checkdnsrr$emailtohostname"MX" )) { 
        die(
"The following e-mail address is not valid: "<b>$emailtoaddr</b>"<br>Because it's server didn't respond."); 
    } 

    
// Lets predefine some variables. Be sure to change the from address! 
    
$emailto "$emailtoname <$emailtoaddr>"
    
$emailfromaddr "someone@somewhere.com"
    
$emailfromname "Rob Dream"
    
$emailsubject "PHP Mail Test"
    
// The body can be as long as you wish, and any combination of text and variables 
    
$emailbody "This is a test of the PHP mail system.nIt looks like everything worked!"
    
$emailbody .= "nnYou left the following comment:nn$comment"

    
// Here we are forming one large header line 
    // Every header must be followed by a n except the last 
    
$emailheaders "From: $emailfromname <$emailfromaddr>n"
    
$emailheaders .= "Reply-To: $emailfromaddrn"
    
// I write myself in here as a BCC so I can make sure no one is abusing the script 
    
$emailheaders .= "Bcc: $emailfromaddrn"
    
$emailheaders .= "X-Mailer: PHP/" phpversion() . "n"
    
// Note the Sender-IP. I write this one in just as a small security percaution 
    
$emailheaders .= "X-Sender-IP: $REMOTE_ADDR"

    
// Because I predefined all of my variables, this mail() function looks nice and clean hmm? 
    // Commented out mail command
    //mail( $emailto, $emailsubject, $emailbody, $emailheaders); 

    
echo "<p>Sending e-mail to: $emailtoaddr<br> 
        Along with a bcc to: $emailfromaddr"


?>




Now I did not create this code, I just found it in my archives...

It works I've tryed it. Just fill in the nesscary information and volia you have yourself your own contact form.

Sunday May 27, 2007 - 156 reads