2011-03-11 67 views
0

这是我尝试在将“购物车”发送给管理员时尝试使用的方法。这可能是一个非正统的方法,请任何建议都会比欢迎。在PHP邮件中包含一个数组的每个循环,如HTML表

此信息来自一个会议,加上一个单独的脚本,它运行良好。它显示了我,然后我试图通过电子邮件发送,以及一些个人信息,购物车的内容:

<?php foreach ($quotes as $quote): ?> 
    <tr> 
    <td class="quoteTdL"><h3><?php echo htmlspecialchars($quote['name'], ENT_QUOTES,'UTF-8'); ?></h3></td> 
    <td class="quoteTdR"><p><?php echo htmlspecialchars($quote['text'], ENT_QUOTES,'UTF-8'); ?></p></td> 
    </tr> 
<?php endforeach; ?> 

基本上我想有这个信息在此砍伐邮件脚本:

//send email 
    $to = ""; 

    $fname = $_REQUEST['fname'] ; 
    $sname = $_REQUEST['sname'] ; 
    $email = $_REQUEST['email'] ; 
    $pnum = $_REQUEST['pnum'] ; 
    $mnum = $_REQUEST['mnum'] ; 
    $content = $_REQUEST['content'] ; 

    $subject = 'Email from SAiGE Longlife website'; 

    $msg = ' 
    <html> 
    <head> 
     <title>SAiGE Longlife Decking enquiry</title> 
    </head> 
    <body> 
     <table width="600" border="0" align="center" cellpadding="10"> 
     <tr> 
      <td colspan="2" align="center" bgcolor="#FF9933" style="font: 18px Arial,Georgia,Serif; color:#fff;">SAiGE Longlife Decking enquiry</td> 
     </tr> 
     <tr> 
      <td width="200" bgcolor="#CCCCCC" style="font: 18px Arial,Georgia,Serif; color:#fff;">Name:</td> 
      <td width="400" bgcolor="#FF9933" style="font: 18px Arial,Georgia,Serif; color:#000;">'; 
     $msg .=$fname; 
     $msg .='&nbsp;'; 
     $msg .=$sname; 
     $msg .=' 
      </td> 
     </tr> 
     <tr> 
      <td bgcolor="#CCCCCC" style="font: 18px Arial,Georgia,Serif; color:#fff;">E-mail</td> 
      <td bgcolor="#FF9933" style="font: 18px Arial,Georgia,Serif; color:#000;">'; 
     $msg .=$email; 
     $msg .=' 
      </td> 
     </tr> 
     <tr> 
      <td bgcolor="#CCCCCC" style="font: 18px Arial,Georgia,Serif; color:#fff;">Phone numbers:</td> 
      <td bgcolor="#FF9933" style="font: 18px Arial,Georgia,Serif; color:#000;">'; 
     $msg .=$pnum; 
     $msg .=' 
      </td> 
     </tr> 
     <tr> 
      <td bgcolor="#CCCCCC">&nbsp;</td> 
      <td bgcolor="#FF9933" style="font: 18px Arial,Georgia,Serif; color:#000;">'; 
     $msg .=$mnum; 
     $msg .=' 
      </td> 
     </tr> 
     <tr> 
      <td colspan="2">&nbsp;</td> 
     </tr> 
     <tr> 
      <td colspan="2" bgcolor="#CCCCCC" style="font: 18px Arial,Georgia,Serif; color:#fff;">Message:</td> 
     </tr> 
     <tr> 
      <td colspan="2" bgcolor="#FF9933" style="font: 18px Arial,Georgia,Serif; color:#000;">'; 
     $msg .=$content; 
     $msg .=$cartHtml; 
     $msg .=' 

      </td> 
     </tr> 
     </table> 
    </body> 
    </html> 
    '; 

我已经尝试过内爆和整合事情的边缘,但没有运气,并花了很长时间!

是否有可能首先输出循环作为变量,然后包含或不必要?

所有帮助将不胜感激。

非常感谢,

汤姆

回答

1

对于爱情,也没有钱,我不能让ob_start解决工作输出。在发布这个问题之前,我尝试了很多次。我能想象的是,发布到另一个页面会清除缓冲区,因此不会有数据被回显?

这是我用于邮件的最终代码。

消息: '; $ msg。= $ content;

foreach ($quotes as $quote): 

     $name = htmlspecialchars($quote['name'], ENT_QUOTES,'UTF-8'); 
     $text = htmlspecialchars($quote['text'], ENT_QUOTES,'UTF-8'); 

    $msg .= ' 
     <tr> 
      <td class="quoteTdL"><h3>'; 

    $msg .= $name; 

    $msg .= ' 
    </h3></td> 
      <td class="quoteTdR"><p>'; 

    $msg .=$text; 

    $msg .= '</p></td> 
     </tr>'; 

     endforeach; 
    $msg .=' 

     </td> 
    </tr> 

希望这有助于

汤姆

1

快速示例的固定。

foreach ($quotes as $quote): 

    $name = htmlspecialchars($quote['name'], ENT_QUOTES,'UTF-8'); 
    $text = htmlspecialchars($quote['text'], ENT_QUOTES,'UTF-8'); 

    $msg .= <<<EOS 
    <tr> 
    <td class="quoteTdL"><h3>{$name}</h3></td> 
    <td class="quoteTdR"><p>{$text}</p></td> 
    </tr> 
EOS; 
endforeach; 

// -- The output has now been added to $msg. 
+0

杰夫。非常感谢。我很确定我以前也尝试过这种方法,但都无济于事。为了解决这个问题,我不得不将PHP中的HTML分成单独的$ msg。's。我赞赏重新思考! – 2011-03-15 09:50:02

+0

我不明白为什么它不能正常工作,但只要你找到了解决方案,这一切都很好:P – 2011-03-15 13:31:55

0

是的,可以先输出循环。你需要调用输出缓冲功能:

ob_start(); 
echo "Some output here"; 
$out = ob_get_clean(); 

的$ out变量将包含你需要

相关问题