Page 1 of 1

Passing variables to a file which gets "output buffered"?

Posted: Wed Dec 10, 2008 9:07 am
by desperado
Sorry, newbie here.

I have a script that queries a MySql DB, then, for each row, depending on fields such as gender and language, will spit out a customized html email. It works fine as long as I embed the code for the body of the email within the script file, but when I try to include an external file, it goes to hell.

I assume I am doing something incredibly stupid and not seeing it. How do I pass the variables to the external file?

Here is the code. As an example, the "English" email does not print the variables, but the "French" (embedded html) does.

Script file:

Code: Select all

<?php require_once('Connections/mailmerger.php'); // ---------------DB connection informantion ?>
 
<?php // ---------------SQL Recordset Query
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
 
  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
 
  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
 
mysql_select_db($database_mailmerger, $mailmerger);
$query_Recordset1 = "SELECT * FROM `1208b`";
$Recordset1 = mysql_query($query_Recordset1, $mailmerger) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
 
<?php // ---------------parse:include file function for emailmessage
function get_include_contents($filename) {
    if (is_file($filename)) {
        ob_start();
        include $filename;
        $contents = ob_get_contents();
        ob_end_clean();
        return $contents;
    }
    return false;
}
?>
 
<?php do { // ---------------Repeat Loop Start ?>
 
<?php echo $row_Recordset1['lastname'], ' | ', $row_Recordset1['companyname'], ' | ', $row_Recordset1['language'], '<br/>' // ---------------print something on page to show it's working ?>
 
<?php // ---------------define variables for emailmessage
$mailer_gender = $row_Recordset1['gender'];
$mailer_language = $row_Recordset1['language'];
$mailer_firstname = $row_Recordset1['firstname'];
$mailer_lastname = $row_Recordset1['lastname'];
$mailer_companyname = $row_Recordset1['companyname'];
$mailer_address1 = $row_Recordset1['address1'];
$mailer_address2 = $row_Recordset1['address2'];
$mailer_phone = $row_Recordset1['phone'];
$mailer_email = $row_Recordset1['email'];
?>
 
<?php // ---------------define language (ENGLISH)
if ($mailer_language=="en")
    {{
    if ($mailer_gender=="M")
        $salutation="Mr.";
    else 
        $salutation="Mrs.";
    }
// ---------------define recipients
$to  = $mailer_email . ', '; 
$to .= '';
 
// ---------------subject
$subject = 'Test-EN';
 
// ---------------message
 
$emailmessage = get_include_contents('eng_email_body.php');  //::::::::::::::::HELP!::::::::::::::::
    
 
// ---------------HTML mail header
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=ISO-8859-1' . "\r\n";
 
// ---------------Additional headers
$headers .= 'From: ' . "\r\n";
$headers .= 'Bcc: ' . "\r\n";
 
// ---------------Mail it
mail($to, $subject, $emailmessage, $headers);
    }
else        // ---------------define language (FRENCH)
    {{
    if ($mailer_gender=="M")
        $salutation="M.";
    else 
        $salutation="Mme";
    }
        // ---------------multiple recipients
$to  = $mailer_email . ', '; 
$to .= '';
 
// ---------------subject
$subject = 'Test-FR';
 
// ---------------message
 
$emailmessage = "<html>
<head>
<title></title>
</head>
 
<body>
        <p align=\"left\"> $salutation $mailer_firstname $mailer_lastname, $mailer_title <br /> 
        $mailer_companyname <br /> 
        $mailer_address1 <br /> 
        $mailer_address2 <br /></p>
        <p align=\"left\">$salutation $mailer_lastname, </p>
        <p align=\"left\">Ceci est un test.</p>
</body>
</html>";
 
// ---------------To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=ISO-8859-1' . "\r\n";
 
// ---------------Additional headers
$headers .= 'From: ' . "\r\n";
$headers .= 'Bcc: ' . "\r\n";
 
// ---------------Mail it
mail($to, $subject, $emailmessage, $headers);
}
?>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); // ---------------Repeat Loop End ?>
      <?php
mysql_free_result($Recordset1);
?>
Contents of eng_email_body.php:

Code: Select all

<html>
<head>
<title></title>
</head>
 
<body>
      <p align="left">
        <?php echo $salutation,'&nbsp;',$mailer_firstname,'&nbsp;',$mailer_lastname,',&nbsp;',$mailer_title;?><br />
        <?php echo $mailer_companyname;?><br />
        <?php echo $mailer_address1;?><br />
        <?php echo $mailer_address2;?><br /></p>
        <p align="left"> Dear <?php echo $salutation,'&nbsp;',$mailer_lastname;?></p>
        <p align="left">Hello. This is a test</p>
</body>
</html>

Re: Passing variables to a file which gets "output buffered"?

Posted: Wed Dec 10, 2008 10:22 am
by Eran
You include the file inside a function, which is a separate scope than your main application and does not have access to variables you define outside. You need to pass those arguments to the include function - either as individual parameters or as an array/object of parameters.

Re: Passing variables to a file which gets "output buffered"?

Posted: Wed Dec 10, 2008 10:51 am
by desperado
I can't define them as global if I want them to populate each email differently, no?

I think I need someone to spell this out for me. very. slowly. :crazy:

Re: Passing variables to a file which gets "output buffered"?

Posted: Wed Dec 10, 2008 11:40 am
by Eran
Not globals, function parameters. This is your function definition:

Code: Select all

function get_include_contents($filename) {
 //...
}
Currently it accepts only one parameter, $filename (which is the only one available inside the function scope). You can modify it to accept more:

Code: Select all

function get_include_contents($filename,$lastname,$companyname,...) {
Which will also be available to the included file inside the function scope. You can pass those parameters dynamically (without specifying each one):

Code: Select all

function get_include_contents() {
      $params = func_get_args(); //$params is an array of all the parameters passed to the function
      $filename = array_shift($params); //Assuming you passed the filename as the first parameter
      //...
}
And there are plenty more ways to pass parameters inside.

Re: Passing variables to a file which gets "output buffered"?

Posted: Wed Dec 10, 2008 12:39 pm
by desperado
YES!!!!!!!!!!

all my base are belong to you!