0

I'm trying to send an HTML email receipt from a shopping cart purchase but I don't get how to echo the session array within the EOF area. php doesn't seem to want to execute here. Thoughts?

//begin of HTML message
$message = <<<EOF
<html>
<body style="font-family: 'Myriad Pro', 'DejaVu Sans Condensed', Helvetica, Arial, sans-serif;     
font-size:10px;"><center>


<table width="750" cellpadding="0" cellspacing="0" class="view-cart" style="text-    
align:center;padding:5px;"><tr>
<tr><td>Image</td><td>SKU</td><td>Description</td><td>QTY</td><td>Price</td><td>&nbsp;</td></tr>
    <tr>

     foreach ($_SESSION["products"] as $cart_itm)
        {
            echo '<td><img src="'.$cart_itm['image'].'"></td>';
            echo '<td>';
            echo '<div class="p-code">'.$cart_itm["code"].'</td>';
            echo '<td align="left">'.$cart_itm["description"].'</td>';
            echo '<td>'.$cart_itm["qty"].'</td>';
            echo '<td><div class="p-price">'.$cart_itm["price"].' each</div></td>';
            echo '<td></td>';
            echo '</tr>';
        }

    </table>


</center>
</body>
</html>

EOF;
//end of message
$headers  = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";

mail($to, $subject, $message, $headers);

The updated code now looks like this. I replaced the "$message +=" with "$message .=" and it seems to work fine now. :

 //begin of HTML message
 $message = <<<EOF
 <html>
  <body style="font-family: 'Myriad Pro', 'DejaVu Sans Condensed', Helvetica, Arial, sans-serif; font-size:10px;"><center>


 <table width="750" cellpadding="0" cellspacing="0" class="view-cart" style="text-align:center;padding:5px;">
 <tr><td>Image</td><td>SKU</td><td>Description</td><td>QTY</td><td>Price</td><td>&nbsp;</td></tr>
    <tr>

 EOF;

foreach ($_SESSION["products"] as $cart_itm) 
 {
            $message .= "<td><img src=".$cart_itm['image'].">";
            $message .= "</td><td>".$cart_itm['code'];
            $message .= "</td><td>".$cart_itm['description'];
            $message .= "</td><td>".$cart_itm['qty'];
            $message .= "</td><td>".$cart_itm['price'] ;
            $message .= "each</td><td></td></tr>";
        }

$message2 = <<<EOF

    </table>

<hr><table width="750" cellpadding="0" cellspacing="0" class="view-cart"><tr>
<td align="right"><b>Shipping: $ $shipping </b><br><b>Total: $ $total </b></td></tr></table>


</center>
</body>
</html>
EOF;

1 Answers1

4

What you're doing is creating a string using the heredoc syntax; so everything that's after the first newline and before the identifier (EOF in your case) is considered part of that string, and no code will be executed inside of it besides variables and escape sequences for special characters.

As far as I know there is no solution besides ending the string just before the loop, then replacing all instances of echo with $message += in the loop and finally creating a new string (using heredoc or conventional quotes and escaped newlines) after the loop and appending that new string to the $message.

Another (bad in my opinion) approach would be to put a placeholder for the loop's contents in the heredoc, then creating a new string that will hold the output of the loop and finally replacing the placeholder with that string.

Edit: for your updated code, note that the identifier EOF1 should be at the beginning of a new line and be terminated with a semicolon.

Also, the identifiers aren't supposed to be unique, it's fine to use the same identifier for different strings, no need for EOF1, 2, etc.

  • 2
    Agreed, don't use heredoc for this. If you're interested in saving brain cells, at least just do string manipulation/concatenation with the `$message` variable. If you want to be a programming Bad Ass, then use templates like Twig or Smarty – Zombiesplat Nov 24 '14 at 21:18