Ajax + Jquery + PHP Mail实现不发送邮件

时间:2015-01-20 03:44:44

标签: php ajax email

我有一个表单,在提交时指向aspx页面,该页面使用表单的数据。在指向上述页面之前,jquery与ajax一起用于检索表单值,它将所有内容发送到我的php电子邮件文件,每当提交表单时都应该向我发送信息性电子邮件。不幸的是,我什么也没收到,其他一切都有效,但我没有收到任何电子邮件。请帮忙,我不知道为什么这不起作用。

附加信息:我正在使用wordpress并通过functions.php调用了jquery文件。 php文件是通过jquery文件调用的。

提前致谢!

HTML表单

<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邮件

<?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);
}
?>

2 个答案:

答案 0 :(得分:0)

试试这个:

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

PHP邮件

 if(mail($sendto, "Apply Submission by " .$email, $message, $headers)){
    echo 'true';  
  }

答案 1 :(得分:0)

尝试从mail.php发出任何反馈,然后success将被解雇:

...
//send email
    mail($sendto, "Apply Submission by " .$email, $message, $headers);
    header("Content-Type:text/html");
    echo("ok");
}
?>