PHP 电子邮件当前页面


php email current page

我制作了一个订购系统,现在从产品表单到评论页面。我将页面设置为使用 PHP 循环来显示他们从上一页中选择的产品,效果很好。现在我需要有一个"订单"按钮来通过电子邮件发送订单。我的做法是编写更少的代码,我不想重新编写所有代码以在另一个php表单上通过电子邮件发送此表单。有没有办法在此页面上包含电子邮件标题并将生成的 html 转储到要发送的电子邮件正文中?

以下是我如何从上一页循环表单内容。基本上,它会遍历所有产品,如果存在,请在评论中显示它们。如果没有,请不要。

for ($i=0; $i<8; $i++) {
        if (!empty($_REQUEST['driver'][$i]['select'])) {
            //add the club to the total of clubs
            $club = $_REQUEST['driver'][$i]['select'];
            array_push($club_total, $club);
            //add the price to the cost
            list($shaft, $price) = explode(":", $_REQUEST['driver'][$i]['shaft']);
            array_push($cost, $price);
            //show the user
            echo '<h3>' .$_POST['driver'][$i]['name']. ' - $'. money_format('%i', $price).'</h3>';
            echo '<ul>';
                if (!empty($_REQUEST['driver'][$i]['left'])) {
                    echo '<li><strong>Left Handed</strong></li>';
                }
                echo '<li>Length: '.$_POST['driver'][$i]['length']. '"</li>';
                echo '<li>Shaft: '.$shaft. '</li>';
                if (!empty($_REQUEST['driver'][$i]['hossel'])) {
                    echo '<li>Purist Hossel: ' .$_POST['driver'][$i]['hossel'].'</li>';
                }
                if (!empty($_REQUEST['driver'][$i]['color'])) {
                    echo '<li>Purist Color: ' .$_POST['driver'][$i]['color'].'</li>';
                }
            echo '</ul>';
        }

您可以在输出缓冲区中捕获 html,然后将其存储在变量中以供以后使用(例如,将其放入电子邮件正文中)。

ob_start();
for ($i=0; $i<8; $i++) {
  // ... your code
}
$my_html = ob_get_contents();
ob_end_clean();

然后将$my_html作为您的电子邮件正文并发送。

第一次将它们全部放在变量中的想法也有效,但这可能会节省您重写所有内容的工作量。

与其回显出 html,不如建立一个字符串,这样$out .= "你的 html" 等,那么你可以轻松地将那块 html 设置为电子邮件函数的主体。

我假设您知道如何编写代码,否则我将为您添加代码示例。