Reputation: 1
I can’t figure out where the problem comes from! The mail() is working perfectly (I already tested it with a simple php file) but my form doesn’t send any email!
HTML
<section id="contact">
<div id="google-map" class="wow fadeIn" data-latitude="35.7777622" data-longitude="10.8285004" data-wow-duration="1000ms" data-wow-delay="400ms"></div>
<div id="contact-us" class="parallax">
<div class="container">
<div class="row">
<div class="heading text-center col-sm-8 col-sm-offset-2 wow fadeInUp" data-wow-duration="1000ms" data-wow-delay="300ms">
<h2>Contact</h2>
<p>Send a message</p>
</div>
</div>
<div class="contact-form wow fadeIn" data-wow-duration="1000ms" data-wow-delay="600ms">
<div class="row">
<div class="col-sm-6">
<form id="main-contact-form" name="contact-form" method="post" action="sendemail.php">
<div class="row wow fadeInUp" data-wow-duration="1000ms" data-wow-delay="300ms">
<div class="col-sm-6">
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Name" required="required">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input type="email" name="email" class="form-control" placeholder="Email" required="required">
</div>
</div>
</div>
<div class="form-group">
<input type="text" name="subject" class="form-control" placeholder="Sub" required="required">
</div>
<div class="form-group">
<textarea name="message" id="message" class="form-control" rows="4" placeholder="Type your message" required="required"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn-submit">Send</button>
</div>
</form>
</div>
<div class="col-sm-6">
</div>
</div>
</div>
</div>
</div>
</section><!--/#contact-->
main.js
// Contact form
var form = $('#main-contact-form');
form.submit(function(event){
event.preventDefault();
var form_status = $('<div class="form_status"></div>');
$.ajax({
url: $(this).attr('action'),
beforeSend: function(){
form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Progress...</p>').fadeIn() );
}
}).done(function(data){
form_status.html('<p class="text-success">Thanks for your message</p>').delay(3000).fadeOut();
});
});
sendemail.php
<?php
$name = @trim(stripslashes($_POST['name']));
$from = @trim(stripslashes($_POST['email']));
$subject = @trim(stripslashes($_POST['subject']));
$message = @trim(stripslashes($_POST['message']));
$to = '[email protected]';
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: {$name} <{$from}>";
$headers[] = "Reply-To: <{$from}>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();
mail($to, $subject, $message, $headers);
die;
Could you please help me figuring out the problem? When I add a simple mail('[email protected]', 'My Subject', 'message') to the sendemail.php file, every time the form is submitted, I receive an email! But I never receive the mail($to, $subject, $message, $headers);
Thank you very much
Upvotes: 0
Views: 159
Reputation:
You are spoofing the From header based on the user's input. Don't do this.
The From header of an email message must accurately represent the sender of the email message -- if you insert another value into this header, many mail servers will reject your mail or flag it as spam, as the resulting message will fail SPF and/or DMARC checks.
Use a From header which accurately represents the source of the message. For instance, if your web site is example.com
, set the From header as From: [email protected]
. Include the email address and name set by the user either in the body of the message, or in a Reply-To
header.
Upvotes: 1
Reputation: 1
I think Headers needs to be a string.
$headers = '';
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: {$name} <{$from}>\r\n";
$headers .= "Reply-To: <{$from}>\r\n";
$headers .= "Subject: {$subject}\r\n";
$headers .= "X-Mailer: PHP/".phpversion() . "\r\n";
http://php.net/manual/en/function.mail.php
You should also filter $_POST
http://php.net/manual/en/function.filter-input.php
I think you also need to send the data through the ajax request.
$.ajax({
url: $(this).attr('action'),
type: "POST",
data: $(this).serialize(),
Upvotes: 0