我有一个包含以下字段的数据库
这些数据存储在多维关联数组中,如下所示:
$array[0][title] = Title-1
$array[0][summary] = Summary-1
$array[1][title] = Title-2
$array[1][summary] = Summary-2我也有一个脚本,可以循环这些和渲染的需要。
然而,我发现很难将这个输出输入到邮件中。
环顾四周让我看到了下面的
我通过谷歌找到了更多的解决方案,但它们或多或少与上面的解决方案相似。
这些都没有帮助,因为内容部分是静态的,部分是动态的。在我的例子中,整个身体都是充满活力的
这是我现在所拥有的
function get_mailer_contents($email_category) {
global $mailer_contents;
$fetch_mailer_contents = mysql_query("SELECT * from `selected_posts` WHERE industry='$email_category'");
$id = "0";
while($temp_mailer_contents = mysql_fetch_array($fetch_mailer_contents))
{
$mailer_contents[$id]['title'] = $temp_mailer_contents['title'];
$mailer_contents[$id]['description'] = $temp_mailer_contents['description'];
$id ++;
}
}
get_mailer_contents($email_category);
foreach($mailer_contents as $title_cat)
{
echo "==================";
echo "<br>";
echo $title_cat['title'];
echo "<br>";
echo $title_cat['description'];
echo "<br>";
}这里呈现的输出不是最终的输出。我用这个只是为了测试。
我的问题是,foreach函数(或任何类似于循环遍历数组的函数)不能成为$message(邮件体)邮件程序的一部分,而且由于数据是动态的,所以我需要一种机制。
希望我说得够清楚了。如果你需要更多的细节,请告诉我。
发布于 2014-07-28 06:27:07
只需将输出分配给变量,然后在PhpMailer中使用它,如下代码所示:
$html = '<html><head><meta charset="utf-8" /></head><body>';
foreach ($array as $item) {
$html.= '<p>'.$item['title'].' '.$item['summary']."</p>";
}
$html.= '</body></html>';
// ... setting up phpMailer
$phpMailer->Body = $html;
$phpMailer->AltBody = nl2br(strip_tags($html));
$phpMailer->IsHTML(true); // you need to set HTML format您还需要使用IsHTML方法告诉phpMailer以HTML的形式发送内容,还应该设置AltBody来为不想/不能以HTML格式显示邮件的人显示您的邮件。上面,我使用了一种非常简单的方法将html转换为文本。然而,您可以在这里,任何其他文本,你想要。
https://stackoverflow.com/questions/24989421
复制相似问题