繁体   English   中英

将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中的某些内容

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

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

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

您需要将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');
...
...
?>

然后,在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>

为什么不尝试在email-template.html中放置一些消息占位符, 然后sendmail.php中将其替换为真实消息?

例如: email-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),并且在发送邮件之前,应使用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提供了更多功能,例如跟踪发送的电子邮件等。

试试这个

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

我认为这是最佳做法。

我认为它将为您提供帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM