Dancer
Dancer

Reputation: 17651

PHP send email issues from ajax/jquery

I'm not the best at PHP and i'm struggling with what I imagine should be a pretty basic script to send a contact form in an email.

My jquery trigger script is as follows -

data_html = "name="+ names + "&phone_number=" + phone + "&email_address="+ email_address + "&arrival_date=" + arrival + "&nights=" + nights + "&adults=" + adults+ "&children=" + children + "&rooms=" + rooms + "&notes=" + notes;


if (error == false){
$('.error').fadeOut(500);

 $.ajax({
  type: 'POST',
  url: 'sendMail.php',
  data: data_html,
  success: function(msg){

   if (msg == 'sent'){
       alert("sent")
    $('#contact-form').fadeOut();
    $('#success').fadeIn();
    $('#success').html('Your request has been sent, we will respond to you shortly - any issues please call us on 01539 724468')  ;
    $('#name').val(''); $('#phone').val(''); $('#emailAd').val(''); $('#arrival').val(''); $('#notes').val(''); 
   }else{
       alert("not sent")
       $('#contact-form').fadeOut();
       $('#success').fadeIn();
    $('#success').html('Mail Error. Please Try Again.!')  ; 
    setTimeout("$('#success').fadeOut(); $('#contact-form').fadeOut();)",5000)
    ;
   }
  }

});

My php script consists of the following -

<?php

 $name = $_POST['name'];
 $phone = $_POST['phone_number'];
 $email = $_POST['email_address'];
 $arrival = $_POST['arrival_date'];
 $nights = $_POST['nights'];
 $adults = $_POST['adults'];
 $children = $_POST['children'];
 $rooms = $_POST['rooms'];
 $notes = $_POST['notes'];
 $to ='[email protected]';

$message = "";
$message .= "Booking Request from Sundial Website<br>\n<br>\n";
$message .= "*Name: " . htmlspecialchars($name, ENT_QUOTES) . "<br>\n";
$message .= "*Email: " . htmlspecialchars($email, ENT_QUOTES) . "<br>\n";
$message .= "*Phone Number: " . htmlspecialchars($phone, ENT_QUOTES) . "<br>\n";
$message .= "Arrival Date: " . htmlspecialchars($arrival, ENT_QUOTES) . "<br>\n";
$message .= "No of Nights: " . htmlspecialchars($nights, ENT_QUOTES) . "<br>\n";
$message .= "Adults: " . htmlspecialchars($adults, ENT_QUOTES) . "<br>\n";
$message .= "Children: " . htmlspecialchars($children, ENT_QUOTES) . "<br>\n";
$message .= "Rooms: " . htmlspecialchars($rooms, ENT_QUOTES) . "<br>\n";
$message .= "Notes: " . htmlspecialchars($notes, ENT_QUOTES) . "<br>\n";

$headers = "MIME-Version: 1.0\r\nContent-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: \"" . $name . "\" <" . $email . ">\r\n";
$headers .= "Reply-To: " .  $email . "\r\n";
$message = utf8_decode($message);  mail($to, "Booking Request from Sundial Website",       $message, $headers);

 if ($message){
 echo 'sent';
 }else{
  echo 'failed';
 }
 ?>

Can anyone help or offer an alternate method? The files are here if you want to see them live (http://www.offthecuffdesign.co.uk/chilli/) and (http://www.offthecuffdesign.co.uk/chilli/sendMail.php)

Upvotes: 0

Views: 854

Answers (1)

dbrumann
dbrumann

Reputation: 17166

The last part in your php-script does not make sense...

$message = utf8_decode($message);  mail($to, "Booking Request from Sundial Website",       $message, $headers);
if ($message) {
    echo 'sent';
} else {
    echo 'failed';
}

This should be something like this:

$message = utf8_decode($message);
$result = mail($to, "Booking Request from Sundial Website",       $message, $headers);
if ($result) {
    echo 'sent';
} else {
    echo 'failed';
}

If that is not your problem, please clarify the expected result and what actually happens.

Upvotes: 1

Related Questions