2

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

10
  • You cannot write PHP code inside HTML file. Commented Jun 17, 2015 at 12:37
  • 1
    Unless you've instructed Apache to treat .html files as PHP, you will need to renamed email-template.html to email-template.php - that file won't get parsed properly Commented Jun 17, 2015 at 12:37
  • @MihirBhatt "You cannot write PHP code inside HTML file." - not entirely true. See my comment above ^ Commented Jun 17, 2015 at 12:37
  • see my above updates :( Commented Jun 17, 2015 at 12:42
  • Add error reporting to the top of your file(s) right after your opening PHP tag for example <?php error_reporting(E_ALL); ini_set('display_errors', 1); then the rest of your code, to see if it yields anything. Commented Jun 17, 2015 at 12:42

5 Answers 5

12

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>
Sign up to request clarification or add additional context in comments.

Comments

2

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>

7 Comments

It was different when I posted this, hmmm... The below answer should work too, try it
Add error_reporting(E_ALL); at the top of your code, what happens?
when i paste along with ini_set('display_errors', 1); in sendemail.php no error displays
Try to use, intead of include, require_once. Also, in my own scripts I would use require_once instead even of file_get_contents... Try to do so but remove session_start(); in email-template.php
Hope you know, my email-tempate.php is a email template which is received in email not as php file which is run in browser. So could you give me a solution
|
2

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

Comments

2

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.

Comments

1

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('[email protected]', '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.

2 Comments

Hope you know, my email-tempate.php is a email template which is received in email not as php file which is run in browser. So could you give me a solution
That's the weird thing about it, nobody usually uses a .php file as a html template. What we do is use a .html file with some wildcards (e.g. %NAME%), and then we replace this wildcards with varibles running an outside php script, as i explained in my answer. I don't see a point on storing all the template in a session variable. Sorry!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.