【问题标题】:Need stylesheet before ob_endflush() and before header(location: ), How?在 ob_endflush() 和 header(location: ) 之前需要样式表,如何?
【发布时间】:2014-01-21 04:23:44
【问题描述】:

我的页面结构如下:

<?php
  session_start();
  ob_start();
?>

HTML HEADER
links to stylesheets global.css and mail.css

<?php
  function email(){
     /* using the stylesheets global.css and mail.css */
     $text = '<div class="test">
                 <p id="par">...
                    <span id="write" class="text">...</span>
                 </p>
              </div>';

     return $text;
  }
?>


/* some text and html stuff */

<?php
  if(isset(...)){
    /* php form validation */

    /* after validating I send an email to the user where I call
       the function email() which use the stylesheets. */
    $mail_text = 'Dear .....';
    $mail_text .= email();

    $mail_headers = "From: test@test.com\r\n".
                     'X-Mailer: PHP/' . phpversion().
                     'MIME-Version: 1.0' . "\r\n".
                     'Content-type: text/html; charset=utf-8' . "\r\n";

     if(mail(..., ..., $mail_text, $mail_headers)){
         header('location: newlocation.php');
     }
?>

/* HTML FORMS and text and .... */

<?php
   ob_end_flush();
?>

电子邮件与来自 email() 函数的文本一起发送,但没有格式化导致 CSS 仅在页面底部的 ob_end_flush() 之后“打印出来”。

我该如何解决? email()里面有很多类和样式,所以每次都写&lt;div style="..."&gt;之类的不是很好的解决办法。

【问题讨论】:

    标签: php css header styles


    【解决方案1】:

    您对输出缓冲感到困惑。您在页面开头打印的 html/css 不会在任何地方被捕获,也不会成为 $mail_text 的一部分。仅仅因为您正在使用输出缓冲并不会使该缓冲区神奇地出现在变量中。您至少需要$mail_text .= ob_get_clean() 或其他东西来提取缓冲区的内容并将其放入您的变量中。

    【讨论】:

    • $mail_text .= ob_get_contents();在调用 ob_get_clean() 之前调用它;
    • 这正是我写的,不是吗? “但没有格式化导致 CSS 仅在页面底部的 ob_end_flush() 之后“打印出来”。”
    • 是的,因为 ob_end_flush() 将输出转储到客户端。该输出未存储在变量中。您的代码有两个输出路径:电子邮件和客户端浏览器。它们没有任何关系,因为您没有编写任何代码来链接它们。
    • 不要构建自己的 mime 电子邮件,尤其是当您刚开始使用 PHP 时。使用 phpmailer 或 swiftmailer。他们使构建 html 邮件变得微不足道。
    猜你喜欢
    • 1970-01-01
    • 2019-07-13
    • 2014-07-22
    • 1970-01-01
    • 2013-05-22
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    • 1970-01-01
    相关资源
    最近更新 更多