【问题标题】:Pass PHP variable from html to php and email template将 PHP 变量从 html 传递到 php 和电子邮件模板
【发布时间】:2015-09-02 16:48:06
【问题描述】:

我正在使用电子邮件模板进行简单的 PHP 表单验证。我有一个名为“index.html”的简单表单,并有提交按钮,表单方法为“post”,动作为“sendmail.php”。在我的 sendmail.php 中,有 smtp-mailer.php,其电子邮件模板称为“email-template.html”。我如何通过 sendmail.php 将变量从 index.html 传递到 mail-template.php... 明白我的意思吗???我知道使用 SESSION。但我不知道我应该在哪里调用它以及如何获取它???有什么想法...???

index.html

<form method="post" action="sendemail.php">
  Email: <input name="email" id="email" type="text" /><br />
  Message:<br />
  <textarea name="message" id="message" rows="15" cols="40"></textarea><br />
  <input type="submit" value="Submit" />
</form>

sendmail.php

<?php
include "class.smtp.php";
include "class.phpmailer.php";
session_start();
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$_SESSION['message'] = $message;

$Body = file_get_contents('email-template.php');
...
...
?>

email-template.php(将其发送到不在浏览器中的电子邮件)

<table border="1">
     <tr>
        <td colspan="2">
          <h3>
            <?php
              session_start();
              $message = $_SESSION['message'];
              echo "Your registration is: ".$message.".";
            ?>
          </h3>
        </td>
     </tr>
   </table>

更新:我做了同样的事...但没有回应...我错过了 sendmail.php 中的某些内容

