0

I've got a script that sends out emails. It goes through a for loop, where it finds all subscribers, gets their name and sets a POST var with their name in it.

Next I've got a separate PHP file which contains the markup of the email and content. It has a $_POST['fname'] var which is the users name and needs to be updated dynamically.

The problem is I'm unsure how to grab the contents of my email file (template.php) and add it into the email processing file (bulk_send.php) so that the $_POST['fname'] var can be dynamically updated. Here's the code.

foreach ($emailList as &$value) {
   $getSubscriber = mysql_query("SELECT * FROM subscribers WHERE email = '$value' ");
   while($row = mysql_fetch_assoc($getSubscriber)){
      $_POST['fname'] = $row['fname'];
      $_POST['lname'] = $row['lname'];
   }
   //WHERE IM STUCK
   $bodyText = ***INCLUDE template.php .....***

   //Code that will send out the email with $bodyText as the body of the email

}

The contents of our template.php is something simple - lets just say something like:

 <div><?php echo $_POST['fname')." ".$_POST['lname]; ?></div>

How could I include template.php file appropriately in the Foreach loop above so that the POST vars are updated with each iteration?

Allen S
  • 3,471
  • 4
  • 34
  • 46

1 Answers1

1

You can't quite directly set a variable equal to the contents of another file using include as suggested by Mr.coder in a comment.

You can think of include as copying and pasting the contents of the included file. Based on your template.php, it would be as if your code were:

   while($row = mysql_fetch_assoc($getSubscriber)){
      $_POST['fname'] = $row['fname'];
      $_POST['lname'] = $row['lname'];
   }

   ?><div><?php echo $_POST['fname']." ".$_POST['lname']; ?></div><?php

That would simply dump the div contents out to the browser, which is probably not what you want. Instead, I'd suggest building into your template a function which would create the message text you desire based on parameters fed to the function. Then you would include your template at the top of your page and call the function when needed. It would look more like this:

include('template.php');

foreach ($emailList as &$value) {
   $getSubscriber = mysql_query("SELECT * FROM subscribers WHERE email = '$value' ");
   while($row = mysql_fetch_assoc($getSubscriber)){
      $_POST['fname'] = $row['fname'];
      $_POST['lname'] = $row['lname'];
   }
   //WHERE IM STUCK
   $bodyText = makeBodyText($_POST['fname'], $_POST['lname']);

   //Code that will send out the email with $bodyText as the body of the email

}

Your template.php would then look something like this:

function makeBodyText($fname, $lname)
{
   return '<div>' . $fname .' '. $lname . '</div>';
}

Alternatively, because the included file does operate in the same scope as where it where was called, and because it does support the concept of a return value (thanks to user @kokx for that insight), you could make your template.php like this:

return '<div>' . $_POST['fname'] .' '. $_POST['lname] . '</div>';

And then you could use it as follows:

$bodyText = include 'template.php';

However, that significantly limits the flexibility of template.php. Also, it would potentially output bad information if called directly (from a browser, rather than as an include). So I would not recommend this method.

Now all that aside, it seems odd to me that you are modifying the contents of $_POST. That strikes me (and others) as bad practice.

Community
  • 1
  • 1
Riley Major
  • 1,904
  • 23
  • 36
  • `include` does return a result. If you put a `return` statement at the end of a file, it will return a result, which is the result of the `include`. – kokx May 02 '14 at 16:55
  • Thanks @kokx. I read more about the `include` statement from the [PHP manual]http://www.php.net/manual/en/function.include.php) and it can indeed have a return value. It strikes me as somewhat unusual usage, but it's valuable to know. Thanks. I updated my answer. – Riley Major May 02 '14 at 17:29
  • it is very useful for configuration files. You simply create a PHP file that returns an array with the configuration. – kokx May 02 '14 at 21:01