View Single Post
Old 06-22-2009, 08:55 AM   #2 (permalink)
Redcore
Design Team Member
 
Redcore's Avatar
 
Join Date: Aug 2007
Location: Jamestown, CA
Posts: 718
OS: Linux Mint 7

My System

Re: PHP Email form problem

You can probably simplify the email address validation with PHP5's filter_var:

PHP Code:
filter_var('bob@example.com'FILTER_VALIDATE_EMAIL
I don't have time to test this morning, but I'll spit out a little code for you to check...if it doesn't work, I'll go over it more...

PHP Code:
<?php

/*variable names for xhtml input*/
$name $_POST['name'];
$email $_POST['email'];
$phone $_POST['phone'];
$contact_choice $_POST['contact'];
$quick_form $_POST['quickform'];
$time $_POST['time'];
$message $_POST['message'];

$email filter_var($emailFILTER_VALIDATE_EMAIL);

if(
$email==false)
    **
    echo 
"<h4>Invalid email address</h4>";
    echo 
"<a href='javascript:history.back(1);'>Back</a>";
    }    elseif(
strlen($message)==0)    **
    echo 
"<h4>You have not entered a message </h4>";
    echo 
"<a href='javascript:history.back(1);'>Back</a>";
    }    

// now that we've made it through error checking, put together the mail properties
$mailmessage 'Name: ' $name "\n";
$mailmessage .= 'Email: ' $email "\n";
$mailmessage .= 'Phone: ' $phone "\n";
$mailmessage .= 'Contact?: ' $contact_choice "\n";
$mailmessage .= 'Time: ' $time "\n";
$mailmessage .= "\n";
$mailmessage .= 'Message: ' "\n";
$mailmessage .= $message;

$subject 'New mail';

$headers 'MIME-Version: 1.0' "\r\n";
$headers .= 'From: ' $name '<' $email '>' "\r\n";

if(@
mail('you@youremail.com'$subject$mailmessage$headers))
    **
    
// the '@' will suppress errors
    
echo "<h4>Thank you for contacting us, you shall receive a reply very soon.</h4>";
    }    else    **
    echo 
"<h4>Can't send email to $email</h4>";
    }
?>
Take notice:
  • I've changed $HTTP_POST_VARS to $_POST. Some people have reported problems when using that - and $_POST always works.
  • I've implemented the filter_var function I described above. This function can do more than validating emails, if you're ever interested in other types of validations and sanitizing features it has.
  • You put together the mail function incorrectly. I put in a way that should play better with it. Remember, PHP.net's manual is your friend! Always refer to it for the proper way of setting up functions.
  • I haven't put in any functionality to prevent form hijacking/injections. I try to keep this stuff out of the scripts I sort out for people because it only confuses them (simpler is better) - although I've received email in the past criticizing that I haven't made a bigger deal out of such things, so I wanted to just mention that you'll need to put some code to keep your code/server safe. There is stuff around the Internet concerning this very thing - but I'll make a new post in this forum with a favorite article of mine (hint: that filter_var function comes in handy yet again!) and a little piece of code I use on everything.
  • Remember when copying this code to test it...this forum changes the opening bracket for the if statements to "**" - correct that before you run it, or it won't work.
__________________
Free Resources
PC Protection - Comodo Firewall | AVG Anti-Virus | WinPatrol | Ad-Aware | Spybot S&D | SpywareBlaster |
Web Design/Programming - KompoZer
(Editor) | Paint.NET (Graphic) | GIMP+GIMPShop (Graphic) | FileZilla (FTP Client) | Free Hosting |
Redcore is offline   Reply With Quote