使用json_decode检索ajax发送的javascript数组的电子邮件


Using json_decode to retrieve ajax-sent javascript array for email

我试图通过使用JSON编码发送JS数组的内容。字符串化,然后在PHP中解码,然后通过电子邮件发送。我得到一个成功警报,数据被发送到PHP,但电子邮件没有通过。有谁能指出我遗漏/弄错的地方吗?

数组已通过填充。push函数,我可以很好地在HTML中输出,所以我知道它已经填充了。

使用ajax编码我的数据字符串:

dataString = myArray; 
var jsonString = JSON.stringify(dataString);
 $.ajax({
    type: "POST",
    url: "script.php",
    data: {data : jsonString}, 
    cache: false,
    success: function(){
       alert("Success");
    }
});

然后在PHP中:

<?php
$data = json_decode(stripslashes($_POST['data']));
$to = "my@email.com";
$header = "Content-Type: text/html'r'nReply-To";
$subject = "This is my Subject Line";
$body = 
    @"
    <strong>The data is:</strong> $data
    ";

    if(mail($to, $subject, $body, $header)) {
        die("true");    
        } else {
            die("There was an error sending the email.");   
        }
?>

电子邮件根本没有通过,我根本没有收到任何错误信息。有人能帮忙吗?谢谢!

首先,你不能直接将$data数组转换为字符串。

必须使用print_r($data, true)将数组的内容插入字符串

PHP机器中有邮件服务器吗?

mail()返回true如果邮件被接受交付,但这并不意味着它已经发送。

您可能没有邮件服务器。

你可以快速做的是,进入这个链接,

http://www.toolheap.com/test-mail-server-tool/

下载工具。按照步骤运行它。

然后把你的代码改成这个,看看你能做什么,

<?php
$data = json_decode(stripslashes($_POST['data']));
$to = "abc@gmail.com";
$header = "Content-Type: text/html'r'nReply-To";
$subject = "This is my Subject Line";
$body = 
@"
<strong>The data is:</strong> 
".print_r($data, true) ;

if(mail($to, $subject, $body, $header)) {
    die("true");    
    } else {
        die("There was an error sending the email.");   
    }
?>

我认为你会得到它的价值,但如何使用它是你的选择。

欢呼。