Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting HTML content from php file , after it has been processed

Tags:

html

php

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

like image 561
Grant Carlisle Avatar asked Dec 19 '12 14:12

Grant Carlisle


1 Answers

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;
like image 128
Touki Avatar answered Sep 25 '22 11:09

Touki