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 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了