ajax将javascript变量传递给php邮件程序

时间:2016-01-09 06:51:23

标签: javascript php ajax

我试图通过ajax将javascript数组传递给php邮件程序,并且似乎无法正确使用它。如果我没有通过变量,则会收到电子邮件,但如果我这样做,则不会发送电子邮件。没有错误被抛出。

这是ajax电话:

function sendDetails() {
  var order = {
    name: "Foo",
    address: "123 Bar"
  };
  var request = $.ajax({
    url: "getdata.php",
    type: "GET",
    data: {
      order: order
    },
    dataType: "html"
  });

  request.done(function(msg) {
    $("#mybox").html(msg);
  });

  request.fail(function(jqXHR, textStatus) {
    alert("Request failed: " + textStatus);
  });
}

这里的getdata.php获取变量并通过php邮件发送出去:

require_once("PHPMailer/class.phpmailer.php");

if (isset($_GET['order']){
    $order=$_GET['order']; //works without this line
}
else 
    echo "not set";  //but no error message appears

file_put_contents("attachment.txt", $order);    

$address="recipient@email.com";
$mail= new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;  // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true;  // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->SetFrom("sender@email.com");
$mail->SMTPAuth=true;
$mail->SMTPSecure="ssl";
$mail->Username="sender@email.com";
$mail->Password="password";
$mail->SetFrom('sender@email.com');
$mail->AddAddress($address, "Dear Client");
$mail->Subject="Your details";
$mail->MsgHTML("somemessage");
$mail->AddAttachment("attachment.txt");
$mail->Body="Attached please find your details";

//no message appears
if(!$mail->Send())
{
  echo "Message sending failed.";
}
else echo "Message sent.";

为什么没有传递变量就可以工作,但是当它变成时会失败?

0 个答案:

没有答案