0

I have a form which upon submission directs to a aspx page, which uses the form's data. Before directing to the above stated page, jquery is used alongside ajax to retrieve form values, and it sends everything to my php email file, which should be sending me informational emails whenever a form is submitted. Unfortunately I recevie nothing, everything else works, but I don't receive any email. Please help, I'm not sure why this isn't working.

Additional Info: I'm using wordpress and have called the jquery file via the functions.php. The php file is called via the jquery file.

Thanks in Advance!

HTML Form

<form action="https://redirection_page.aspx" method="get" name="nform" id="nform">
  <div class="submission">
    <div class="fname">
      <input type="text" name="firstName" class="fname" placeholder="First name" required="">
    </div>
    <br>
    <div class="lname">

      <input type="text" name="lastName" class="lname" placeholder="Last name" required="">
    </div>
    <br>
    <div class="email">

      <input type="email" name="email" class="email" placeholder="example@email.com" required="">
    </div>
    <br>
    <div class="phone">

      <input type="text" name="homePhone" class="phone" placeholder="Phone Number" required="">
    </div>
    <br>


    <br>
    <!-- Edit the Continue Text -->
    <div class="form-button">
       <input type='button' class="submit tp-button green big :hover.green" value="Continue">
    </div>
  </div>
</form>

Jquery/Ajax

jQuery(document).ready(function($) {  

  var nform = $('#nform');
  $('#nform .form-button input.submit').click(function(){
  var data = {
    first      : $("#nform input.fname").val(),
    last       : $("#nform input.lname").val(),
    email      : $("#nform input.email").val(),
    phone      : $("#nform input.phone").val(),
  };


$.ajax({
    type: "POST",
    url: "/wp-content/themes/Avada/email.php",
    data: data,
    success: function(){
    $('#nform').submit();
    }
});

});

});

PHP Mail

<?php
if($_POST){
    $first = $_POST['first'];
    $last = $_POST['last'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $sendto = "myemailaddress@gmail.com";

$message = "";  
$message .= "<p> Name: " . $first . " " . $last "</p>";
$message .= "<p> Email: " . $email . "</p>";
$message .= "<p> Phone Number: " . $phone . "</p>";

$mail     = 'no-reply@'.str_replace('www.', '', $_SERVER['SERVER_NAME']);
$uniqid   = md5(uniqid(time()));
$headers  = 'From: '.$mail."\r\n";
$headers .= 'Reply-to: '.$mail."\r\n";
$headers .= 'Return-Path: '.$mail."\r\n";
$headers .= 'Message-ID: <'.$uniqid.'@'.$_SERVER['SERVER_NAME'].">\r\n";
$headers .= 'Date: '.gmdate('D, d M Y H:i:s', time())."\r\n";
$headers .= 'X-Priority: 3'."\r\n";
$headers .= 'X-MSMail-Priority: Normal'."\r\n";
$headers .= 'Content-Type: multipart/mixed;boundary="----------'.$uniqid.'"'."\r\n";
$headers .= '------------'.$uniqid."\r\n";
$headers .= 'Content-transfer-encoding: 7bit';
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

//send email
    mail($sendto, "Apply Submission by " .$email, $message, $headers);
}
?>
Bakajuice
  • 173
  • 1
  • 9

2 Answers2

0

Try this:

 $.ajax({
     type: "POST",
     async: false,
     url: "/wp-content/themes/Avada/email.php",
     data: data,
     success: function(){
         window.location = "https://redirection_page.aspx/"
     }
 });

PHP Mail

 if(mail($sendto, "Apply Submission by " .$email, $message, $headers)){
    echo 'true';  
  }
Afsar
  • 3,104
  • 2
  • 25
  • 35
  • Just tried it out, removed the form's action, then adjusted the ajax accordingly, but the redirection page seems to only appear when the form action is used and then submitted with the form's values. When I clicked submit the page showed a not allowed page. Thanks for the help so far! – Bakajuice Jan 20 '15 at 04:07
  • are you getting mail ? and which page is showing when you are submitting – Afsar Jan 20 '15 at 04:12
  • ya check , if you success call back , then only it will redirect – Afsar Jan 20 '15 at 04:15
  • I'm unfortunately not getting any emails, I'll try your second adjustment as well, hopefully it will make ajax wait for a successful sent message – Bakajuice Jan 20 '15 at 04:42
  • error_reporting(E_ALL); keep this line at top of mail script and check what kind of error you are getting – Afsar Jan 20 '15 at 04:44
  • Just tried doing as you said, but I think the problem is the redirection page is on a different site form, so the data is transferred, but ultimately the errors/echos don't show. Is there any way to make ajax success wait until the php file finishes to redirect? – Bakajuice Jan 20 '15 at 04:50
  • ya you can do that , use async: false in ajax, check the code i have updated – Afsar Jan 20 '15 at 05:01
  • thanks for the code, but still no luck, the ajax seems to redirect in an instant, regardless of async. – Bakajuice Jan 20 '15 at 05:18
  • so Hadar, redirecting is working and only thing is you are not getting mail right – Afsar Jan 20 '15 at 05:38
  • yep, I've tried using timeout, but it didn't work as it should – Bakajuice Jan 20 '15 at 05:41
  • I think the php may not be working at all, for success I made setTimeout for 5 secs and during that time a inserted into the php `echo '';` and nothing showed. Do you think the php might not be working? – Bakajuice Jan 20 '15 at 05:56
0

Try to make any feedback from mail.php, then success will fired:

...
//send email
    mail($sendto, "Apply Submission by " .$email, $message, $headers);
    header("Content-Type:text/html");
    echo("ok");
}
?>
Ruben Kazumov
  • 3,803
  • 2
  • 26
  • 39
  • Yes, I also thought checking for email success first before proceeeding to success would be the best bet, but I'm not entirely sure through what means - I just tried echoing ok but same results - page was loaded, no email. – Bakajuice Jan 20 '15 at 04:41