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条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口