简体   繁体   中英

Pass PHP variable from html to php and email template

I am doing simple PHP form validation with email template. I am having simple form called "index.html" and having submit button with form method "post" and action as "sendmail.php". In my sendmail.php, am having smtp-mailer.php with email template called as "email-template.html". How can i pass a variable from index.html to mail-template.php via sendmail.php... Catch my point??? I know about using SESSION. But i don't know where should i call this and how to fetch this??? Any idea...???

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

<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

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.

Rename email-template.html to email-template.phtml. That's a fairly standard extension for php templates. 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.

Include and execute the template file in 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.

You use these variables in template.phtml as you were trying to do before. 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. Injecting a var in the link, using a session or using a cookie. 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:

<?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(); 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 ;)

Your code in sendmail.php is a little bit messed, here the fixed one:

<?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:

<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

For example: email-template.html

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

And then in 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

That way you'll have the html template and you might also have some very basic templating. 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.

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

<?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. 2)For sending emails 3rd party APIs offer more functionalities, such as tracking the sent emails, etc.

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.

I think this is the best practice.

I think it will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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