Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending array of data as email

Tags:

arrays

php

email

I have a PHP array, and I need to test the content of that array via an email. I'm aware that we can see the entire array using var_dump(), but how can I send that output in an email?

like image 885
Shin Avatar asked May 14 '12 06:05

Shin


2 Answers

You can use print_r( $array, true ) to get the output as a string. You can then pass this into your message body. The second paramter instructs the method to return the value rather than output it directly, permitting you to handle the results.

$message = "Results: " . print_r( $array, true );
like image 145
Sampson Avatar answered Sep 19 '22 10:09

Sampson


First Convert your array string using foreach() or implode function. I am using foreach to convert array to string..

Where string will be key and value pairs.

$data = '';
foreach ($array as $key=>$value){
    $data .= $key.'-------'.$value;
    $data.= "\n";
}

or use following code to convert array to string.

$data = implode("\n", $array);

Now send that using php mail function.

mail($recipient, $subject, $data, $headers);
like image 32
muni Avatar answered Sep 19 '22 10:09

muni