Importing a variable as a file

I’m writing an app for my own use. In essesence it extracts some data from MySQL tables and sends an individual HTML e-mail to each of our members.

The HTML is created as a variable to insert into the mailing script (SwiftMail), and it all works fine. The HTML variable contains other variables and a DATE function, introduced via concatenation as in this excerpt:

$htmlmsg = "<html>
<body>
	<div id='container'>
		<h2>Your Subscription Renewal for 2012</h2>

		<p class='floatr'>" . date('jS F Y') . "</p>

		<p>Dear " . $bus_info['first_name'] . ",

		<p>This year instead of asking you to complete and return a Renewal Form we are sending you a note of your renewal subscription.</p>

	</div><!-- container -->
</body>
</html>";

Both the date and the inserted name work perfectly.
However, the variable is about 150 lines long which is cumbersome. I’d like to read it in from a file but when I do this the ‘internal’ variables and DATE function no longer work. I have tried:

$htmlfile = "billtemplate.php";
$htmlmsg = file_get_contents($htmlfile);
echo $htmlmsg;
exit();

The text is imported OK, but when the HMTL is echoed the internal variables are just shown literally. (e.g. Dear " . $bus_info[‘first_name’] . ", ).
The variables for insertion are created before the HTML variable.
I have tried a few alternatives, such as putting the internal variables in PHP tags, but nothing’s worked yet.
Is there a way to do this, or am I just going to have to live with an unwieldy variable ?


return "<html> 
<body> 
    <div id='container'> 
        <h2>Your Subscription Renewal for 2012</h2> 

        <p class='floatr'>" . date('jS F Y') . "</p> 

        <p>Dear " . $bus_info['first_name'] . ", 

        <p>This year instead of asking you to complete and return a Renewal Form we are sending you a note of your renewal subscription.</p> 

    </div><!-- container --> 
</body> 
</html>";


$htmlmsg = include 'billtemplate.php';

That should do what I think you’re trying to do. :slight_smile:

Use require:


<?php
$htmlfile = "billtemplate.php"; 
require $htmlfile; 
echo $htmlmsg; 
exit; 

Thank you Anthony and Ernie.

Include (and presumably ‘require’) works, but I have to enclose the ‘internal’ variables in the ‘billtemplate.php’ file within PHP tags (and without concatenation). I’ll probably end up using ‘require’ because the app obviously can’t work without it.

I’d probably have got there in the end, but you’ve saved me a lot of time. I’ve used include frequently, but never to create a variable before. Thank you.

Later: I spoke too soon, of course. The file does indeed get included, and it is displayed perfectly on screen (because it’s an HTML file). At that point I thought we’d cracked it. But the actual value of the ‘$htmlmsg’ variable isn’t the text contained within the file (which is what I’m after) but just ‘1’, presumably indicating boolean true (successful inclusion).

//	Assemble e-mail: Import HTML template
		$htmlmsg = include "billtemplate.php";
		echo '<br />' . $htmlmsg;
		exit();

The code above displays the included ‘billtemplate.php’ file, followed by ‘1’.

I need ‘$htmlmsg’ to be the actual contents of the file (with the ‘internal’ variables evaluated, see previous posts) to pass to the e-mailing script. At this moment I’m e-mailing myself the exciting message: ‘1’.

Reusing this:

Then billtemplate.php should contain this:


<?php

$htmlmsg = "I am not a number, I'm a man.... (said number 6)";

output buffering is what you want…


ob_start();
include 'billtemplate.php';
$htmlmsg = ob_get_clean();

echo $htmlmsg;

Thank you both.

Cups: Yes, I got that approach to work too, but it doesn’t quite give me what I wanted. I want the entire file to be included as the variable. billtemplate.php is an HTML file, from doctype to closing tag, which I can display in my browser on its own (for editing and checking purposes).

Cholmon: I think output buffering has finally cracked it (a bit more testing, this time, before I declare victory). The three ‘internal’ variables are inside PHP tags, and they are getting completed with the customised information for every pass through the loop (per e-mail). I can view billtemplate.php in my browser entirely separately (no variables inserted, of course) which achieves my other aim. Thank you.