Running a Loop through $message in mail()
<p>This is a follow on to <a href="http://stackoverflow.com/questions/7573505/dynamic-content-into-mail-message">this question</a>.</p>

<p>I'm putting together a simple HTML email confirming an order to my database. As each order is obviously dynamic, i need part of $message to run through a loop.</p>

<p>For example i query my database outside of the $message and end up with </p>

<pre><code>$emailinfo=mysql_fetch_assoc($result) or die(mysql_error()); 
</code></pre>

<p>I begin my $message...</p>

<pre><code>$message = &lt;&lt;&lt;END
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Whatever&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;p&gt;{$emailinfo['itemname']}&lt;/p&gt;
  &lt;/body&gt;
&lt;/html&gt;
END;
</code></pre>

<p>The above is fine if someone has only ordered one item, but what i need to do is account for if someone orders more than one item, looping through each one and echoing out in $message. Outside of $message i can do this (which works)</p>

<pre><code>do {
echo $emailinfo['itemname'];
} 
while ($emailinfo=mysql_fetch_assoc($result));
</code></pre>

<p>But when i wrap my $message within the loop, as was suggested in the comments on the previous question, it still only echos out the first row/order. E.g.</p>

<pre><code>do {
$message = &lt;&lt;&lt;END
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Whatever&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;p&gt;{$emailinfo['itemname']}&lt;/p&gt;
  &lt;/body&gt;
&lt;/html&gt;
END;
} 
while ($emailinfo=mysql_fetch_assoc($result));
</code></pre>

<p>Can someone help? Outside of $message, the loop, query etc work fine, i just need it to work within $message. And it's only part of the order i need to loop through. I don't need to loop through customer info, delivery address etc as there'll only be one of those (if that helps at all).</p>

<p>Thanks as always </p>
