dongrou839975 2014-07-28 06:20
浏览 50
已采纳

PHP Mailer中的动态HTML

I have a database with the following fields

  1. ID
  2. title
  3. URL
  4. summary

This data is stored in a multi dimensional associative array like this:

$array[0][title] = Title-1
$array[0][summary] = Summary-1
$array[1][title] = Title-2
$array[1][summary] = Summary-2

I also have a script that can loop through these and render as needed.

However I'm finding it tough getting this output into the body of a mail.

Looking around got me the following

  1. Dynamic Content into mail() $message
  2. Dynamic HTML Table in PHP Mail
  3. HTML mail with dyanmic values
  4. How to send HTML Dynamic Table as mail in php?

I found many more solutions via google, but they're more or less similar to the one's above.

These have not been helpful as the content is part static and part dynamic. In my case the whole body would be dynamic

Here's what I have as of now

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>";
}

The output rendered here is not the final output. I'm using this just for testing purpose.

My problem is, a foreach function(or anything similar that would loop through the array) cannot be part of $message(body of the mail) mailer and I need to have a mechanism as the data is dynamic

Hope I have been clear enough. Do let me know if you need more specifics

  • 写回答

1条回答 默认 最新

  • drtiwd06558 2014-07-28 06:27
    关注

    You simply need to assign your output to variable and then use it in PhpMailer as in the following code:

    $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
    

    You also need to use IsHTML method to tell phpMailer to send the content as HTML, you should also set AltBody to display your mail for people who don't want/cannot display it in HTML format. Above I used a very simple way to transform html to text. However you can put here any other text as you want.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下:
  • ¥15 Mac(标签-IDE|关键词-File) idea
  • ¥15 潜在扩散模型的Unet特征提取
  • ¥15 iscsi服务无法访问,如何解决?
  • ¥15 感应式传感器制作的感应式讯响器
  • ¥15 如何使用SC92F8003固件库解析私有协议数据?
  • ¥15 如何在音频中嵌入字符串(水印)信息进行传递
  • ¥30 plc怎么以设计说明书申请软著
  • ¥15 硬盘识别不了,需要初始化,可我的数据怎么办
  • ¥15 lvm2被mask了,怎么unmask都没用(标签-ubuntu|关键词-apt)