1

I have a newsletter template in one file and a mail script in another file which basically pulls email addresses from a database and loops through each one sending the email to them.

I am using this code to get the contents of the newsletter template:

$content = file_get_contents('attach/T_newsletter.php');

What I need to do now is send PHP variables along with this eg:

T_newsletter.php?test=hello

and then be able to pull them from the URL in the template and adjust the content accordingly.

Could someone explain how I would do this please?

Thanks for any help

2
  • can you give us a peek into the newsletter.php? Commented Dec 19, 2011 at 12:25
  • Are you talking about a GET or POST? those are the methods of passing data. Commented Dec 19, 2011 at 12:25

4 Answers 4

3

file_get_contents('attach/T_newsletter.php');

Whoa! Stop right there. If this is not PHP source code then it should not have a .php extension - if it is PHP source code then you shouldn't be sending it out on a mailing list.

If you want to execute the code inside the file, then this is not the way to do it. One method would be to access the file via HTTP, e.g.

file_get_contents('http://localhost/attach/T_newsletter.php');

While this simplifies passing parameters to the script, it is still a rather messy solution.

Another really horrible way to solve the problem would be....

ob_start();
include('/attach/T_newsletter.php');
$content=ob_get_contents();
ob_end_flush();

The right way to implement this would be

1) as function (or class) in the included file:

 require_once ('/attach/T_newsletter.php');
 $content=generate_newsletter($params);

2) using the file as a DATA file template:

 $template=file_get_contents('/attach/T_newsletter.data');
 $content=preg_replace($template_vars, $template_values, $template);
Sign up to request clarification or add additional context in comments.

Comments

2

If you want to do that, you will need to call that file "T_newsletter.php" as part of a URL:

$content = file_get_contents("http://yourdomain.com/T_newsletter.php?test=hello");

and inside T_newsletter.php:

<?php
  $v = $_GET['test']; // this will be 'hello'

  if($v == 'hello')
  {
    ...
  }
  else
  {
    ...
  }
?>

The other option would be to use a template engine like Twig or Smarty to parse the files directly if you do not wish to make an HTTP request.

3 Comments

thanks thats brilliant...I was trying to do it like attach/T_newsletter.php?test=hello and it was returning nothing.
Does your solution read the file from file system or performs it a download from "yourdomain"?
@strauberry it makes an HTTP request to the specified domain.
0
$response=file_get_contents("attach/T_newsletter.php?test=hello");

Comments

0

The cleanest and most flexible solution would be to use a template engine (like smarty) and instead of outputting the parsed content reading it into a variable... you can use variables, loops etc. in your template file (see here) and read the result into a variable with fetch (see here).

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.