Im not quiet sure where im going wrong but, when i use this mail function:
<?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']) . "
";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "
";
$headers .= "BCC: email@address.co.za
";
$headers .= "MIME-Version: 1.0
";
$headers .= "Content-Type: text/html; charset=ISO-8859-1
";
$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);
it returns:
Main member data:
There are 3 Dependants
Dependant 0
Dependant 1
Dependant 2
instead of (as displayed in console):
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
here's the 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"
}
]
}
I did get an answer on this: Parsing Duplicatable Form's JSON data to PHP for Mail
But just not the actual mailing..
Any help greatly appreciated
The problem is on the printMember()
function, you are doing echo
and that doesn't return anything: Update your printMember function like this:
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;
}