Jump to content

[SOLVED] Printing Array in email message


phpdolan

Recommended Posts

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

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

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

<?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>";
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.