• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 325
  • Last Modified:

PHP contact code works but is inconsistant.

Hi,

I am using the following php code as a feedback.php file. It is a contact form in which viewers state their name, email, question/comment, and a department to whom it should be sent to. I got this form from an expert here but the code only works a few times and doesn't work anymore. I am currently using the same address for all departments because I am testing it.

I have noticed when I clear my temporary internet files and or cache, I can successfully send a message again. However after having sent a contact form once, it will not send the second one before clearing the cache. Is there something wrong with this code? Any suggestions? The form in question is at www.hbni.net/contact.htm
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$question = $_POST["comments"];
$dept = $_POST["dept"];
 
if ($dept == "Technical Support")  $emailaddress="me@me.com";
elseif ($dept == "Webfilter") $emailaddress = "me@me.com";
elseif ($dept == "LifeSize") $emailaddress = "me@me.com";
elseif ($dept == "Member Portal Login Request") $emailaddress = "me@me.com";
elseif ($dept == "Webmaster") $emailaddress = "me@me.com";
else {exit;}
 
mail($email,"Webpage Comments",$question,"From: <webpagerequest@yourdomain.com>");
header("Location: http://www.hbni.net/thanks.htm");
?>

Open in new window

0
wingcat
Asked:
wingcat
  • 8
  • 5
  • 3
  • +1
2 Solutions
 
