Solved

PHP contact code works but is inconsistant.

Posted on 2008-10-28
18
310 Views
Last Modified: 2013-12-13
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
Comment
Question by:wingcat
  • 8
  • 5
  • 3
  • +1
18 Comments
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 22829157
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
 
LVL 19

Expert Comment

by:v2Media
ID: 22829470
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
 
LVL 19

Assisted Solution

by:NerdsOfTech
NerdsOfTech earned 50 total points
ID: 22829530
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:wingcat
ID: 22830919
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
 
LVL 17

Expert Comment

by:nanharbison
ID: 22831705
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
 

Author Comment

by:wingcat
ID: 22832064
In which file, the feedback.php or in the html?
0
 
LVL 17

Expert Comment

by:nanharbison
ID: 22832331
Put it under this line:
mail($email,"Webpage Comments",$question,"From: <webpagerequest@yourdomain.com>");

0
 
LVL 19

Accepted Solution

by:
v2Media earned 200 total points
ID: 22832625
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
 
LVL 19

Expert Comment

by:v2Media
ID: 22833079
Seems to work a treat - just choofed off 5 in a row, no problems.
0
 

Author Comment

by:wingcat
ID: 22833099
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
 
LVL 19

Expert Comment

by:v2Media
ID: 22833309
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
 

Author Comment

by:wingcat
ID: 22834392
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
 

Author Comment

by:wingcat
ID: 22836446
"From:$email");

this will solve the problem I had works fine now.
       
0
 

Author Closing Comment

by:wingcat
ID: 31511034
Thanks for your help! couldn't have done it without you.
0
 
LVL 19

Expert Comment

by:v2Media
ID: 22836935
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
 

Author Comment

by:wingcat
ID: 22961657
how would you validate the email address?
0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 22962042
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
 

Author Comment

by:wingcat
ID: 22962354
where do I put this code?
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question