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

Kar*_*k N 1 html php session phpmailer

我正在使用电子邮件模板进行简单的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>
Run Code Online (Sandbox Code Playgroud)

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');
...
...
?>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

更新:我做了同样的...但没有回复......我在missmail.php中遗漏了什么

Tom*_*man 8

您正试图将您的电子邮件放在自己的模板文件中,这很棒,但您需要在其中实际执行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);
...
...
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)