NerdsOfTechTechnology ScientistCommented:
Try adding expiry to the page, bracketing the if statements, and single quoting static text.
<?php
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header ('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header ('Cache-Control: no-cache, must-revalidate');
header ('Pragma: no-cache');
header ('content-type: application/x-javascript; charset=tis-620');
 
$name = $_POST['name'];
$email = $_POST['email'];
$question = $_POST['comments'];
$dept = $_POST['dept'];
 
if ($dept == 'Technical Support')  {$emailaddress='me@me.com';}
elseif ($dept == 'Webfilter') {$emailaddress = 'me@me.com';}
elseif ($dept == 'LifeSize') {$emailaddress = 'me@me.com';}
elseif ($dept == 'Member Portal Login Request') {$emailaddress = 'me@me.com';}
elseif ($dept == 'Webmaster') {$emailaddress = 'me@me.com';}
else {exit;}
 
mail($email,'Webpage Comments',$question,'From: <webpagerequest@yourdomain.com>');
header('Location: http://www.hbni.net/thanks.htm');
?>

Open in new window

0
 
v2MediaCommented:
The last line needs and exit; after the call to header.
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$question = $_POST["comments"];
$dept = $_POST["dept"];
 
if ($dept == "Technical Support")  $emailaddress="me@me.com";
elseif ($dept == "Webfilter") $emailaddress = "me@me.com";
elseif ($dept == "LifeSize") $emailaddress = "me@me.com";
elseif ($dept == "Member Portal Login Request") $emailaddress = "me@me.com";
elseif ($dept == "Webmaster") $emailaddress = "me@me.com";
else {exit;}
 
mail($email,"Webpage Comments",$question,"From: <webpagerequest@yourdomain.com>");
header("Location: http://www.hbni.net/thanks.htm");
exit;
?>

Open in new window

0
 
NerdsOfTechTechnology ScientistCommented:
Why?
"exit; " after header redirect is only necessary if there is code beyond the header redirect line.
reference: http://us3.php.net/header

I guess you can put it in and it won't hurt.
Don't try to steal points. LOL

=NerdsOfTech
<?php
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header ('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header ('Cache-Control: no-cache, must-revalidate');
header ('Pragma: no-cache');
header ('content-type: application/x-javascript; charset=tis-620');
 
$name = $_POST['name'];
$email = $_POST['email'];
$question = $_POST['comments'];
$dept = $_POST['dept'];
 
if ($dept == 'Technical Support')  {$emailaddress='me@me.com';}
elseif ($dept == 'Webfilter') {$emailaddress = 'me@me.com';}
elseif ($dept == 'LifeSize') {$emailaddress = 'me@me.com';}
elseif ($dept == 'Member Portal Login Request') {$emailaddress = 'me@me.com';}
elseif ($dept == 'Webmaster') {$emailaddress = 'me@me.com';}
else {exit;}
 
mail($email,'Webpage Comments',$question,'From: <webpagerequest@yourdomain.com>');
header('Location: http://www.hbni.net/thanks.htm');
exit;
?>

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
wingcatAuthor Commented:
tried both methods and both produced the same results. still inconsistant: worked 1 time only. Below is my code for the form just in case it has an error.
<form action="feedback.php" method="post">
<div align="center">
<table border="0" cellpadding="4" cellspacing="4" summary="feedback form">
<tr align="left" valign="top">
<td width="129"><font size="2" class="bodys">Your Name:</font></td>
<td width="235"><input type="text" name="name" size="30" /></td>
									  </tr>
										<tr align="left" valign="top">
											<td><font size="2" class="bodys">Email address:</font></td>
										  <td><input type="text" name="email" size="30" /></td>
									  </tr>
										<tr align="left" valign="top">
<td><font size="2" class="bodys">Department:</font></td>
<td><label>
<select name="dept" size="1" id="dept">
<option selected="selected">Technical Support</option>
										      <option>Webfilter</option>
										      <option>LifeSize</option>
										      <option>Webmaster</option>
										      <option>Member Portal Login Request</option>
									            </select>
</label></td>
</tr>
<tr align="left" valign="top">
  <td colspan="2"><p><font size="2" class="bodys">Question or Request:</font><font size="1"><br />
</font>
<textarea rows="5" cols="60" name="comments"></textarea>
</p></td>
</tr>
<tr align="left" valign="top">
<td colspan="2" align="center"><input type="submit" value="Send " /></td>
</tr>
</table>
</div>
</form><br />

Open in new window

0
 
nanharbisonCommented:
Try  unsetting the variables so they can take on a new value


unset($name);
unset($email);
unset($question);
unset($dept);

Open in new window

0
 
wingcatAuthor Commented:
In which file, the feedback.php or in the html?
0
 
nanharbisonCommented:
Put it under this line:
mail($email,"Webpage Comments",$question,"From: <webpagerequest@yourdomain.com>");

0
 
v2MediaCommented:
Here's a copy paste version of feedback.php to try as well. It has a few levels of error debugging to help illuminate the problem: -

<?php
 
if(!empty($_POST["dept"])) {
	
	$arr_required = array('name','email','comments','dept');
	
	foreach($_POST as $k => $v) {
	
		if(in_array($k,$arr_required) && empty($v)) {
		
			die("Field $k is required to send a message.");
			
		}
	}
	
	$name = $_POST["name"];
	$email = $_POST["email"];
	$question = stripslashes($_POST["comments"]);
	$dept = $_POST["dept"];
	
	switch($dept) {
	
		case 'Technical Support':
			$emailaddress="me@me.com";
		break;
		case 'Webfilter':
			$emailaddress="me@me.com";
		break;
		case 'LifeSize':
			$emailaddress="me@me.com";
		break;
		case 'Member Portal Login Request':
			$emailaddress="me@me.com";
		break;
		case 'Webmaster':
			$emailaddress="me@me.com";
		break;
		
		default:
			$emailaddress = false;
	
	//EOS
	}
	
	$send = false;
	
	//kill if !emailaddress else send
	if(!$emailaddress) {
		die('Unsupported contact type');
	} else {
		$send = mail($emailaddress,"Webpage Comments",$question,"From:webpagerequest@yourdomain.com");
	}
	
	if($send) {
		header("Location: http://www.hbni.net/thanks.htm");
		exit;
	} else {
		die("Mailserver error");
	}
	
//end form processing
} else {
 
	die('No direct access');
	
}
?>

Open in new window

0
 
v2MediaCommented:
Seems to work a treat - just choofed off 5 in a row, no problems.
0
 
wingcatAuthor Commented:
This worked for me now too. What is the difference with the code v2media made with the one I had. I tried several and it worked. It seems ok now.
0
 
v2MediaCommented:
It could have been many things together, or something as simple as the mail server not supporting angle brackets around the from address in the call to mail(). Having php errors displaying on the server would have helped to find out.

0
 
wingcatAuthor Commented:
Just one more thing. When I receive emails, it is always from the same sender. Is it possible to have the form use the email address from the "email" field and have that seen in the "from" part of the email?

"From:webpagerequest@yourdomain.com");is the part in question.

Currently, I am not able to reply to the people sending me a comment because their email address does not show. Can someone show how to do this?

0
 
wingcatAuthor Commented:
"From:$email");

this will solve the problem I had works fine now.
       
0
 
wingcatAuthor Commented:
Thanks for your help! couldn't have done it without you.
0
 
v2MediaCommented:
Be aware that using the email address entered into the html form by the user is exploitable by malicious users/bots. If the malicious user/bot happens to enter a thousand email addresses separated by a semicolon in an attempt to send spam, someone won't be happy.

You should either validate the email address, or use a hard coded from address. The only caveat of using a hard-coded from address is that the receiver cannot use the "Reply" button in their mail client; they have to paste the email address into the "To" field.
0
 
wingcatAuthor Commented:
how would you validate the email address?
0
 
NerdsOfTechTechnology ScientistCommented:
if (preg_match("/^[\w-]+(\.[\w-]+)*@([0-9a-z][0-9a-z-]*[0-9a-z]\.)+([a-z]{2,4})$/i", trim($_POST['email']))) {
            $email = preg_replace("/\r\n/", "", $_POST['email']);
} else {
 // invalid email
}
0
 
wingcatAuthor Commented:
where do I put this code?
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 8
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now