简体   繁体   English

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

[英]Pass PHP variable from html to php and email template

I am doing simple PHP form validation with email template. 我正在使用电子邮件模板进行简单的PHP表单验证。 I am having simple form called "index.html" and having submit button with form method "post" and action as "sendmail.php". 我有一个简单的表单,称为“ index.html”,并具有表单方法“ post”和操作为“ sendmail.php”的提交按钮。 In my sendmail.php, am having smtp-mailer.php with email template called as "email-template.html". 在我的sendmail.php中,正在将smtp-mailer.php的电子邮件模板称为“ email-template.html”。 How can i pass a variable from index.html to mail-template.php via sendmail.php... Catch my point??? 如何通过sendmail.php将变量从index.html传递到mail-template.php ... I know about using SESSION. 我知道使用SESSION。 But i don't know where should i call this and how to fetch this??? 但是我不知道该在哪里打电话以及如何获取呢??? Any idea...??? 任何想法...???

index.html 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 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 (send it to email not in browser) 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>

Updates : I did the same... But no response... Did i miss something in sendmail.php 更新:我做了同样的事情...但是没有回应...我是否错过了sendmail.php中的某些内容

You're trying to put your email in its own template file, which is great, but you need to actually execute the PHP code within it to fill out the variables. 您正在尝试将电子邮件放入其自己的模板文件中,这很好,但是您需要实际执行其中的PHP代码以填写变量。

Rename email-template.html to email-template.phtml. 将email-template.html重命名为email-template.phtml。 That's a fairly standard extension for php templates. 这是php模板的相当标准的扩展。 It's worth noting that using $_REQUEST is not recommended in PHP as it takes both POST and GET parameters, and you'd decided that the user needs to POST the form. 值得注意的是,不建议在PHP中使用$ _REQUEST,因为它同时具有POST和GET参数,并且您已确定用户需要POST表单。

Include and execute the template file in sendmail.php : 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 is including the template file, exposing the two variables $email and $message to that template file and then returning the output of including the template. render_email包含模板文件,将两个变量$ email和$ message暴露给该模板文件,然后返回包含模板的输出。

You use these variables in template.phtml as you were trying to do before. 您可以像以前一样在template.phtml中使用这些变量。 email-template.phtml: email-template.phtml:

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

You can do that in 3 ways. 您可以通过3种方式来做到这一点。 Injecting a var in the link, using a session or using a cookie. 使用会话或Cookie在链接中注入var。 Personally I suggest sessions because users can't modify them and they expire when the user close his browser. 我个人建议使用会话,因为用户无法修改会话,会话会在用户关闭浏览器时过期。

You need to rename index.html to index.php so you can insert php code into this file, then you'll need to write something like this: 您需要将index.html重命名为index.php,以便可以将php代码插入此文件,然后需要编写如下内容:

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

Doing so you setted the session, now each time you need to call the session use this code: 为此,您设置了会话,现在每次需要调用会话时,请使用以下代码:

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

Now you use 现在你用

echo "$var";

Where you wanna print the var 您想在哪里打印变量

Don't forget session_start(); 不要忘记session_start(); or you won't be able to call or set sessions 否则您将无法通话或设置会话

If you wanna do this using a cookie or injecting the var into the url feel free to ask and I'll explain you how ;) 如果您想使用Cookie或将var注入url来进行此操作,请随时提出,我将为您解释如何;)

Your code in sendmail.php is a little bit messed, here the fixed one: 您在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');
...
...
?>

Then in email-template.php you'll use this code: 然后,在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>

Why don't you try to put some message placeholder in email-template.html and later replace it with the real message in sendmail.php 为什么不尝试在email-template.html中放置一些消息占位符, 然后sendmail.php中将其替换为真实消息?

For example: email-template.html 例如: email-template.html

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

And then in sendmail.php 然后在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 http://php.net/str_replace

That way you'll have the html template and you might also have some very basic templating. 这样,您将拥有html模板,并且还可能具有一些非常基本的模板。 You'll also get rid of that session stuff 您还将摆脱该会话内容

For those who are still facing this problem, I think this is what it's all about. 对于那些仍然面临这个问题的人,我认为这就是全部。

In the template the string(s) to be displayed should be specified (with special characters) instead of printing by php. 在模板中,应指定要显示的字符串(带有特殊字符),而不是用php打印。

email_template.php email_template.php

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

Back in sendmail.php the template should be opened and read (using fopen and fread) and the specified string should be replaced using str_replace before mailing 返回sendmail.php ,应该打开并读取模板(使用fopen和fread),并且在发送邮件之前,应使用str_replace替换指定的字符串。

sendmail.php 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
...
...
?>

Suggestions: 1) CSS in email templates should always be inline. 建议: 1)电子邮件模板中的CSS应该始终是内联的。 2)For sending emails 3rd party APIs offer more functionalities, such as tracking the sent emails, etc. 2)对于发送电子邮件,第三方API提供了更多功能,例如跟踪发送的电子邮件等。

Try something like this 试试这个

$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);

So email_template.html will have %NAME% inside of it, str_ireplace will replace it by John Doe and you will send the variable $message as the email html. 因此email_template.html中将包含%NAME%str_ireplace将其替换为John Doe ,您将把变量$message为电子邮件html。

I think this is the best practice. 我认为这是最佳做法。

I think it will help you. 我认为它将为您提供帮助。

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

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