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);
?>Code: Select all
<html>
<head>
<title></title>
</head>
<body>
<p align="left">
<?php echo $salutation,' ',$mailer_firstname,' ',$mailer_lastname,', ',$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,' ',$mailer_lastname;?></p>
<p align="left">Hello. This is a test</p>
</body>
</html>