0

I'm trying to send out a voucher, via the PHP's email() function, where I reference an external .php file using include() as the message contents. Heres the code:

$message = '<?php include ("/fullpath/inc/voucher.php"); ?>';

This isn't working because I'm declaring it as a text string, but if I don't it will just print out the contents and not put it as the content of the variable. Any ideas?

0

7 Answers 7

3

You can use the ob_buffer for that, try this:

<?php
   ob_start();  // start buffer
   include("file.php");  // read in buffer
   $var=ob_get_contents();  // get buffer content
   ob_end_clean();  // delete buffer content
   echo $var;  // use $var
?>

Example from http://www.webmaster-eye.de/include-in-Variable-umleiten.210.artikel.html (German)

Sign up to request clarification or add additional context in comments.

Comments

3

In this post two solutions depending on the circumstances will be described..


voucher.php is returning a value

If voucher.php has a return statement in it you can safely use include to get a hold of this returned value.

See the below example:

voucher.php

<?php

$contents = "hello world!";

...

return $contents;

?>

...

$message = include ("/fullpath/inc/voucher.php");

voucher.php is printing data that I'd like to store in a variable

If voucher.php is printing data that you'd like to store in a variable you can use php's output buffering functions to store away the printed data and then assign it to $message.

ob_start ();
  include ("/fullpath/inc/voucher.php");
  $message = ob_get_contents ();
ob_end   ();

Comments

1
ob_start();
include ("/fullpath/inc/voucher.php");
$message = ob_get_clean();

However output buffering in most cases is considered as bad practice and not recommended to use

Like in your case you better have a function declared in voucher.php that returns the message as a string. So all the code could be as simple as: $message = get_voucher();

1 Comment

@Ryan Brodie: why wouldn't you copy exact message error? These 3 lines are perfectly valid, doubtfully they contain any errors
1

I've encountered the same ploblem, here is the functions that resolves the problem for me. It uses the Tobiask answer

function call_plugin($target_) { // $target_ is the path to the .PHP file
        ob_start();  // start buffer
        include($target_);  // read in buffer
        $get_content = ob_get_contents();  // get buffer content
        ob_end_clean();
        return $get_content;
    }

Comments

0

You want to use the readfile() function instead of include. See: http://en.php.net/manual/en/function.readfile.php

If you want to get the =results= of PHP parsing the voucher.php one way is to make sure that you put voucher.php on a reachable URL, and then call it like:

$message = file_get_contents("http://host.domain.com/blabla/voucher.php");

This would insert the contents/output of the parsed voucher.php into the variable $message.

If you can't reach the voucher.php publically you have to rewrite it to output the string in the end with the return() function, or write a function in voucher.php that outputs the string you want when called after which you include() it and call the function from within this master PHP script.

3 Comments

I tried it myself before posting and it worked just fine. The entire contents of the included file ended up in my variable, and then I could process, print, etc. as I wanted? What happens for you?
Aha, your question is a bit unclear. I see now, based on comments on other replies, that you want the =result= of the voucher.php file and include that one. file_get_contents() should do the trick then or possibly fopen() and to read from it. Good luck!
Updated a little with more suggestions.
0
$message = include '/fullpath/inc/voucher.php';

and withing /fullpath/inc/voucher.php

// create $string;
return $string;

Comments

-1
$message=file_get_contents("/fullpath/inc/voucher.php");

4 Comments

Which is the "message content" as in the original Question.
This has worked, but not as expected. The file content's is put into $message but the PHP within voucher.php no longer works? As in it prints the users name using echo $var but this is now unsuccessful?
As you see from the above comments, I assumed you want to include the text from voucher.php as-is, not its output when interpreted by PHP. You might need eval(file_get_contents("/fullpath/inc/voucher.php")) and inside voucher.php no echo or print, but returning a string.
A better description of what you assumed in your answer would have probably helped clarify

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.