sforcash sforcash - 3 months ago 10
PHP Question

Newb PHP mail form not working

I'm sure this is a very basic answer and that I'm overlooking something obvious.

I'm trying to get a mail form to work correctly, but it keeps giving sending me to the the php page with the error message stating that the name and comments I entered do not appear to be valid.

My HTML:

<div id="footer" class="container">
<header>
<h2>Questions or comments? <strong>Get in touch:</strong></h2>
</header>
<div class="row">
<div class="6u">
<section>
<div id="contact-form">
<form name="feedback" method="post" action="php/send_form_email.php">
<div class="row half">
<div class="6u">
<input name="name" placeholder="Name" type="text" class="text" />
</div>
<div class="6u">
<input name="email" placeholder="Email" type="text" class="text" />
</div>
</div>
<div class="row half">
<div class="12u">
<textarea name="message" placeholder="Message"></textarea>
</div>
</div>
<div class="row half">
<div class="12u">
<input type="submit" value="submit">
<a href="php/send_form_email.php"></a>
</div>
</div>
</form>
</div>
</section>




My PHP:

<?php

if(isset($_POST['email'])) {

$email_to = "sforcash@gmail.com";

$email_subject = "Message from rmcabinetry.com";





function died($error) {

// your error code can go here

echo "We are very sorry, but there were error(s) found with the form you 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['name']) ||

!isset($_POST['email']) ||

!isset($_POST['message'])) {

died('We are sorry, but there appears to be a problem with the form you submitted.');

}



$first_name = $_POST['name']; // required

$email_from = $_POST['email']; // required

$comments = $_POST['message']; // required



$error_message = "";

$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

if(!preg_match($email_exp,$email_from)) {

$error_message .= 'The Email Address you entered does not appear to be valid.<br />';

}

$string_exp = "/^[A-Za-z .'-]+$/";

if(!preg_match($string_exp,$name)) {

$error_message .= 'The Name you entered does not appear to be valid.<br />';

}

if(strlen($message) < 2) {

$error_message .= 'The Comments you entered do 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 .= "Name: ".clean_string($name)."\n";

$email_message .= "Email: ".clean_string($email_from)."\n";

$email_message .= "Message: ".clean_string($message)."\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 Riley Mills. We will be in touch with you very soon.



<?php

}

?>

Answer

This is the error:

if(strlen($message) < 2) {

You defined the variable as $comments earlier but reference $message here. There is no $message defined any place in your code.

Solution:

Replace $message with $comments in that section, as well as where it appears further below:

$email_message .= "Message: ".clean_string($message)."\n";

OR

Replace $comments with $message above where it's defined:

$comments = $_POST['message']; // required

Comments