Web Design Forums

PHP

Have questions about PHP? Ask them here and our experts will assist you before you know it! You can also find help in the documentation at PHP.net.

PHP Contact Form Email Verification



Site of the Month Voting - Now Open. CAST YOUR VOTE NOW!

Reply
 
LinkBack Thread Tools
Old October 25 '08, 10:49 AM (#1)
Abstract is offline
WDF Regular
 
Abstract's Avatar
 
Join Date: August 2003
Posts: 170
Abstract
PHP Contact Form Email Verification

I sometimes get blank responses to my contact form. I am not sure if someone is just playing with it, or its some kind of bot . I'd like to possibly put an email verification script in it so it will at least verify there is a functional email address in the email field. How would I do that? And if you have any other recommendations, please let me know. I appreciate any advice.

Here's my code:

Code:
$sendTo = "contact@mywebsite.org";
$subject = "Contact Request";

// variables are sent to this PHP page through
// the POST method.  $_POST is a global associative array
// of variables passed through this method.  From that, we
// can get the values sent to this page from Flash and
// assign them to appropriate variables which can be used
// in the PHP mail() function.

$headers = "From: " . $_POST["name"] ." <" . $_POST["email"] .">\r\n";
$headers .= "Reply-To: " . $_POST["email"] . "\r\n";
$headers .= "Return-path: " . $_POST["email"];

// content of the message to a body variable
$message="name: ".$name."
email: ".$email."
phone: ".$phone."
comments: ".$comments."
";


// once the variables have been defined, they can be included
// in the mail function call which will send you an email
mail($sendTo, $subject, $message, $headers);
header("Refresh: 0;url=http://www.mywebsite.org/thankyou.html");
?>
Thank you in advance.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old October 25 '08, 12:04 PM (#2)
bfsog is offline
Coder
 
bfsog's Avatar
 
Join Date: May 2003
Location: UK
Posts: 2,354
bfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to behold
Send a message via MSN to bfsog
A tip I use is to set the From field to an e-mail address of my choice. Such as "customerqueries@mydomain.com".

Now, you would still have the e-mail input in the form (incase the person who filled it in needs a reply) so you just append/add the entered e-mail address to the $message variable.

And I see you sometimes receive empty e-mails. Well this is easily fixed.

What you can do is trim the fileds (trim() removes any surplus spaces in a string) and then check the length of the fileds. Note: The fields that you do not want to be empty should be indicated with an asterix (*) or other symbol on the effect and with explanatory text stating what the * or other symbol means.

With that in mind, your code would look something like this.

PHP Code:
<?php
$sendTo 
"contact@mywebsite.org";
$subject "Contact Request";

// variables are sent to this PHP page through
// the POST method.  $_POST is a global associative array
// of variables passed through this method.  From that, we
// can get the values sent to this page from Flash and
// assign them to appropriate variables which can be used
// in the PHP mail() function.

// I assume there is 3 fields
// name, email and message

$errors 0;

if(
strlen(trim($_POST['name']) == 0))
{
   
$errors $errors 1;
}
if(
strlen(trim($_POST['email']) == 0))
{
   
$errors $errors 1;
}
if(
strlen(trim($_POST['message']) == 0))
{
   
$errors $errors 1;
}

if(
$errors == 0)
{
// Send email)

  
$headers "From: Customer Query <customer-query@mydomain.com>\r\n";
  
$headers .= "Reply-To: " $_POST["email"] . "\r\n";
  
$headers .= "Return-path: " $_POST["email"];

  
// content of the message to a body variable
  
$message="name: ".$name."
  email: "
.$email."
  phone: "
.$phone."  
  comments: "
.$comments."
  "
;


  
// once the variables have been defined, they can be included
  // in the mail function call which will send you an email
  
mail($sendTo$subject$message$headers);
  
header("Refresh: 0;url=http://www.mywebsite.org/thankyou.html");
}
?>
Note that this is untested and there are some more possible improvements. I thought I posted a tutorial about this but cannot seem to find it. There are however lots and lots of tutorials on handling PHP email so google is your friend.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old October 25 '08, 01:24 PM (#3)
smoseley is online now
WDF Moderator
 
smoseley's Avatar
 
Join Date: March 2003
Location: Miami, FL
Posts: 8,719
smoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud of
Here's a form validation script I wrote a while ago.... I still use it today...

http://www.webdesignforums.net/codin...tion_7885.html
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old October 25 '08, 02:05 PM (#4)
imagn is offline
WDF Regular
 
imagn's Avatar
 
Join Date: July 2007
Location: Los Angeles
Posts: 156
imagn will become famous soon enough
If you are getting a lot of spam from your form you might want to consider "encoding" the form and corresponding fields in JavaScript Unicode.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old October 27 '08, 09:57 PM (#5)
Abstract is offline
WDF Regular
 
Abstract's Avatar
 
Join Date: August 2003
Posts: 170
Abstract
OK. I am going to try out these scripts and see if it works. Thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

  Web Design Forums » Programming Help » PHP

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Top 21 PHP Programming mistakes thexchord PHP 19 June 18 '10 02:20 PM
PHP email form not getting thorugh to recipent bfsog PHP 5 October 26 '07 10:16 AM
Contact Form for email info itsdonny Other Languages 2 May 16 '06 12:15 AM
Help' Inserting CAPTCHA into php email form peter77 PHP 6 November 30 '05 02:59 AM
Problem with email contact form.... NewComputer PHP 5 September 4 '04 11:15 PM

 
User Infomation
Your Avatar

Site Of The Month
Nominate Your Site Now!

Advertisement
WolfCMS.org

Latest Articles
- by RickM
- by bfsog

Advertisement

Partner Links



All times are GMT -4. The time now is 01:53 PM.


WebDesignForums.net is Copyright © 2010 RikeMedia.

SEO by vBSEO

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164