Hi Everyone
Aside from this being my first post here and at the risk of repeating another thread, I'm struggling with a php code and simple form.
Ive got it set up, but it sends blank emails, for some reason it's not capturing the info from the fields. My form is a simple name, email address, comments and then a choice from 3 check boxes on the form.
The email I receive gives the title of the check boxes and then says on. i.e. position on and yet there is nothing further in the mail.
Without wanting someone else to do it for me - I was wondering if anyone could give me some idea as to what I might be looking for in the code thats either missing or set wrong. I really want to try to figure it out myself before resorting to posting the code for others to check, so any and all ideas as to what might be wrong would be very welcome.
Thanks a lot.
Wil
Page 1 of 1
PHP Contact form sends blank emails
#2
Posted 01 September 2010 - 03:08 PM
Can you post the html and php code (remove all sensetive information from the php code prior to posting, such as email addresses, log files (if any) etc.)
#3
Posted 01 September 2010 - 03:16 PM
wil, on 01 September 2010 - 02:56 PM, said:
Hi Everyone
Aside from this being my first post here and at the risk of repeating another thread, I'm struggling with a php code and simple form.
Ive got it set up, but it sends blank emails, for some reason it's not capturing the info from the fields. My form is a simple name, email address, comments and then a choice from 3 check boxes on the form.
The email I receive gives the title of the check boxes and then says on. i.e. position on and yet there is nothing further in the mail.
Without wanting someone else to do it for me - I was wondering if anyone could give me some idea as to what I might be looking for in the code thats either missing or set wrong. I really want to try to figure it out myself before resorting to posting the code for others to check, so any and all ideas as to what might be wrong would be very welcome.
Thanks a lot.
Wil
Aside from this being my first post here and at the risk of repeating another thread, I'm struggling with a php code and simple form.
Ive got it set up, but it sends blank emails, for some reason it's not capturing the info from the fields. My form is a simple name, email address, comments and then a choice from 3 check boxes on the form.
The email I receive gives the title of the check boxes and then says on. i.e. position on and yet there is nothing further in the mail.
Without wanting someone else to do it for me - I was wondering if anyone could give me some idea as to what I might be looking for in the code thats either missing or set wrong. I really want to try to figure it out myself before resorting to posting the code for others to check, so any and all ideas as to what might be wrong would be very welcome.
Thanks a lot.
Wil
Hey Wil.
First off welcome to WDF and second no one can just tell you what may and may not be wrong considering there's a variety of things that could be wrong with your code, so no one can really help without seeing the source but some of the common problems for the script to be sending a blank email prob includes not sending the mail right heres an example of the proper way to send your emails.
Ok so say we have one form field called name, in which we wanna send the contents of the field to an email address heres how you would do it.
<?php
$youremail = "Yourname@yourdomain.com";
$subject = "Your subject";
$name = (isset($_POST['name'])) ? $_POST['name'] : FALSE;
$headers = "From: My website <$youremail>";
$body = "
Name: $name
";
if($name)
{
//Send email
mail($youremail,$subject,$body,$headers);
}
?>
Hope this helps
Regards Steve
#5
Posted 01 September 2010 - 06:04 PM
<?
if ($_POST['sendcontact']) {
if ($_POST['sender_n'] != "" AND eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['sender_m']) AND $_POST['sender_last'] != "" AND $_POST['sender_mo'] != "" AND $_POST['sender_ad'] != "" AND $_POST['sender_coun'] != "") {
$email = $_POST['sender_m'];
$email = stripslashes($email);
$email = strip_tags($email);
$headers = "From: $email\n";
$headers .= "MIME-Version:1.0\n";
$headers .= "Content-type: text/html; charset=utf-8\n";
$headers .= "Return-Path: $email\n";
$headers .= "X-Return-Path: $email\n";
$headers .= "Reply-To: $email\n";
$headers .= "X-Sender: $email\n";
$headers .= "X-Authenticated-Sender: $email\n";
$headers .= "X-Priority: 3\n";
$headers .= "X-Scanned-By: MIMEDefang 2.35\n";
$headers = rtrim($headers);
$to="you@yourdomain.tdl";
$subject = $email;
$message .="<table width='400px'>";
$message .="<tr>";
$message .="<td width='50%'>First Name :</td>";
$message .="<td width='50%'>".$_POST['sender_n']."</td>";
$message .="</tr>";
$message .="<tr>";
$message .="<td width='50%'>Last Name :</td>";
$message .="<td width='50%'>".$_POST['sender_last']."</td>";
$message .="</tr>";
$message .="<tr>";
$message .="<td width='50%'>Mobile :</td>";
$message .="<td width='50%'>".$_POST['sender_mo']."</td>";
$message .="</tr>";
$message .="<tr>";
$message .="<td width='50%'>Address :</td>";
$message .="<td width='50%'>".$_POST['sender_ad']."</td>";
$message .="</tr>";
$message .="<tr>";
$message .="<td width='50%'>Country :</td>";
$message .="<td width='50%'>".$_POST['sender_coun']."</td>";
$message .="</tr>";
$message .="<tr>";
$message .="<td width='50%'>Note :</td>";
$message .="<td width='50%'>".$_POST['sender_msg']."</td>";
$message .="</tr>";
$message .="</table>";
if(mail($to,$subject,$message,$headers)) {
$s = 1;
}
else {
$s= 0;
}
}
if ($s == 0) {
$error = "You Must Fill All fileds";
}
else {
$error = "Thankx!";
}
}
?>
<form action="" method="post">
<table cellpadding="0" cellspacing="0" align="center" dir="ltr">
<tr>
<td colspan="2" style="text-align:center;color:#CC0000;font-family:Tahoma;font-size:10pt;">
<?=$error;?>
</td>
</tr>
<tr>
<td style="text-align:left; width:125px;font-family:Tahoma;font-size:9pt; padding-left: 10px;">
First Name :
</td>
<td style="text-align:left;">
<input name="sender_n" type="text" />
</td>
</tr>
<tr>
<td style="text-align:left; width:125px;font-family:Tahoma;font-size:9pt; padding-left: 10px;">
Last Name :</td>
<td style="text-align:left;">
<input name="sender_last" type="text" /></td>
</tr>
<tr>
<td style="text-align:left; width:125px;font-family:Tahoma;font-size:9pt; padding-left: 10px;">
E-mail :</td>
<td style="text-align:left;">
<input name="sender_m" type="text" /></td>
</tr>
<tr>
<td style="text-align:left; width:125px;font-family:Tahoma;font-size:9pt; padding-left: 10px;">
Mobile :</td>
<td style="text-align:left;">
<input name="sender_mo" type="text" /></td>
</tr>
<tr>
<td style="text-align:left; width:125px;font-family:Tahoma;font-size:9pt; padding-left: 10px;">
Address :</td>
<td style="text-align:left;">
<input name="sender_ad" type="text" /></td>
</tr>
<tr>
<td style="text-align:left; width:125px;font-family:Tahoma;font-size:9pt; padding-left: 10px;">
Country :</td>
<td style="text-align:left;">
<input name="sender_coun" type="text" /></td>
</tr>
<tr>
<td style="text-align:left; width:125px;font-family:Tahoma;font-size:9pt; padding-left: 10px;">
Note :
</td>
<td style="text-align:left;">
<textarea cols="20" dir="ltr" name="sender_msg" rows="2" style="width: 200px; height: 150px"></textarea></td>
</tr>
<tr>
<td style="text-align:right;width:125px;padding-right:10px;font-family:Tahoma;font-size:9pt;">
</td>
<td style="text-align:left;">
<input name="sendcontact" type="submit" value="send" /></td>
</tr>
</table>
</form>
#6
Posted 01 September 2010 - 10:06 PM
nooraljamali, on 01 September 2010 - 06:04 PM, said:
<?
if ($_POST['sendcontact']) {
if ($_POST['sender_n'] != "" AND eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['sender_m']) AND $_POST['sender_last'] != "" AND $_POST['sender_mo'] != "" AND $_POST['sender_ad'] != "" AND $_POST['sender_coun'] != "") {
$email = $_POST['sender_m'];
$email = stripslashes($email);
$email = strip_tags($email);
$headers = "From: $email\n";
$headers .= "MIME-Version:1.0\n";
$headers .= "Content-type: text/html; charset=utf-8\n";
$headers .= "Return-Path: $email\n";
$headers .= "X-Return-Path: $email\n";
$headers .= "Reply-To: $email\n";
$headers .= "X-Sender: $email\n";
$headers .= "X-Authenticated-Sender: $email\n";
$headers .= "X-Priority: 3\n";
$headers .= "X-Scanned-By: MIMEDefang 2.35\n";
$headers = rtrim($headers);
$to="you@yourdomain.tdl";
$subject = $email;
$message .="<table width='400px'>";
$message .="<tr>";
$message .="<td width='50%'>First Name :</td>";
$message .="<td width='50%'>".$_POST['sender_n']."</td>";
$message .="</tr>";
$message .="<tr>";
$message .="<td width='50%'>Last Name :</td>";
$message .="<td width='50%'>".$_POST['sender_last']."</td>";
$message .="</tr>";
$message .="<tr>";
$message .="<td width='50%'>Mobile :</td>";
$message .="<td width='50%'>".$_POST['sender_mo']."</td>";
$message .="</tr>";
$message .="<tr>";
$message .="<td width='50%'>Address :</td>";
$message .="<td width='50%'>".$_POST['sender_ad']."</td>";
$message .="</tr>";
$message .="<tr>";
$message .="<td width='50%'>Country :</td>";
$message .="<td width='50%'>".$_POST['sender_coun']."</td>";
$message .="</tr>";
$message .="<tr>";
$message .="<td width='50%'>Note :</td>";
$message .="<td width='50%'>".$_POST['sender_msg']."</td>";
$message .="</tr>";
$message .="</table>";
if(mail($to,$subject,$message,$headers)) {
$s = 1;
}
else {
$s= 0;
}
}
if ($s == 0) {
$error = "You Must Fill All fileds";
}
else {
$error = "Thankx!";
}
}
?>
<form action="" method="post">
<table cellpadding="0" cellspacing="0" align="center" dir="ltr">
<tr>
<td colspan="2" style="text-align:center;color:#CC0000;font-family:Tahoma;font-size:10pt;">
<?=$error;?>
</td>
</tr>
<tr>
<td style="text-align:left; width:125px;font-family:Tahoma;font-size:9pt; padding-left: 10px;">
First Name :
</td>
<td style="text-align:left;">
<input name="sender_n" type="text" />
</td>
</tr>
<tr>
<td style="text-align:left; width:125px;font-family:Tahoma;font-size:9pt; padding-left: 10px;">
Last Name :</td>
<td style="text-align:left;">
<input name="sender_last" type="text" /></td>
</tr>
<tr>
<td style="text-align:left; width:125px;font-family:Tahoma;font-size:9pt; padding-left: 10px;">
E-mail :</td>
<td style="text-align:left;">
<input name="sender_m" type="text" /></td>
</tr>
<tr>
<td style="text-align:left; width:125px;font-family:Tahoma;font-size:9pt; padding-left: 10px;">
Mobile :</td>
<td style="text-align:left;">
<input name="sender_mo" type="text" /></td>
</tr>
<tr>
<td style="text-align:left; width:125px;font-family:Tahoma;font-size:9pt; padding-left: 10px;">
Address :</td>
<td style="text-align:left;">
<input name="sender_ad" type="text" /></td>
</tr>
<tr>
<td style="text-align:left; width:125px;font-family:Tahoma;font-size:9pt; padding-left: 10px;">
Country :</td>
<td style="text-align:left;">
<input name="sender_coun" type="text" /></td>
</tr>
<tr>
<td style="text-align:left; width:125px;font-family:Tahoma;font-size:9pt; padding-left: 10px;">
Note :
</td>
<td style="text-align:left;">
<textarea cols="20" dir="ltr" name="sender_msg" rows="2" style="width: 200px; height: 150px"></textarea></td>
</tr>
<tr>
<td style="text-align:right;width:125px;padding-right:10px;font-family:Tahoma;font-size:9pt;">
</td>
<td style="text-align:left;">
<input name="sendcontact" type="submit" value="send" /></td>
</tr>
</table>
</form>
I would like to make a few notes on this, for one you really should not be using eregi cause its depreciated in php 5 i'd use preg_match insted. Another thing i'd point out is you should really be using isset when checking certain things like if the form is submitted,You should be using this to check if the forms submitted
if(isset($_POST['sendcontact)){} to prevent errors piling up inside the error log. And also you should be writing your variables like this $email = (isset($_POST['sender_m'])) ? stripslashes(strip_tags($_POST['email'])) : FALSE;this will also prevent the same thing as listed above, which is errors piling up inside the php error log. And finally i would not be using short tags like this
<? ?>but use the php tags like this
<?php ?>reason being some host do not have short tags turned on.
#7
Posted 01 September 2010 - 11:50 PM
Hello webdesigner93
I'm Just try to help , was asked my friend he is a php programmer and i told him about this matter and get this code , so thank you for the notes
and hope that's help Wil too
I'm Just try to help , was asked my friend he is a php programmer and i told him about this matter and get this code , so thank you for the notes
#8
Posted 02 September 2010 - 08:39 PM
Right thats it - I give in - Ive studied the codes here (thank you all very much) and I've trawled the internet and I cant find out why my code here is not working ...Please help ..Thanks
*/
$my_email = "";
$from_email = "";
$continue = "/";
$errors = array();
// Remove $_COOKIE elements from $_REQUEST.
if(count($_COOKIE)){foreach(array_keys($_COOKIE) as $value){unset($_REQUEST[$value]);}}
// Validate email field.
if(isset($_REQUEST['email']) && !empty($_REQUEST['email']))
{
$_REQUEST['email'] = trim($_REQUEST['email']);
if(substr_count($_REQUEST['email'],"@") != 1 || stristr($_REQUEST['email']," ") || stristr($_REQUEST['email'],"\\") || stristr($_REQUEST['email'],":")){$errors[] = "Email address is invalid";}else{$exploded_email = explode("@",$_REQUEST['email']);if(empty($exploded_email[0]) || strlen($exploded_email[0]) > 64 || empty($exploded_email[1])){$errors[] = "Email address is invalid";}else{if(substr_count($exploded_email[1],".") == 0){$errors[] = "Email address is invalid";}else{$exploded_domain = explode(".",$exploded_email[1]);if(in_array("",$exploded_domain)){$errors[] = "Email address is invalid";}else{foreach($exploded_domain as $value){if(strlen($value) > 63 || !preg_match('/^[a-z0-9-]+$/i',$value)){$errors[] = "Email address is invalid"; break;}}}}}}
}
// Check referrer is from same site.
if(!(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) && stristr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST']))){$errors[] = "You must enable referrer logging to use the form";}
// Check for a blank form.
function recursive_array_check_blank($element_value)
{
global $set;
if(!is_array($element_value)){if(!empty($element_value)){$set = 1;}}
else
{
foreach($element_value as $value){if($set){break;} recursive_array_check_blank($value);}
}
}
recursive_array_check_blank($_REQUEST);
if(!$set){$errors[] = "You cannot send a blank form. Please complete all fields including at least 1 check box.";}
unset($set);
// Display any errors and exit if errors exist.
if(count($errors)){foreach($errors as $value){print "$value<br>";} exit;}
if(!defined("PHP_EOL")){define("PHP_EOL", strtoupper(substr(PHP_OS,0,3) == "WIN") ? "\r\n" : "\n");}
// Build message.
function build_message($request_input){if(!isset($message_output)){$message_output ="";}if(!is_array($request_input)){$message_output = $request_input;}else{foreach($request_input as $key => $value){if(!empty($value)){if(!is_numeric($key)){$message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL;}else{$message_output .= build_message($value).", ";}}}}return rtrim($message_output,", ");}
$message = build_message($_REQUEST);
$message = $message . PHP_EOL.PHP_EOL."-- ".PHP_EOL."Thank you for using FormToEmail from http://FormToEmail.com";
$message = stripslashes($message);
$subject = "FormToEmail Comments";
$subject = stripslashes($subject);
if($from_email)
{
$headers = "From: " . $from_email;
$headers .= PHP_EOL;
$headers .= "Reply-To: " . $_REQUEST['email'];
}
else
{
$from_name = "";
if(isset($_REQUEST['name']) && !empty($_REQUEST['name'])){$from_name = stripslashes($_REQUEST['name']);}
$headers = "From: {$from_name} <{$_REQUEST['email']}>";
}
mail($my_email,$subject,$message,$headers);
?>
Any help greatfully received...
*/
$my_email = "";
$from_email = "";
$continue = "/";
$errors = array();
// Remove $_COOKIE elements from $_REQUEST.
if(count($_COOKIE)){foreach(array_keys($_COOKIE) as $value){unset($_REQUEST[$value]);}}
// Validate email field.
if(isset($_REQUEST['email']) && !empty($_REQUEST['email']))
{
$_REQUEST['email'] = trim($_REQUEST['email']);
if(substr_count($_REQUEST['email'],"@") != 1 || stristr($_REQUEST['email']," ") || stristr($_REQUEST['email'],"\\") || stristr($_REQUEST['email'],":")){$errors[] = "Email address is invalid";}else{$exploded_email = explode("@",$_REQUEST['email']);if(empty($exploded_email[0]) || strlen($exploded_email[0]) > 64 || empty($exploded_email[1])){$errors[] = "Email address is invalid";}else{if(substr_count($exploded_email[1],".") == 0){$errors[] = "Email address is invalid";}else{$exploded_domain = explode(".",$exploded_email[1]);if(in_array("",$exploded_domain)){$errors[] = "Email address is invalid";}else{foreach($exploded_domain as $value){if(strlen($value) > 63 || !preg_match('/^[a-z0-9-]+$/i',$value)){$errors[] = "Email address is invalid"; break;}}}}}}
}
// Check referrer is from same site.
if(!(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) && stristr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST']))){$errors[] = "You must enable referrer logging to use the form";}
// Check for a blank form.
function recursive_array_check_blank($element_value)
{
global $set;
if(!is_array($element_value)){if(!empty($element_value)){$set = 1;}}
else
{
foreach($element_value as $value){if($set){break;} recursive_array_check_blank($value);}
}
}
recursive_array_check_blank($_REQUEST);
if(!$set){$errors[] = "You cannot send a blank form. Please complete all fields including at least 1 check box.";}
unset($set);
// Display any errors and exit if errors exist.
if(count($errors)){foreach($errors as $value){print "$value<br>";} exit;}
if(!defined("PHP_EOL")){define("PHP_EOL", strtoupper(substr(PHP_OS,0,3) == "WIN") ? "\r\n" : "\n");}
// Build message.
function build_message($request_input){if(!isset($message_output)){$message_output ="";}if(!is_array($request_input)){$message_output = $request_input;}else{foreach($request_input as $key => $value){if(!empty($value)){if(!is_numeric($key)){$message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL;}else{$message_output .= build_message($value).", ";}}}}return rtrim($message_output,", ");}
$message = build_message($_REQUEST);
$message = $message . PHP_EOL.PHP_EOL."-- ".PHP_EOL."Thank you for using FormToEmail from http://FormToEmail.com";
$message = stripslashes($message);
$subject = "FormToEmail Comments";
$subject = stripslashes($subject);
if($from_email)
{
$headers = "From: " . $from_email;
$headers .= PHP_EOL;
$headers .= "Reply-To: " . $_REQUEST['email'];
}
else
{
$from_name = "";
if(isset($_REQUEST['name']) && !empty($_REQUEST['name'])){$from_name = stripslashes($_REQUEST['name']);}
$headers = "From: {$from_name} <{$_REQUEST['email']}>";
}
mail($my_email,$subject,$message,$headers);
?>
Any help greatfully received...
- ← Need help pointing Mac to site residing on an IP
- Server Side (PHP, Databases, ASP.NET, etc)
- resize image problem →
Share this topic:
Page 1 of 1
Help




















