phpdolan Posted December 16, 2008 Share Posted December 16, 2008 Well, the title's not exactly correct. I can print 'Array' in an email message but not much else. Please advise what's missing or wrong. Here's an excerpt from the array. It mainly a curl troubleshooting feature: Array ( [354611] => Array ( [mysql_stat] => 0 [mysql_error] => [mysql_info] => [time-stamp] => 12/16/2008 1:19:28 PM ) [153220] => Array ( [mysql_stat] => 0 [mysql_error] => [mysql_info] => [time-stamp] => 12/16/2008 1:19:43 PM Here's the code I'm using: $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: Cron Manager <[email protected]>' . "\r\n"; $message = "<html><head><title>Cron Sucessful</title> <body><p>Time Stamp = $stamp</p> <br />"; foreach ( $stats as $user -> $values ) { $message .= "[" . $user . "]<br />"; foreach ( $user as $key -> $value ) { $message .= "[" . $key . "] ->" . $value . "<br />"; } } $message .= "<br /></body></html>"; mail ("[email protected]", "Update DB bot - Cron Successful!", $message, $headers ); Here's the resulting email body: Time Stamp = 12/16/2008 1:14:09 PM [29] -> [29] -> Thanks, David Link to comment https://forums.phpfreaks.com/topic/137241-solved-printing-array-in-email-message/ Share on other sites More sharing options...
phpdolan Posted December 17, 2008 Author Share Posted December 17, 2008 Hi All, In case my request was not clear enough, here's the scoop. I want to inlude a multidimensional array of info in the contents of $message. Print () and echo () send the contents to the screen. I want it in the $message variable surrounded by html. Thanks for reading down here. David Link to comment https://forums.phpfreaks.com/topic/137241-solved-printing-array-in-email-message/#findComment-717663 Share on other sites More sharing options...
phpdolan Posted December 17, 2008 Author Share Posted December 17, 2008 Just in case someone wants the solution, it's takes a lot of strings appended to $message. Revised code below: $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: Cron Manager <[email protected]>' . "\r\n"; $message = "<html><head><title>Cron Sucessful</title> <body><p>Time Stamp = $stamp</p> <br /><br />Start ( hr : m . s ) = $start<br/><br /> End ( hr : m . s ) = $end<br/><br /> Duration = $duration<br/><br />"; $keys = array_keys ( $stat ); foreach ( $stat as $user => $status_item) { $message .= "<br />[user-" . $user . "]<br />"; foreach ( $status_item as $key => $value ) { if ( is_array ( $value ) ) { $message .= " [" . $key . "] -> " . $value . "<br />"; foreach ( $value as $status => $result ) { $message .= " [" . $status . "] -> " . $result . "<br />"; $check = 'ON'; } } else { $message .= " [" . $key . "] -> " . $value . "<br />"; $check = 'ON'; } } } $message .= "<br /></body></html>"; This is the best way I've found so far to print an array in a mail message. Cheers, David Link to comment https://forums.phpfreaks.com/topic/137241-solved-printing-array-in-email-message/#findComment-718010 Share on other sites More sharing options...
premiso Posted December 17, 2008 Share Posted December 17, 2008 <?php $stat = array("test" => array("extra" => array("bob" =>1), "bob2" => 2), "test2" => "test1"); $message = ""; echo "<pre>"; echo printMultiArray($stat); echo "</pre>"; function printMultiArray($array, $message="", $i=1, $recur=false) { $tmpMessage = str_pad($tmpMessage, $i, "\t"); if (is_array($array)) { foreach ($array as $key => $value ) { if (!$recur) $message .= "<br />[user-" . $key . "]<br />"; if (is_array($value)) { $message .= $tmpMessage . "[" . $key . "] -> " . $value . "<br />"; $message = printMultiArray($value, $message, ($i+1), true); }else { $message .= $tmpMessage . "[" . $key . "] -> " . $value . "<br />"; } } } return $message; } die(); ?> That should work with any size array. Got bored so yea decided to make it recursive. (Note that does the same thing as the following would): <?php $stat = array("test" => array("extra" => array("bob" =>1), "bob2" => 2), "test2" => "test1"); $message = ""; echo "<pre>"; $message = print_r($stat, 1); echo $message . "</pre>"; ?> Link to comment https://forums.phpfreaks.com/topic/137241-solved-printing-array-in-email-message/#findComment-718166 Share on other sites More sharing options...
phpdolan Posted December 17, 2008 Author Share Posted December 17, 2008 Premiso, Really. Thanks for pointing that out. A much simpler way to spit out the array. David Link to comment https://forums.phpfreaks.com/topic/137241-solved-printing-array-in-email-message/#findComment-718283 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.