Print array in sendmail

2019-06-13 06:05发布

问题:

I am trying to echo an array in sendmail's message body. I created a function to print a POST array:

function printArray($array, $pad=''){
     foreach (array_slice($array, 3) as $key => $value){
        echo $pad . "$key: $value <br>";
        if(is_array($value)){
            printArray($value, $pad.' ');
        }  
    } 
}

It prints perfectly both through print_r

printArray($_POST);

and if put in a variable

$Parray = printArray($_POST);
echo $Parray;

But I am not getting it work in sendmail message:

$message = printArray($_POST);
mail($to, $subject, print_r($message), $headers);

The above code sends email with '1' in message. Cant figure out what am I doing wrong here? since array echoes perfectly, its just the sendmail which does not print it.

回答1:

You are using echo in the printArray function, which sends the data to standard output (so you can see the output on your screen). But if you want to use the result of the function in a variable (like $message) you need to return the value from your function.

Since your function is a recursive one (it calls itself), you'll have to collect the output of function calls in a local variable and then return the accumulated value.

So I'd modify the printArray() function to return the constructed message as a string, beside printing it. Something like this:

function printArray($array, $pad=''){
    $buffer = array();
     foreach (array_slice($array, 3) as $key => $value){
        $buffer[] = $pad . "$key: $value <br>";
        if(is_array($value)){
            $buffer[] = printArray($value, $pad.' ');
        }
    }
    $output = join('', $buffer);
    echo $output;
    return $output;
}

Also as mentioned on other answers, print_r() will print the parameter (same as the sample printArray() function in the question) but return value is not the printed value unless you pass the second parameter as true, which causes print_r to return the string value.



回答2:

print_r function return 1 when you have not added second param TRUE, if you need buffer output then use 2nd param true in print_r function

$message = print_r($_POST, true);
mail($to, $subject, $message, $headers);


回答3:

It seems $value also may be an array(), so you can not echo $value directly. Since you checked the value by is_array() in if() condition, you can place echo $pad . "$key: $value <br>"; in else part. Can try this:

function printArray($array, $pad=''){
    foreach (array_slice($array, 3) as $key => $value){        
        if(is_array($value)){
            printArray($value, $pad.' ');
        }else{
            echo $pad . "$key: $value <br>";
        }
    }
}

You should not use print_r($message) inside mail(), Just use the $message var instead. Like mail($to, $subject, $message, $headers);

Also you didn't return anything from printArray(), so

$Parray = printArray($_POST);
echo $Parray;

here echo $Parray; don't show anything!

In below section

$message = printArray($_POST);
mail($to, $subject, $message, $headers);

$message is empt() because printArray() don't any thing.