using a variable to mail a long set of html code

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
amendelson
Forum Newbie
Posts: 3
Joined: Mon Oct 09, 2006 8:28 pm

using a variable to mail a long set of html code

Post 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!
klarinetking
Forum Commoner
Posts: 59
Joined: Mon Jul 24, 2006 9:43 am

Post 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
amendelson
Forum Newbie
Posts: 3
Joined: Mon Oct 09, 2006 8:28 pm

Post 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!
amendelson
Forum Newbie
Posts: 3
Joined: Mon Oct 09, 2006 8:28 pm

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Heredoc syntax. Look it up ;)
Post Reply