0

I am trying to send an email upon submiting completed form from my site. however when ever i hit submit it only displays my php file as text and does not send the email.

below is all of my scripts could you please help me figure out why it wont send the email and display the thank you page?

My HTML File

<form method="post" name="myemailform" action="form-to-email.php">
<p>
    <label for='name'>Enter Name: </label>
    <br>
    <input type="text" name="name">
</p>
<p>
    <label for='email'>Enter Email Address:</label>
    <br>
    <input type="text" name="email">
</p>
<p>
<label for='message'>Enter Message:</label>
<br>
<textarea name="message"></textarea>
</p>
<input type="submit" name='submit' value="submit">
</form>
<br>
<br>
<br>
<br>
<script language="JavaScript">
var frmvalidator = new Validator("myemailform");
frmvalidator.addValidation("name", "req", "Please provide your name");
frmvalidator.addValidation("email", "req", "Please provide your email");
frmvalidator.addValidation("email", "email", "Please enter a valid email address");
</script>

My php File

<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];

//Validate first
if(empty($name)||empty($visitor_email)) 
{
echo "Name and email are mandatory!";
exit;
}

if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}

$email_from = 'nmckinney@chaoscomputerpro.com';
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".

$to = "nicholas.mckinney@outlook.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.html');


// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
          '(\r+)',
          '(\t+)',
          '(%0A+)',
          '(%0D+)',
          '(%08+)',
          '(%09+)'
          );
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}

?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • *"displays my php file as text"* - Can you elaborate on that? If you're seeing PHP code in your browser then PHP isn't working at all on your server. (And would involve linking to a different duplicate question here on Stack Overflow.) – David May 07 '18 at 15:53
  • Some sensible code indentation would be a good idea. It helps us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](http://www.php-fig.org/psr/psr-2/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly May 07 '18 at 15:53
  • 1
    @David I added another duplicate in regards to "showing code". – Funk Forty Niner May 07 '18 at 16:00

0 Answers0