Page 1 of 1

using a variable to mail a long set of html code

Posted: Mon Oct 09, 2006 8:36 pm
by amendelson
I want to send in an email a bunch of html code that includes some stuff queried from a database in my mail function but I'm having a problem having the queries within my message variable for the mail function.

Im including the important parts of the code, but much of it is left out: (the important part is having the php echos inside the html code...so is there a way to get this done?)

Code: Select all

// message
$message = 
<html>
<head>
  <title>test</title>
</head>
<body>
  <p>Here is what was submitted</p>
  <table>
   <tr>
     <th>Test</th><th> <?php echo $weekly['announcements']  ?></th>
   </tr>
  </table>
</body>
</html>

// Mail it
mail($to, $subject, $message, $headers);

?>
Thanks!

Posted: Mon Oct 09, 2006 8:46 pm
by klarinetking
Hi,

That's an acceptable way to send HTML e-mail. The problem you have is that you need to enclose the HTML markup in quotes. This is how your code should look:

Code: Select all

$message = "
<html>
<head>
  <title>test</title>
</head>
<body>
  <p>Here is what was submitted</p>
  <table>
   <tr>
     <th>Test</th><th>$weekly['announcements']</th>
   </tr>
  </table>
</body>
</html>";

// Mail it
mail($to, $subject, $message, $headers);
?>
Hope this helps

klarinetking

Posted: Mon Oct 09, 2006 8:50 pm
by amendelson
Thanks for your quick response. I didn't realize all I needed to do was take out the "php echo" and it would work! (oh, and add the quotes at the beginning).

But, what if in my code I have double quotes I can't change them all to single because that will invalidate some of the properties, I think.

Thanks again!

Posted: Mon Oct 09, 2006 9:05 pm
by amendelson
I found a solution to my own problem by using some code I found online, so I thought I'd post it.

I am keeping all the code in a seperate page for cleanliness but since a regular include wouldn't work I am using this function I found:

Code: Select all

<?php
ob_start();        //start output buffering
include ("table.php");    //all output goes to buffer
$buf = ob_get_contents();    //assign buffer to a variable
ob_end_clean();        //clear buffer and turn off output buffering

print $buf;
?>
Source: http://www.zend.com/tips/tips.php?id=131&single=1

Posted: Tue Oct 10, 2006 3:47 am
by Chris Corbyn
Heredoc syntax. Look it up ;)