Web Design Forum: PHP Contact form sends blank emails - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

PHP Contact form sends blank emails Rate Topic: -----

#1 User is offline   wil 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 43
  • Joined: 25-August 10
  • Reputation: 4
  • Gender:Male
  • Location:North Yorkshire
  • Experience:Beginner
  • Area of Expertise:Coder

Posted 01 September 2010 - 02:56 PM

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
0

#2 User is offline   bocaj 

  • The First Messiah of Javology
  • PipPipPipPipPip
  • View gallery
  • Group: Members
  • Posts: 3,383
  • Joined: 11-January 09
  • Reputation: 99
  • Gender:Male
  • Location:The West Country.
  • Experience:Web Guru
  • Area of Expertise:Entrepreneur

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.)
0

#3 User is offline   webdesigner93 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,858
  • Joined: 22-September 09
  • Reputation: 212
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 01 September 2010 - 03:16 PM

View Postwil, 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

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
0

#4 User is online   zed 

  • Web Guru
  • Group: Moderators
  • Posts: 4,677
  • Joined: 25-May 10
  • Reputation: 673
  • Gender:Male
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

Posted 01 September 2010 - 04:37 PM

http://www.w3schools...hp/php_mail.asp
0

#5 User is offline   nooraljamali 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 14
  • Joined: 01-September 10
  • Reputation: 0
  • Gender:Male
  • Location:syria
  • Experience:Advanced
  • Area of Expertise:Web Designer

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>


0

#6 User is offline   webdesigner93 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,858
  • Joined: 22-September 09
  • Reputation: 212
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 01 September 2010 - 10:06 PM

View Postnooraljamali, 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.
0

#7 User is offline   nooraljamali 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 14
  • Joined: 01-September 10
  • Reputation: 0
  • Gender:Male
  • Location:syria
  • Experience:Advanced
  • Area of Expertise:Web Designer

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 :good:
0

#8 User is offline   wil 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 43
  • Joined: 25-August 10
  • Reputation: 4
  • Gender:Male
  • Location:North Yorkshire
  • Experience:Beginner
  • Area of Expertise:Coder

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...
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users