【问题标题】:create html table with variable rows using php inside phpmailer在 phpmailer 中使用 php 创建具有可变行的 html 表
【发布时间】:2015-09-29 15:31:07
【问题描述】:

所以我正在尝试通过电子邮件向使用 phpmailer 的人发送消息。当这是电子邮件时,它需要可以具有可变数量的行。我在这里找到了一些代码,这些代码为我指明了正确的方向,但是当我将此代码放入消息中时,发生了一些事情。

<thead>
    <tr>
        <th>Item Name</th>
        <th>SKU Number</th>
        <th>Price</th>
    </tr>
</thead>
<tbody>
    <?php foreach($stuff as $trow) : ?>
    <tr>
    <td><?php echo $trow->$name; ?></td>
    <td><?php echo $trow->$sku; ?></td>
    <td><?php echo $trow->$price; ?></td>
    </tr> 
    <?php endforeach; ?>
</tbody>

首先,foreach 循环在第一行的末尾关闭,至少这是我的假设,因为在第一行之后我没有其他行。其次,当这发送电子邮件时,我收到了电子邮件,但分号、问号和右括号都与每个单元格中的数据一样。但是,我为第一行获得的数据是准确的。只是一无所获。我是否缺少此代码的某些内容?我应该发布所有代码以帮助缩小范围吗?我发布了我的 phpmailer 代码。是不是我试图调用 php 两次?虽然我认为这并不重要,因为 $message 内容是发送到电子邮件地址的 html 电子邮件。

to_name="$firstname $lastname";
$to="to@email.address";
$subject="Your store order for: $date";
$headers="MIME-VERSION1.0\r\n";
$headers .="Content-Type: text/html; charset=ISO-8859-1\r\n";

$message="Dear $custfirstname $custlastname, Thank you for choosing this store for your purchase. You purchsed the following items on $date at store $store.<br>";

$message .="<table border=\"1\">
<thead>
    <tr>
        <th>Item Name</th>
        <th>SKU Number</th>
        <th>Price</th>
    </tr>
</thead>
<tbody>
    <?php foreach($stuff as $trow) : ?>
    <tr>
    <td><?php echo $trow->name; ?></td>
    <td><?php echo $trow->sku; ?></td>
    <td><?php echo $trow->price; ?></td>
    </tr> 
    <?php endforeach; ?>
</tbody>
</table>";


$message .="Please fill out these surveys below to tell us how your         experience was with our company.";

$from_name="from name";
$from = "from@email.address";

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host ="mailhost";
$mail->Port =25;
$mail->SMTPAuth = false;
$mail->Username="mailboxsendingmessage";
$mail->Password="passwordforsendingmailbox";
$mail->FromName=$from_name;
$mail->From=$from; 
$mail->AddAddress($to, $to_name);
$mail->Subject = $subject;
$mail->isHTML(true);
$mail->Body = $message; 

$result = $mail->Send();
echo $result ? 'Sent' : 'Error';
$mailsend = NULL;
$mailsend = $result;

echo "$mailsend";

编辑:添加了我的 php 邮件程序代码。

【问题讨论】:

  • &lt;?php var_dump($stuff); ?&gt; 的输出是什么?这将告诉您 $stuff 变量的结构是否正确以及是否包含您要打印的所有行。除此之外,语法对我来说是正确的。
  • 我怀疑您在 echo 语句中包含此代码,而不是在 HTML 代码块中。
  • 如果你硬编码$stuff 然后运行这个循环,它会打印出什么?即制作一个Minimal, Complete, and Verifiable example。我怀疑您的问题出在这个特定的代码块之外,但我们永远不会知道您是否不对其进行测试。
  • 如果你将该代码块作为 HTML 传递给浏览器,你会在不同的行上得到 Item Name SKU Number Price$name; ?&gt; $sku; ?&gt; $price; ?&gt;,看起来和你得到的一样吗?你能提供这段代码生成的 HTML 的相关 sn-p 吗?

标签: php html


【解决方案1】:

你可以像这样使用html

    $message .='<table border="1">
<thead>
    <tr>
        <th>Item Name</th>
        <th>SKU Number</th>
        <th>Price</th>
    </tr>
</thead>
<tbody>';

    <?php foreach($stuff as $trow) { ?>

    $message .='<tr>
    <td>'.$trow->name.'</td>
    <td>'.$trow->sku.'</td>
    <td>'.$trow->price.'</td>
    </tr>'; 
    <?php } ?>
$message .='</tbody>
</table>';

【讨论】:

  • 这非常有效。我必须进行更改以确保由于我的错误而显示我的数据,但否则这是完美的。谢谢。
【解决方案2】:

我发现以这种方式循环令人困惑,我总是更喜欢heredoc
这是我通常做的:

<thead>
    <tr>
        <th>Item Name</th>
        <th>SKU Number</th>
        <th>Price</th>
    </tr>
</thead>
<tbody>
<?php

foreach($stuff as $trow){
echo <<< LOL
<tr> 
<td>{$trow->$name}</td>
<td>{$trow->$sku}</td>
<td>{$trow->$price}</td>
</tr> 
LOL;
}

?>
</tbody>

您最终会减少代码,从而减少错误。

【讨论】:

  • 这会在 phpmailer 的 html 消息的内容中起作用吗?打算试一试,看看效果如何。
  • 我之前使用过heredocPHPMailer,它可以正常工作,此外,请确保设置正确的 mime 类型来处理电子邮件中的 html。
  • 很遗憾,heredoc 不起作用。现在表格返回为空,
  • 我确定这是我在格式化方面遇到的问题,但我不确定。
【解决方案3】:

使用附加代码,问题变得清晰。

您将 PHP 语句插入到字符串中,然后通过电子邮件发送。如果您查看该电子邮件的来源,您可能会看到未执行的 PHP 代码。

您不能在字符串中插入 foreach 循环。相反,您将需要使用循环逐步构建字符串,例如

$message .="<table border=\"1\">
<thead>
    <tr>
        <th>Item Name</th>
        <th>SKU Number</th>
        <th>Price</th>
    </tr>
</thead>
<tbody>";

foreach($stuff as $trow) :
    $message .= "<tr>
    <td>".$trow->name."</td>
    <td>".$trow->sku."</td>
    <td>".$trow->price."</td>
    </tr>";
endforeach;
$message .= "</tbody>
</table>";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    • 2019-10-28
    • 2020-10-22
    • 2013-01-30
    • 2013-01-10
    • 1970-01-01
    相关资源
    最近更新 更多