4

Im trying to email an order with PHPMailer to the customer when this order is received. I tried doing that this way:

        $email_page = file_get_contents("email_order_html.php?order=$order_id");

I want to get the contents of this page as string so I can send this with PHPMailer but this function wont execute the page because of the variable $order_id in it, how can I fix this?

5
  • 1
    file_get_contents does support URIs - php.net/manual/en/function.file-get-contents.php Commented Jul 15, 2011 at 16:26
  • @holodoc true, but i'd say the issue is likely that the OP doesnt know the difference between calling it with a URI and a local file. Commented Jul 15, 2011 at 16:28
  • In fact I think he does :) He is simply using a dynamic page to generate the required content based on the order_id which is sent with the request. Perhaps a bit odd way to do it but certainly not the worst I've seen :) Commented Jul 15, 2011 at 16:32
  • 1
    @holodoc just that file_get_content wont generate a page here because its fetching the file from the filesystem and that wont execute it all. There is no request. Commented Jul 15, 2011 at 16:33
  • Hm... Valid point... Argument doesn't start with a protocol identifier :( Commented Jul 15, 2011 at 16:39

6 Answers 6

5

You can only add Query Params when using file_get_contents with an Url Aware Stream Wrapper, e.g. it would work for http://localhost/yourfile.php?foo=bar. Doing so would issue an HTTP Get Request to a webserver at localhost with the specified query params. The request would be processed and the result of the request would be returned.

When using file_get_contents with a filename only, there wont be any HTTP Requests. The call will go directly to your file system. Your file system is not a webserver. PHP will only read the file contents. It wont execute the PHP script and will only return the text in it.

You should include the file and call whatever the script does manually. If the script depends on the order argument, set it via $_GET['order'] = $orderId before including the file.

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

Comments

4

One of the certainly better ways to do it would be to use output buffering combined with simply including the content creating script.

// $order_id is certainly available in place where file_get_contents has been used...
ob_start();
require 'email_order_html.php';
$email_page = ob_get_clean();

Comments

4

email_template.php

<body>
<p>{foo}</p>
<p>{bar}</p>
</body>

sendmail.php

../

$mail = new PHPMailer(); 

//get the file:
$body = file_get_contents('email_template.php');
$body = eregi_replace("[\]",'',$body);

//setup vars to replace
$vars = array('{foo}','{bar}');
$values = array($foor,$bar);

//replace vars
$body = str_replace($vars,$values,$body);

//add the html tot the body
$mail->MsgHTML($body);

/...

Hope it helps someone ;)

Comments

2
use require("email_order_html.php");

and $order_id will available in your file

2 Comments

yes and no. It will be available but depending on how the script looks like, it wont help the OP. Given that he's trying to pass it as a URI param, the script likely requires it to be in $_GET.
But $order_id is surely available in place where he used to call file_get_contents.
1

As Gordon said, file_get_contents() reads files from the filesystem - text or binary files, that is, not the result of the execution of those files.

You can use Curl (docs) to do that, in case you'll want to move the script to a separate server. At the moment, simply including the file and passing the arguments directly to the function you want is a more sensitive approach.

Comments

0
$email_page = file_get_contents("email_order_html.php?order=".$order_id);

file_get_contents can read content in url too, at least, in 2021, yes it can.

Comments

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.