Reputation: 173
I want to get the contents of a php file in HTML. I am using it as a HTML email template but it needs to be populated with dynamic data from my database.
I tried
file_get_contents('/emails/orderconfirm.php');
But all that does is return the php code before it is processed by the server into HTML.
I'm guessing it's really easy? any ideas would be much appreciated.
Thanks
Upvotes: 6
Views: 2819
Reputation: 7525
In order to execute the specific code in your emails/orderconfirm.php
, you need to include its content.
Included content are going to be compiled and rendered.
However, You need to get the content as long as you echo
That way, you need to use ob_start() library.
Here's an example of usage :
ob_start(); //Init the output buffering
include('emails/orderconfirm.php'); //Include (and compiles) the given file
$emailContent = ob_get_clean(); //Get the buffer and erase it
The compiled content is now stored in your $emailContent
variable and you can use it the way you want.
$email->body = $emailContent;
Upvotes: 10
Reputation: 1903
Try going to the full http address eg:
file_get_contents('http://mydomain.com/emails/orderconfirm.php');
Upvotes: 0
Reputation: 522099
ob_start();
require 'emails/orderconfirm.php';
$email = ob_get_clean();
Upvotes: 4
Reputation: 1149
You are close, insert the complete URL
file_get_contents('http://example.com/emails/orderconfirm.php');
Upvotes: 0