在邮件文本 [mail()] 中使用 php 循环

Using php loops inside mailtext [mail()]


我必须为每个订单发送一封电子邮件。我会稍微格式化文本,如果有更多订单,我会像这样分开:
1 件衣服 20eur
1 鞋 10 欧元...

目前看起来像:
1、1件衣服、鞋子20eur,10eur

这样我需要一个循环来将它们分开,但出现错误 'unexpected foreach'.

$mailtext = '
<html>
<head>
  <title>Form order</title>
</head>
<body>
<h1>Form order</h1>
<p>Details:</p>
<table border="1">
<tr>
  <td>Name</td>
  <td>Adress</td>
</tr>
<tr>
  <td>'.$name.'</td>
  <td>'.$strase.'</td>
</tr>
</table>

<p>Order:</p>
'
 foreach ($produktarr as $row){
  echo'<tr>';
    echo'<td>'.$row['amount'].'</td>';
    echo'<td>'.$row['product'].'</td>';
    echo'<td>'.$row['price'].'</td>';
  echo'</tr>';
}
'
</body>
</html>

';

为什么在变量声明中使用 foreach 循环? 我会做这样的事情:

$mailtext = '
<html>
<head>
  <title>Form order</title>
</head>
<body>
<h1>Form order</h1>
<p>Details:</p>
<table border="1">
<tr>
  <td>Name</td>
  <td>Adress</td>
</tr>
<tr>
  <td>'.$name.'</td>
  <td>'.$strase.'</td>
</tr>
</table>

<p>Order:</p>
';
 foreach ($produktarr as $row){
 $mailtext.= '<tr><td>'.$row['amount'].'</td><td>'.$row['product'].'</td><td>'.$row['price'].'</td></tr>';

}
$mailtext.= '</body>
</html>';