【问题讨论】:

  • 您不能在 HTML 文件中编写 PHP 代码。
  • 除非您已指示 Apache 将 .html 文件视为 PHP,否则您需要将 email-template.html 重命名为 email-template.php - 该文件将无法正确解析跨度>
  • @MihirBhatt “你不能在 HTML 文件中编写 PHP 代码。” - 不完全正确。见我上面的评论^
  • 查看我上面的更新:(
  • 将错误报告添加到文件顶部,紧跟在打开 PHP 标记之后,例如 &lt;?php error_reporting(E_ALL); ini_set('display_errors', 1);,然后是其余代码,看看它是否产生任何结果。

标签: php html session phpmailer


【解决方案1】:

您正在尝试将您的电子邮件放入自己的模板文件中,这很好,但您需要实际执行其中的 PHP 代码来填写变量。

将 email-template.html 重命名为 email-template.phtml。这是一个相当标准的 php 模板扩展。 值得注意的是,不建议在 PHP 中使用 $_REQUEST,因为它需要 POST 和 GET 参数,并且您决定用户需要 POST 表单。

sendmail.php中包含并执行模板文件:

<?php
include "class.smtp.php";
include "class.phpmailer.php";

function render_email($email, $message) {
    ob_start();
    include "email-template.phtml";
    return ob_get_contents();
}

$email = $_POST['email'] ;
$message = $_POST['message'];

$body = render_email($email, $message);
...
...

render_email 包含模板文件,将两个变量 $email 和 $message 暴露给该模板文件,然后返回包含模板的输出。

您可以像以前一样在 template.phtml 中使用这些变量。 电子邮件模板.phtml:

<table border="1">
 <tr>
 <td colspan="2">
    <h3>
        Your registration is:<?= htmlspecialchars($message) ?>
</h3>
</td>
</tr>
</table>

【讨论】:

    【解决方案2】:

    您可以通过 3 种方式做到这一点。使用会话或 cookie 在链接中注入 var。我个人建议使用会话,因为用户无法修改它们,并且当用户关闭浏览器时它们会过期。

    您需要将 index.html 重命名为 index.php,以便将 php 代码插入此文件,然后您需要编写如下内容:

    <?php
    session_start();
    $_SESSION['name'] = "value";
    ?>
    

    这样做你设置了会话,现在每次你需要调用会话时使用这个代码:

    <?php
    session_start();
    $var = $_SESSION['name'];
    ?>
    

    现在你使用

    echo "$var";
    

    你想在哪里打印 var

    不要忘记session_start();,否则您将无法呼叫或设置会话

    如果您想使用 cookie 或将 var 注入到 url 中,请随时询问,我会向您解释如何;)

    您在 sendmail.php 中的代码有点乱,这里是固定的:

    <?php
    include "class.smtp.php";
    include "class.phpmailer.php";
    session_start();
    $email = $_REQUEST['email'] ;
    $message = $_REQUEST['message'] ;
    $_SESSION['message'] = $message;
    
    $Body = file_get_contents('email-template.php');
    ...
    ...
    ?>
    

    然后在 email-template.php 中您将使用以下代码:

    <table border="1">
         <tr>
            <td colspan="2">
              <h3>
                <?php
                  session_start();
                  $message = $_SESSION['message'];
                  echo "Your registration is: ".$message.".";
                ?>
              </h3>
            </td>
         </tr>
       </table>
    

    【讨论】:

    • 我发这个的时候不一样了,嗯...下面的答案应该也可以,试试吧
    • 添加error_reporting(E_ALL);在代码的顶部,会发生什么?
    • 当我与 ini_set('display_errors', 1); 一起粘贴时在 sendemail.php 中没有错误显示
    • 尝试使用,而不是包含,require_once。此外,在我自己的脚本中,我什至会使用 require_once 而不是 file_get_contents ...尝试这样做但删除 session_start();在 email-template.php 中
    • 希望你知道,我的 email-tempate.php 是一个电子邮件模板,它是通过电子邮件接收的,而不是在浏览器中运行的 php 文件。那你能给我一个解决方案吗
    【解决方案3】:

    您为什么不尝试在 email-template.html 中放置一些消息占位符,然后在 sendmail.php

    中将其替换为真实消息

    例如: 电子邮件模板.html

    <table border="1">
     <tr>
     <td colspan="2">
        <h3>{{message-placeholder}}</h3>
    </td>
    </tr>
    </table>
    

    然后在 sendmail.php

    <?php
    include "class.smtp.php";
    include "class.phpmailer.php";
    session_start();
    $email = $_REQUEST['email'] ;
    $message = $_REQUEST['message'] ;
    $_SESSION['message'] = $message;
    
    $Body = file_get_contents('email-template.php');
    $Body = str_replace('{{message-placeholder}}', $message, $Body);
    ...
    ...
    ?>
    

    http://php.net/str_replace

    这样您将拥有 html 模板,并且您可能还会拥有一些非常基本的模板。您还将摆脱那些会话的东西

    【讨论】:

      【解决方案4】:

      试试这样的

      $name = 'John Doe';    
      $vars = ('NAME' => $name);
      $message = file_get_contents('email_template.html');
      foreach($vars as $search => $replace){
          $message = str_ireplace('%' . $search . '%', $replace, $message); 
      }
      mail('john@doe.com', 'Subject', $message, $headers);
      

      所以email_template.html 内部将包含%NAME%str_ireplace 将替换为John Doe,您将变量$message 作为电子邮件html 发送。

      我认为这是最好的做法。

      我想它会对你有所帮助。

      【讨论】:

      • 希望你知道,我的 email-tempate.php 是一个电子邮件模板,它是通过电子邮件接收的,而不是在浏览器中运行的 php 文件。那你能给我一个解决方案吗
      • 这很奇怪,通常没有人使用 .php 文件作为 html 模板。我们所做的是使用带有一些通配符(例如 %NAME%)的 .html 文件,然后我们用运行外部 php 脚本的变量替换这个通配符,正如我在回答中所解释的那样。我没有看到将所有模板存储在会话变量中的意义。对不起!
      【解决方案5】:

      对于那些仍然面临这个问题的人来说,我认为这就是它的全部意义所在。

      在模板中应指定要显示的字符串(使用特殊字符)而不是通过 php 打印。

      email_template.php

      <table border="1">
       <tr>
          <td colspan="2">
            <h3>
              Your Registration number is: #registration_number#
            </h3>
          </td>
       </tr>
      

      回到 sendmail.php 模板应该被打开和阅读(使用 fopen 和 fread)并且指定的字符串应该在邮件之前使用 str_replace 替换

      sendmail.php

      <?php
      include "class.smtp.php";
      include "class.phpmailer.php";
      session_start();
      
      $email = $_REQUEST['email'] ;
      $message = $_REQUEST['message'] ;
      $_SESSION['message'] = $message;
      
      $template_loc = fopen("../path/to/email_template.php");
      $template_read = fread($template_loc, filesize("../path/to/email_template.php"));
      $template_read = str_replace("#registration_number#", $message, $template_read);
      
      $Body = $template_read;  #or directly use $template_read as body
      ...
      ...
      ?>
      

      建议: 1) 电子邮件模板中的 CSS 应始终是内联的。 2)对于发送电子邮件,第三方 API 提供了更多功能,例如跟踪发送的电子邮件等。

      【讨论】:

        猜你喜欢
        • 2015-12-11
        • 2017-11-30
        • 2019-08-23
        • 2010-12-16
        • 2020-08-16
        • 1970-01-01
        • 2019-07-25
        • 2017-06-30
        • 1970-01-01
        相关资源
        最近更新 更多