Page 1 of 1

Outputting the contents of a php array into a html email usp

Posted: Fri Apr 10, 2009 3:40 am
by mrb16a
As the topic suggests, i am trying output the contents of a php array into a html email that i would use php code to send.

Can do everything besides get the php array data into the html email.

Any ideas?

Re: Outputting the contents of a php array into a html email usp

Posted: Fri Apr 10, 2009 3:55 am
by corkman
What about this?

Code: Select all

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$headers = 'From: webmaster@example.com'
 
$message = print_r($your-array-to-send,true);
 
mail($to, $subject, $message, $headers);
?>
If you will want to look it like html (why?), you will have to add content type text/html to headers

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

and probably replace line with $message with something like

$message = "<html><body><pre>".print_r($your-array-to-send,true)."</pre></body></html>";

to maintain formatting.

Re: Outputting the contents of a php array into a html email usp

Posted: Fri Apr 10, 2009 4:24 am
by mrb16a
Hi thanks for the reply. Unfortunately your suggestion does not do what i was after. I actually need to go through my array and list out each item. Also the output that i need to do is just not from one array, it is also a combination of some mysql queries which i already have as they output to the screen fine.

The main reason why i want to use html would be better formatting.

The main problem is really where to put php loops(which generate the data) into the html form. It seems like it wont allow for this to happen.

Re: Outputting the contents of a php array into a html email usp

Posted: Fri Apr 10, 2009 5:05 am
by corkman
I think we will need some exapmles ;) If you want to go through an array, you can use various functions, for, or reset with while and each, or foreach...

To output a table for example:

Code: Select all

$message="<table>\r\n";
for ($row = 0; $row < $MAX_TABLE_ROWS; $row++) {
  $message.="<tr>";
  for ($column = 0; $column < $MAX_TABLE_COLUMNS; $column++) {
    $message="<td>".$your-array[$row][$column]."</td>";
  }
  $message=."</tr>\r\n";
}
$message=."</table>\r\n";
Note, that I have not tested the code above...

Re: Outputting the contents of a php array into a html email usp

Posted: Fri Apr 10, 2009 6:40 am
by mrb16a
Hi, what you wrote was very close to what i needed to write.

I was able to work my way through it with what you suggested and all is working now.

Thanks. Much appreciated.