【问题标题】:How to return the contents of a HTML file without executing the code?如何在不执行代码的情况下返回 HTML 文件的内容?
【发布时间】:2014-12-03 05:40:29
【问题描述】:

我有两个文件,一个包含电子邮件布局和基本 CSS 的 HTML 文件和一个实际发送电子邮件的 PHP 文件。我想知道是否有任何方法可以要求或包含 HTML 文件作为要从 PHP 脚本发送的消息。

例如:

HTML

<h1>A title</h1>

PHP

mail("email", "subject", require('htmlfile'));

我尝试过类似的方法,但它只是将 HTML 文件的内容放入 PHP 脚本的流程中。有什么办法让 HTML 文件只返回我可以保存到变量或其他东西的文本?

提前致谢!

【问题讨论】:

  • 你可以在你的 html 文件中内联你的 php 代码或将你的 php 文件放入变量中,这样你就可以得到那个值并传递给 $message。
  • $body = file_get_contents('yourhtmlfile.html');

标签: php html css email


【解决方案1】:
mail("email", "subject", file_get_contents('file_name.html'));

【讨论】:

    【解决方案2】:

    PHP documentation 状态:Also, it's possible to return values from included files. You can take the value of the include call as you would for a normal function.

    它提供了如何执行此操作的示例,但您需要修改代码以满足您的需求。

    return.php
    <?php
    
    $var = 'PHP';
    
    return $var;
    
    ?>
    
    noreturn.php
    <?php
    
    $var = 'PHP';
    
    ?>
    
    testreturns.php
    <?php
    
    $foo = include 'return.php';
    
    echo $foo; // prints 'PHP'
    
    $bar = include 'noreturn.php';
    
    echo $bar; // prints 1
    
    ?>
    

    【讨论】:

      【解决方案3】:

      你可以试试这个,肯定会奏效的。

      Your PHP file:
      "msg.php"
      <?php
      
         $message = "This is a sample message";
      
      ?>
      
      Your HTML file with a mail() function:
      
      "mail.php"
      <?php
      
         require ('msg.php');
         mail("sample.yahoo.com", "sample subject", $message);
      
      ?> 
      
      <html>
      <head>
      <body>
      <h1>A title</h1>
      </body>
      </head>
      </html> 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多