PHP Contact Form Troubles

Soldato
Joined
11 Jul 2004
Posts
16,151
Location
Neptune
:(

I've been trying to implement a contact form with little success.

I was concerned I was making a mistake so I borrowed a bit of code from a website instruction how to do it just to make sure everything was accounted for:

Code:
<form name="contactform" method="post" action="send_form_email.php">
<table width="450px">
</tr>
<tr>
 <td valign="top">
  <label for="first_name">First Name *</label>
 </td>
 <td valign="top">
  <input  type="text" name="first_name" maxlength="50" size="30">
 </td>
</tr>

<tr>
 <td valign="top"">
  <label for="last_name">Last Name *</label>
 </td>
 <td valign="top">
  <input  type="text" name="last_name" maxlength="50" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="email">Email Address *</label>
 </td>
 <td valign="top">
  <input  type="text" name="email" maxlength="80" size="30">
 </td>

</tr>
<tr>
 <td valign="top">
  <label for="telephone">Telephone Number</label>
 </td>
 <td valign="top">
  <input  type="text" name="telephone" maxlength="30" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="comments">Comments *</label>
 </td>
 <td valign="top">
  <textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea>
 </td>

</tr>
<tr>
 <td colspan="2" style="text-align:center">
  <input type="submit" value="Submit">   <a href="http://www.freecontactform.com/email_form.php">Email Form</a>
 </td>
</tr>
</table>
</form>

And the PHP:

PHP:
<?php
if(isset($_POST['email'])) {
	
	// EDIT THE 2 LINES BELOW AS REQUIRED
	$email_to = "******@blueyonder.co.uk";
	$email_subject = "Your email subject line";
	
	
	function died($error) {
		// your error code can go here
		echo "We are very sorry, but there were error(s) found with the form your submitted. ";
		echo "These errors appear below.<br /><br />";
		echo $error."<br /><br />";
		echo "Please go back and fix these errors.<br /><br />";
		die();
	}
	
	// validation expected data exists
	if(!isset($_POST['first_name']) ||
		!isset($_POST['last_name']) ||
		!isset($_POST['email']) ||
		!isset($_POST['telephone']) ||
		!isset($_POST['comments'])) {
		died('We are sorry, but there appears to be a problem with the form your submitted.');		
	}
	
	$first_name = $_POST['first_name']; // required
	$last_name = $_POST['last_name']; // required
	$email_from = $_POST['email']; // required
	$telephone = $_POST['telephone']; // not required
	$comments = $_POST['comments']; // required
	
	$error_message = "";
	$email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
  if(!eregi($email_exp,$email_from)) {
  	$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
	$string_exp = "^[a-z .'-]+$";
  if(!eregi($string_exp,$first_name)) {
  	$error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!eregi($string_exp,$last_name)) {
  	$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(strlen($comments) < 2) {
  	$error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }
  $string_exp = "^[0-9 .-]+$";
  if(!eregi($string_exp,$telephone)) {
  	$error_message .= 'The Telphone Number you entered does not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
  	died($error_message);
  }
	$email_message = "Form details below.\n\n";
	
	function clean_string($string) {
	  $bad = array("content-type","bcc:","to:","cc:","href");
	  return str_replace($bad,"",$string);
	}
	
	$email_message .= "First Name: ".clean_string($first_name)."\n";
	$email_message .= "Last Name: ".clean_string($last_name)."\n";
	$email_message .= "Email: ".clean_string($email_from)."\n";
	$email_message .= "Telephone: ".clean_string($telephone)."\n";
	$email_message .= "Comments: ".clean_string($comments)."\n";
	
	
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

<!-- include your own success html here -->

Thank you for contacting us. We will be in touch with you very soon.

<?
}
?>

The final message shows up suggesting all was well but nothing comes through to my inbox.

I've checked the PHP version running (5.2.13) on the server and noted that 'Internal Sendmail Support for Windows' is enabled.

These two items from the phpinfo page are set at 'no value'. Could that be the issue?:

sendmail_from
sendmail_path
 
Have you tried printing:
Code:
echo($email_to.$email_subject.$email_message.$headers);
To make sure that the email function is being passed the variables correctly?
 
Could even be a mail relaying issue. Can you see any activity in the mail logs, failing that also check the web logs too.
 
Sorry, print is the HTML term, echo just echos something on screen, like
Code:
echo("Hello World");
would show up as "Hello World".

Therefore by using
Code:
echo($email_to.$email_subject.$email_message.$headers);
It will show you the data it is trying to use for the email, and you can check if it is correct.
 
If you enter that "echo" line, it will print (i.e. display) the values to your browser screen so that you can check there aren't any faults in the data being passed in the variables.

The other classic is to try sending it to a different email address in order to check your mail host isn't auto-filtering your PHP mail as spam.
 
If the mail server is the local box - try sending it to a local account. That will rule out any spam or relay issues
 
I added the 'echo' and this came up.

[email protected] from WebsiteForm details below. First Name: John Last Name: Smith Email: [email protected] Telephone: 07777777777 Comments: dsg sdgsdg sdgsd gsdg sd From: [email protected] Reply-To: [email protected] X-Mailer: PHP/5.2.13 Thank you for contacting us. We will be in touch with you very soon.

Would that suggest it's OK? I've tried changing to two different emails but nothing has come through on either.
 
Try removing the @ to make it show any potential errors:

PHP:
@mail($email_to, $email_subject, $email_message, $headers);

to

PHP:
mail($email_to, $email_subject, $email_message, $headers);

Also add error_reporting(E_ALL); to the top of the file (just below <?php) to make it show all errors.
 
Tried all those suggestions. No luck.

I posted this before up above:

These two items from the phpinfo page are set at 'no value'. Could that be the issue?:

sendmail_from
sendmail_path

Could this be the issue? I don't have full control over the domain to change them, but could get them changed (if it is indeed causing the problem).

I'm getting no error messages. Tried with 'error_reporting(E_ALL);' just under the first line of code too.
 
Certainly seems server-side. Something's not right. I've tried another website's working php contact form first on that site, then transferred it to this one. First one works, this one doesn't.

Is it something specific that needs enabling server side? If so, what?

Thanks,
 
What level of access do you have to the server? Is it simply shared hosting or have you got full access to the box? If you have full access you can more easily check the SMTP logs etc.
 
Back
Top Bottom