0

我不确定我哪里出错了,但是当我使用这个邮件功能时:

    <?php
function printMember($member) {
    foreach($member as $key=>$value) {
        echo "$key : $value <br />";
    }
}

$to = 'email@address.co.za';

$subject = 'Application Form';

$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "BCC: email@address.co.za\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";


$json = $_POST['parameters'];
$json_string = stripslashes($json);
$data = json_decode($json_string, true);

$depCount = count($data["dependants"]);


$msg .= "<h2>Main member data:</h2>";
$msg .= printMember($data["mainmember"]);

$msg .= "<h2>There are $depCount Dependants</h2>";

foreach ($data["dependants"] as $index => $dependant) {
   $msg .= "<h2>Dependant $index</h2>";
   $msg .= printMember($dependant);
}

 mail($to, $subject, $msg, $headers);

它返回:

Main member data:
There are 3 Dependants
Dependant 0
Dependant 1
Dependant 2

而不是(如控制台中所示):

Main member data:
name : name
surname : surename
id : 6110190027087
age : 51
gender : Female
townofbirth : george
email : naem@gmail.com
contact : 0512148615
passport : 1111111111111
postal : test
postal_code : 4545
residential : test
residential_code : 4545

There are 1 Dependants
Dependant 1
name : dep1
surname : dep1
id : 8202255133088
age : 30
gender : Male
townofbirth : dep1
cell : 0145264448
email : dep1
passport : 2222222222222
relationship : parent

这是 JSON:

{
    "mainmember": {
        "name": "name",
        "surname": "surename",
        "id": "6110190027087",
        "age": "51",
        "gender": "Female",
        "townofbirth": "george",
        "email": "naem@gmail.com",
        "contact": "0512148615",
        "passport": "1111111111111",
        "postal": "test",
        "postal_code": "4545",
        "residential": "test",
        "residential_code": "4545"
    },
    "dependants": [
        {
            "name": "dep1",
            "surname": "dep1",
            "id": "8202255133088",
            "age": "30",
            "gender": "Male",
            "townofbirth": "dep1",
            "cell": "0145264448",
            "email": "dep1",
            "passport": "2222222222222",
            "relationship": "parent"
        }
    ]
}

我确实得到了答案:Parsing Duplicatable Form's JSON data to PHP for Mail

但只是不是实际的邮寄..

非常感谢任何帮助

4

1 回答 1

1

问题出在printMember()函数上,你正在做echo并且没有返回任何东西:像这样更新你的 printMember 函数:

function printMember($member) {
    foreach($member as $key=>$value) {
        //Fill the aux string first
        $str.= "$key : $value <br />";
    }
    //string that will be added to $msg variable inside the loop
    return $str;
}
于 2013-01-22T07:35:58.817 回答