I have made a form that emails the info entered in the fields but I am getting an error when filled out.
Could not send mail! Please check your PHP mail configuration.
All the code seems good but I can't find the cause of the error. The error shows once I try to submit the form (which is the error message meant to show on a send mail error). Any help is greatly appreciated!!
I should mention that there are 2 forms in the directory; Form "1", uses the "1" versions of the JS and PHP files, Form "2' uses the "2" version. They do not share the JS and PHP files.
<form id="email-form2" name="email-form2" method="POST" data-name="Insurance Form">
Enter Name
<input class="w-input text-field" id="name" type="text" placeholder="First name and last name" name="name" data-name="Name" required> Email
<input class="w-input text-field" id="email" type="email" name="email" placeholder="Enter your email address" data-name="Email" required> Date of Birth
<input class="w-input text-field" id="date" type="date" name="date" data-name="Date" required> Gender
<input class="w-input text-field" id="gender" type="text" placeholder="Male or Female" name="gender" data-name="Gender" required> Member ID
<input class="w-input text-field" id="member" type="text" placeholder="Member number" name="member" data-name="Member" required> Policy ID
<input class="w-input text-field" id="policy" type="text" placeholder="Policy number" name="policy" data-name="Policy" required>
<div class="div-spc">
<button class="w-button button no-margin" type="submit">Submit Form</button>
</div>
</form>
<?php
if($_POST)
{
$to_Email = "email@email.com"; //Replace with recipient email address
//check if its an ajax request, exit if not
if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest')
{
//exit script outputting json data
$output = json_encode(array(
'type'=> 'error',
'text' => 'Request must come from Ajax'
));
die($output);
}
//check $_POST vars are set, exit if any missing
if (!isset($_POST["userName"]) || !isset($_POST["userEmail"]) || !isset($_POST["userDate"]) || !isset($_POST["userGender"]) || !isset($_POST["userMember"]) || !isset($_POST["userPolicy"]))
{
$output = json_encode(array('type'=>'error', 'text' => 'Input fields are empty!'));
die($output);
}
//additional php validation
if (empty($_POST["userName"]))
{
$output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!'));
die($output);
}
if (!filter_var($_POST["userEmail"], FILTER_VALIDATE_EMAIL))
{
$output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!'));
die($output);
}
if (empty($_POST["userMember"]))
{
$output = json_encode(array('type'=>'error', 'text' => 'Need Member ID number!'));
die($output);
}
if (empty($_POST["userGender"]))
{
$output = json_encode(array('type'=>'error', 'text' => 'Please enter Male or Female!'));
die($output);
}
if (empty($_POST["userPolicy"]))
{
$output = json_encode(array('type'=>'error', 'text' => 'Need Policy ID number!'));
die($output);
}
if (empty($_POST["userDate"]))
{
$output = json_encode(array('type'=>'error', 'text' => 'Please fill out birth date!'));
die($output);
}
//proceed with PHP email.
$headers = 'From: '.$_POST["userEmail"].'' . "\r\n" .
'Reply-To: '.$_POST["userEmail"].'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// send mail
$sentMail = @mail($to_Email, $_POST["userName"], $_POST["userDate"], $_POST["userGender"], $_POST["userMember"], $_POST["userPolicy"], $headers);
if (!$sentMail)
{
$output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
die($output);
} else {
$output = json_encode(array('type'=>'message', 'text' => 'Hi '.$_POST["userName"] .' Thank you for your email'));
die($output);
}
}
?>
$(document).on("ready", function() {
$("#email-form2 [type='submit']").click(function(event) {
event.preventDefault();
//get input field values
var user_name = $('input[name=name]').val()
var user_email = $('input[name=email]').val()
var user_date = $('input[name=date]').val()
var user_gender = $('input[name=gender]').val()
var user_member = $('input[name=member]').val()
var user_policy = $('input[name=policy]').val()
//data to be sent to server
post_data = {
'userName': user_name,
'userEmail': user_email,
'userGender': user_gender,
'userMember': user_member,
'userPolicy': user_policy,
'userDate': user_date
}
//Ajax post data to server
$.post('contact_me2.php', post_data, function(response) {
//load json data from server and output message
if (response.type == 'error') {
output = '<div class="error-message"><p class="from">' + response.text + '</p></div>'
} else {
output = '<div class="success-message"><p class="seuccses">' + response.text + '</p></div>'
//reset values in all input fields
$('#email-form2 input').val('')
}
$("#result").hide().html(output).slideDown()
}, 'json')
});
//reset previously set border colors and hide all message on .keyup()
$("#email-form2 input").keyup(function() {
$("#result").slideUp()
})
});