将SQL结果表添加到邮件消息中

时间:2012-01-31 14:51:58

标签: php sql email

我想将表添加到电子邮件中:

foreach ($songs as $key => $value) {
            echo "<tr><td>" . ucfirst($key) . "</td><td>" . $value . "</td></tr>";
        }

这个之类的东西不起作用:

$message = '
<html>
<head>
</head>
<body>
  <p>Hi, ' . ucfirst($name) . '<br>
      </p>
      <p>Heres the table</p>
        <table class="tables">' .
                foreach ($songs as $key => $value) {
            echo "<tr><td>" . ucfirst($key) . "</td><td>" . $value . "</td></tr>";
        }
                . '
        </table>    
</body>
</html>
';

提前致谢! :)

3 个答案:

答案 0 :(得分:5)

问题是你是在回应而不是追加。

$message = '
<html>
<head>
</head>
<body>
  <p>Hi, ' . ucfirst($name) . '<br>
      </p>
      <p>Heres the table</p>
        <table class="tables">';
                foreach ($songs as $key => $value) {
            $message .= "<tr><td>" . ucfirst($key) . "</td><td>" . $value . "</td></tr>";
        }
$message .= '</table>    
</body>
</html>
';

答案 1 :(得分:1)

为什么不将表生成变量,然后将其包含在您的消息中......

foreach ($songs as $key => $value) {
   $mytable .= "<tr><td>" . ucfirst($key) . "</td><td>" . $value . "</td></tr>";
}

$message = "blah blah" . $mytable . "blah blah";

答案 2 :(得分:1)

您需要结束对$message的值的赋值,例如:

...
<table class="tables">'; // stop here

然后执行你的foreach,通过连接运算符将结果追加到$message,例如:

foreach ($songs as $key => $value) {
   $message .= '<tr><td>' . ucfirst($key) . '</td><td>' . $value . '</td></tr>';
}

然后再次使用$message字符串:

$message = '</table>    
</body>
</html>';