douzhanlie9209 2014-04-03 19:51
浏览 25
已采纳

HTML电子邮件中的PHP代码?

This is probably a very stupid question but I can't seem to find an answer anywhere. Can php code be used inside the body of a html email?

I have only every used variable names inside an email before i.e.

'<p>Dear'.$name.'</p>

But what if I want to make a table of data and use a loop? Can I put the php loop code in the email html? I have tried but it does not seem to like my syntax no matter what I try.

Just to be clear I have no problems sending the email.

  • 写回答

2条回答 默认 最新

  • douzhi1924 2014-04-03 20:06
    关注

    PHP is a server-side language. You can use PHP on your server to create a HTML code for your e-mail. But you cannot include the PHP code in your email.

    Example of creating a table with PHP for an email.

    <?php
    $my_data = Array(
                        Array("John", "32",  "Male"),
                        Array("Casey", "28",  "Male"),
                        Array("Peter", "43",  "Male"),
                        Array("Michael", "19",  "Male"),
                        Array("Samantha", "22",  "Female")
    );
    
    
    $table = "<table>";
    $table .= " <tr>";
    $table .= "     <td>Name</td>";
    $table .= "     <td>Age</td>";
    $table .= "     <td>Gender</td>";
    $table .= " </tr>";
    foreach($my_data AS $value)
    {
        $table .="<tr>";
        $table .= " <td>".$value[0]."</td>";
        $table .= " <td>".$value[1]."</td>";
        $table .= " <td>".$value[2]."</td>";
        $table .="</tr>";
    }
    
    $table .= "</table>";
    
    mail("info@domain.com", "Our users", $table);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 如何通过代码传输视频到亚马逊平台
  • ¥15 php查询mysql数据库并显示至下拉列表中
  • ¥15 freertos下使用外部中断失效
  • ¥15 输入的char字符转为int类型,不是对应的ascall码,如何才能使之转换为对应ascall码?或者使输入的char字符可以正常与其他字符比较?
  • ¥15 devserver配置完 启动服务 无法访问static上的资源
  • ¥15 解决websocket跟c#客户端通信
  • ¥30 Python调用dll文件输出Nan重置dll状态
  • ¥15 浮动div的高度控制问题。
  • ¥66 换电脑后应用程序报错
  • ¥50 array数据同步问题