6

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

4 Answers 4

10

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;
0
4
ob_start();
require 'emails/orderconfirm.php';
$email = ob_get_clean();

http://php.net/ob_start

0
0

You are close, insert the complete URL

file_get_contents('http://example.com/emails/orderconfirm.php'); 
0

Try going to the full http address eg:

file_get_contents('http://mydomain.com/emails/orderconfirm.php'); 

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.