将 PHP 变量从 html 传递到 php 和电子邮件模板

Pass PHP variable from html to php and email template

我正在使用电子邮件模板进行简单的 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

中的某些内容

尝试这样的事情

$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.

我认为这是最佳做法。

我想它会对你有所帮助。

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

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

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

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

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

现在你使用

echo "$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');
...
...
?>

然后在电子邮件中-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>

您正在尝试将您的电子邮件放入其自己的模板文件中,这很好,但您需要实际执行其中的 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 中使用了这些变量,就像您之前尝试做的那样。 电子邮件-template.phtml:

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

你为什么不尝试在 email-template.html 中放置一些消息占位符,然后用 [= 中的真实消息替换它32=]

例如: 电子邮件-template.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 模板,并且您可能还拥有一些非常基本的模板。您还将摆脱会话内容

对于那些仍然面临这个问题的人,我认为这就是问题所在。

在模板中,应指定要显示的字符串(使用特殊字符)而不是通过 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)并且指定的字符串应该在 mailing

之前使用 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)对于发送电子邮件,第 3 方 API 提供了更多功能,例如跟踪已发送的电子邮件等