I implemented the following contact form on my website (from a template), and I can send and receive an email message successfully, but the message part (the users text) is NOT received in my Inbox. I receive the Name and From Email, but not the content of the message.
screenshot from the website form
Can someone help finding and fixing the issue? It is the php, the jQuery or the html wrong?
HTML:
<form action="contact.php" class="contact_form" id="form" method="post">
<div class="col-lg-6 col-md-6 col-lg-offset-3 col-md-offset-3">
<span class="input input_name">
<input class="input__field" type="text" name="name" id="name" required/>
<label class="input__label" for="name">
<span class="input__label_content">Name</span>
</label>
</span>
<span class="input input_website">
<input class="input__field" type="url" name="website" id="website" />
<label class="input__label" for="website">
<span class="input__label_content">Website</span>
</label>
</span>
<span class="input input_mail">
<input class="input__field" type="email" name="email" id="email" required/>
<label class="input__label" for="email">
<span class="input__label_content">email</span>
</label>
</span>
</div>
<div class="col-lg-8 col-md-8 col-lg-offset-2 col-md-offset-2">
<span class="textarea">
<textarea class="textarea_field" name="textarea" id="textarea" rows="4" placeholder="type your text here . . . ."></textarea>
</span>
<span class="submit">
<button type="submit" class="rms-btn submit_btn" id="submit">submit request <span><i class="fa fa-pencil"></i></span></button>
</span>
</div>
</form>
contact.php:
<?php
/*
* Ajax form submit
*/
# request sent using HTTP_X_REQUESTED_WITH
if( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) ){
if (isset($_POST['name']) AND isset($_POST['email']) AND isset($_POST['textarea'])) {
$to = 'info@antarescodestudio.com'; /* Add your contact email address here */
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$subject = filter_var($_POST['subject'], FILTER_SANITIZE_STRING);
$message = filter_var($_POST['textarea'], FILTER_SANITIZE_STRING);
$sent = email($to, $email, $name, $message);
if ($sent) {
echo 'Message sent!';
} else {
echo 'Message couldn\'t sent!';
}
}
else {
echo 'All Fields are required';
}
return;
}
/**
* email function
*
* @return bool | void
**/
function email($to, $from_mail, $from_name, $subject, $message){
$header = array();
//$header[] = "MIME-Version: 1.0";
$header[] = "From: {$from_name} < {$from_mail}>";
$header[] = "Message: {$message}";
/* Set message content type HTML*/
/*$header[] = "Content-type:text/html; charset=iso-8859-1";*/
/*$header[] = "Content-Transfer-Encoding: 7bit";*/
if( mail($to, $message, implode("
", $header)) ) return true;
}
?>
contact.js
jQuery(document).ready(function() {
var form = jQuery('#form'); // contact form
var submit = jQuery('#submit'); // submit button
var alert = jQuery('.alert'); // alert div for show alert message
// form submit event
form.on('submit', function(e) {
e.preventDefault(); // prevent default form submit
// sending ajax request through jQuery
jQuery.ajax({
url: 'contact.php', // form action url
type: 'POST', // form submit method get/post
dataType: 'html', // request type html/json/xml
data: form.serialize(), // serialize form data
beforeSend: function() {
alert.fadeOut();
submit.html('Sending....'); // change submit button text
},
success: function(data) {
alert.html(data).fadeIn(); // fade in response data
form.trigger('reset'); // reset form
submit.html('Email Sent <span> <i class="fa fa-check"></i> </span>'); // reset submit button text
},
error: function(e) {
console.log(e)
}
});
});
});
Thanks!