PHP converts form data to an email, then displays a web page.

Hey guys.

I’m trying to write a PHP file that will take the data entered into a form on an html page and send it to an email address, with a nice, decent layout, and then display a new page once it is successful. Here’s what I have so far, but for some reason it just isn’t working.

<form id="contact_form" method="post" action="send_form_email.php" novalidate>

		<div>
		<label>Name:*</label>
		<input type="text" id="name" name="name" tabindex="1">
		</div>

		<div>
		<label>Email:*</label>
        <span id="sprytextfield1">
        <label>
          <input type="text" name="validateEmail" id="validateEmail" tabindex="2"/>
        </label>
        <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span>
        <br/><br/>
<p class="center"><input type="submit" id="submit" value="Submit" tabindex="4">
<a href="send_form_email.php">Email Form</a>
<br/><br/>
  <br />
</p>

	</form>
	<div id="textarea">
	<label>Comments:*</label>
    <span id="sprytextarea1">
    <textarea id="comments2" name="comments2" form="contact_form" tabindex="3"></textarea><br/>
    <span id="countsprytextarea1">&nbsp;</span><br/><span class="textareaRequiredMsg">A value is required.</span><span class="textareaMinChar* disabled word ** disabled word ** disabled word *g">Minimum number of characters not met.</span><span class="textareaMaxChar* disabled word ** disabled word ** disabled word *g">Exceeded maximum number of characters.</span></span></div><!-- #textarea -->
    </div><!--End forms-->

And here’s the PHP

[php]

<?php if(isset($_POST['email'])) { // EDIT THE 2 LINES BELOW AS REQUIRED $email_to = "[email protected]"; $email_subject = "New Contact Form Submission"; $name = $_POST['name']; // required $email_from = $_POST['validateEmail']; // required $comments = $_POST['comments2']; // required $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 .= "Name: ".clean_string($name)."\n"; $email_message .= "Email: ".clean_string($email_from)."\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); ?>

//essentially, this should redirect to a new page.

<?php } ?>

[/php]

Well, first, why are you using “novalidate” in the form?

Also, you are suppressing the errors on the mail command. You should not suppress them and check them to print of an error occurs. Also, sometimes a very small error can occur inside one of the variable passed. You should echo all of the parts of the mail arguments and verify they are accurate for debugging.

Here is a minor change to your code to check for errors… (PS: You check some of the inputs for garbage like “BCC:”, but, do not check for empty inputs such as an omitted email address, etc.)

[php]
$email_status = mail($email_to, $email_subject, $email_message, $headers);
if($email_status){
echo “Email was sent!”;
} else {
echo “Email had errors and was not sent!”;
}
[/php]
So, that will give you a little more info. If it sends the mail, it might just not be received. (This can happen if the email address is off or if the headers are refused at the other end…

Also, to debug this, just before the email() line, add an echo for each to see exactly what you are sending…
Like this:
[php]
echo "TO: " . $email_to . "

;
echo "TO: " . $email_subject . "

;
echo "TO: " . $email_message . "

;
echo "TO: " . $headers . "

;
die();
[/php]
This will show what is being sent. It will be obvious if one is bad! Good luck…

Sponsor our Newsletter | Privacy Policy | Terms of Service