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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mrb16a
Forum Newbie
Posts: 3
Joined: Fri Apr 10, 2009 3:38 am

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

Post 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?
corkman
Forum Newbie
Posts: 14
Joined: Thu Apr 09, 2009 3:40 am

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

Post 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.
mrb16a
Forum Newbie
Posts: 3
Joined: Fri Apr 10, 2009 3:38 am

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

Post 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.
corkman
Forum Newbie
Posts: 14
Joined: Thu Apr 09, 2009 3:40 am

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

Post 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...
mrb16a
Forum Newbie
Posts: 3
Joined: Fri Apr 10, 2009 3:38 am

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

Post 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.
Post Reply