APDB APDB - 20 days ago 5
PHP Question

Contact Form don't work

When submitting my form i get a 404 page. Something is missing, or wrong.
It's a wordpress website. I inserted the email in the options of the wordpress and nothing happen. I saw a lot of stack overflow questions but none helped me. I don't want to use plugins.

<form class="form-horizontal" role="form" method="post" action="contact.php">

<div class="input-group">
<span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-user"></i></span>
<input type="text" class="form-control" id="name" name="name" placeholder="Name" value="">
</div>
<br/>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-envelope"></i></span>
<input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="">
</div>
<br/>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-pencil"></i></span>
<textarea class="form-control" rows="4" name="message" placeholder="Message..."></textarea>
</div>
<br/>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">2 + 3 = ?</span>
<input type="text" class="form-control" id="human" name="human" placeholder="">
</div>
<br/>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-5">
<input id="submit" name="submit" type="submit" value="Enviar" class="btn btn-button">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<! Will be used to display an alert to the user>
</div>
</div>
</form>


PHP Code:

<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Contact Form';
$to = 'example@domain.com';
$subject = 'Contact ';

$body = "From: $name\n E-Mail: $email\n Message:\n $message";

// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}

// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}

//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
//Check if simple anti-bot test is correct
if ($human !== 5) {
$errHuman = 'Your anti-spam is incorrect';
}

// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}
?>

Answer

First of all thank you for your reply's.

The problem here is the php mail(). I hope i can help someone with this thread.

To reach this conclusion these were the steps:

  • Tried everything that i saw in stack overflow with the same problem has me;
  • Install Check Email Plugin;
  • I did not received any email so i contact my hosting service and they said they will not enable the php mail() to avoid spam.

Solution - When this happen this is the best way to work around.