Forum Moderators: coopster

Message Too Old, No Replies

PHP email and While Loop / variable issues

trying to get while loop to work in php html email

         

Cactus_Jack

2:38 pm on Dec 29, 2009 (gmt 0)

10+ Year Member



Hello;

I'm new to sending HTML emails and need a little help. I'm trying to send an email to some recipients that shows the standings of our league from a MySql DB.

When it runs the table with the while loop it errors out. Looking at the email output it seems that possibly the variables are not being passed through correctly.

(Connects to database are okay, code not shown for brevity reasons)
=====================================================
<?php
$to = "me@domain.com";
$subject ="Standings";
$headers ="From: me@domain.com \r\n";
$headers .="MIME-Version: 1.0\r\n"
. "Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
. "Content-Transfer-Encoding: 7bit\r\n";

$message = <<<EOF

$query = "SELECT team, wins, points FROM stand_sat_gold ORDER BY rank";
$result = mysql_query($query);

<table align=center cellpadding='0' cellspacing='0' border='1'>
<tr bgcolor=#999999 align=center>
<th>Team</th>
<th>W</th>
<th>L</th>
<th>T</th>
<th>Pts</th>
</tr>

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "<tr align=center>
<td width='150'>{$row['team']}</td>
<td width='34'>{$row['wins']}</td>
<td width='34'>{$row['loses']}</td>
<td width='34'>{$row['ties']}</td>
<td width='34'>{$row['points']}</td>
</tr>";
}
echo "</table>";

</body>
</html>
EOF;

if(mail($to, $subject, $message, $headers))
{
echo 'MAIL SENT';
} else {
echo 'MAIL FAILED';
}
?>
=======================================================
The output comes out in a decent looking table for the header row. The (while) loop simply shows the command right after the "Pts" column, it doesn't make a new row. Also the $row variable is missing making me wonder if that is even being set right.

Team W L T Ptswhile( = mysql_fetch_array(Resource id #2,MYSQL_ASSOC)) {
}

rocknbil

8:55 pm on Dec 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard Cactus_Jack, try this.


$message ='<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<title>' . $subject . '</title>
</head>
<body>
<table align="center" cellpadding="0" cellspacing="0" border="1">
<tr>
<th style="text-align:center; background:#999999;">Team</th>
<th style="text-align:center; background:#999999;">W</th>
<th style="text-align:center; background:#999999;">L</th>
<th style="text-align:center; background:#999999;">T</th>
<th style="text-align:center; background:#999999;">Pts</th>
</tr>
'; // END $message for a second while we query . .


$query = "SELECT team, wins, points FROM stand_sat_gold ORDER BY rank";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$message .= '
<tr>
<td width="150" style="text-align:center;">' . $row['team'] . '</td>
<td width="34" style="text-align:center;">' . $row['wins'] . '</td>
<td width="34" style="text-align:center;">' . $row['loses'] . '</td>
<td width="34" style="text-align:center;">' . $row['ties'] . '</td>
<td width="34" style="text-align:center;">' . $row['points'] . '</td>
</tr>
';
}

$message .= '
</table>
<p>&nbsp;</p>
</body>
</html>
';

The problem is you are concatenating commands, etc. to the message with the <<< PRINT TO HERE statements. Just concatenate $message. At one point you are echoing to STDOUT, you want to concatenate the message.

If you want an HTML email, supply a full HTML document. Issuing an HTML mail header without an html document increases the SpamAssassin "score." It also helps the recipient client render the document, just like browsers.

The quoting and inline CSS changes are just a bonus when it comes time to validate [validator.w3.org] your output, which too few programmers do . . . inline CSS is supported by nearly all mail clients, sooner or later they will become compliant as well.

Cactus_Jack

11:56 pm on Dec 29, 2009 (gmt 0)

10+ Year Member



That worked, thank you. It was driving me mad trying to get the syntax correct. I'm glad I found this forum.