我需要一些帮助来构建我尝试使用PHP发送的HTML电子邮件的正确结构。以下是发送电子邮件的行:
$this->_send_user_email($recipient, $subject, $message);
我遇到麻烦的是构建$message
。我需要$message
包含一个动态行数的表。以下是$message
变量的当前结构:
$message = "Hello, <br><br>
Please review details in the table below:
<br><br>
**** NEED TO INSERT DYNAMIC PHP TABLE HERE ****
<br><br>
If you have questions, please call 1-888-888-8888 and we will assist you.
<br><br>
Customer Support<br>
<br><br>
";
我不确定如何在现有的$messsage
变量中插入我的PHP foreach循环。我对何时关闭引号以及如何继续等感到困惑。以下是我试图插入的foreach循环的代码:
<?php if (is_array($foo['bar'])): ?>
<table>
<tr>
<th >1</th>
<th >2</th>
<th >3</th>
</tr>
<?php $row = 01;
foreach ($foo['bar'] as $key => $value): ?>
<tr>
<td ><?php echo str_pad($row++, 2, '0', STR_PAD_LEFT); ?></td>
<td >AAA</td>
<td >BBB</td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
所以我想我的问题是:将这个PHP foreach循环插入上面的$message
变量的正确方法是什么?
答案 0 :(得分:2)
这样做的方法是将模板消息拆分为两个字符串,例如$ message1和$ message2,其中第一个包含所有内容直到动态表,后者包含在表后面的所有内容。然后使用字符串连接生成动态表,并连接整个消息的三个部分。
$message1 = "Hello, <br><br>
Please review details in the table below:
<br><br>";
$message2 = "<br><br>
If you have questions, please call 1-888-888-8888 and we will assist you.
<br><br>
Customer Support<br>
<br><br>";
$row = "01";
$table = "";
if(is_array($foo['bar'])) {
$table .= "<table>
<tr>
<th >1</th>
<th >2</th>
<th >3</th>
</tr>";
foreach($foo['bar'] as $key => $value) {
$table .= "<tr>
<td >" . str_pad($row++, 2, '0', STR_PAD_LEFT) . "</td>
<td >AAA</td>
<td >BBB</td>
</tr>";
}
$table .= "</table>";
}
$message = $message1 . $table. $message2;
现在正如评论中提到的那样,这段代码可以正常运行但是非常混乱,因为你大量混合了php逻辑和普通的html文本。理想情况下,您希望尽可能地将这两者分开。
答案 1 :(得分:1)
您可以使用发送给您的用户的php创建静态HTML消息,但是您不能在您的电子邮件中包含php(如果这是您正在尝试做的事情),以便电子邮件是动态的,因为它是显示。所以假设你正在做前者,那么joe42s答案中的代码就是现实。他写的是给你的!
如果你想要一个动态更新的电子邮件,当人们看到它时会发生变化(使用php),你可以生成一个链接,在电子邮件中发送带有参数的链接,这些参数会显示为用户定制的网页。
答案 2 :(得分:0)
<?php
$message = "Hello, <br><br>
Please review details in the table below:
<br><br>";
if (is_array($foo['bar'])) {
$message .= "<table>
<tr>
<th >1</th>
<th >2</th>
<th >3</th>
</tr>";
$row = 01;
foreach ($foo['bar'] as $key => $value) {
$message .= "<tr>
<td >".str_pad($row++, 2, '0', STR_PAD_LEFT)."</td>
<td >AAA</td>
<td >BBB</td>
</tr>";
}
$message .= "</table>";
}
$message .= "<br><br>
If you have any questions, please call 1-888-888-8888 and we will be happy to assist you.
<br><br>
Customer Support<br>
<br><br>
";
?>
答案 3 :(得分:0)
嗯,你的方式可能会有某种方式,但我可能会建议用更合适的方式发送电子邮件 您可以将视图用作消息内容,例如:
$data=$foo;
$message = $this->load->view('some view',$data,true);
您可以将其视为普通视图