I am generating an HTML email via PHP.
I have an array called $containerArray that may hold 1 or more containers. I am using a foreach loop to retrieve each container from the array and display them in a table inside the HTML email.
I'll try to show the necessary code. If I need to show more, please let me know.
Here is the PHP code that is immediately after a form:
<?php
$containerArray = explode(',', $_POST['containerNumber']);
$trucker_name = mysql_real_escape_string(stripslashes($_POST['trucker_name']));
$trucker_email = mysql_real_escape_string(stripslashes($_POST['trucker_email']));
$remarks = mysql_real_escape_string(stripslashes($_POST['remarks']));
$username = $_SESSION['username'];
The above code is retrieved from the form. There is also a session variable.
Here is the code that takes the above variables and sends them in an HTML email. Please take notice of the foreach loop:
$to = $trucker_email;
$subject = 'Hello';
$headers = "From: Supplier Inc" . "
";
$headers .= "Reply-To: supplierinc.com" . "
";
$headers .= "MIME-Version: 1.0
";
$headers .= "Content-Type: text/html; charset=ISO-8859-1
";
$message = "You have received a message from Supplier Inc.:<br /><br />";
$message .= "Greetings " . stripslashes($_POST['trucker_name']) . "<br />";
$message .= '<html><body>';
$message .= '<table rules="all" style="cellpadding="10">';
$message .= '<tr><th>Containers</th><th>B/L</th><th>Size/Type</th>';
foreach($containerArray as $container){
$message .= '<tr style="background: #D1E5EB; border: 1px solid black;"><td>';
$message .= $container;
$message .= '</td></tr>';
}
$message .= '</table>';
$message .= '<body></html>';
$message .= "Additional Comments: " . stripslashes($_POST['remarks']);
@mail($to, $subject, $message, $headers);
?>
At this point, the email is sucessfully sent, and the containers are displayed in the first cell of the table in the email.
What I need to do now is run a SQL statement to retrieve other information for each container. If you'll notice the table headers, I included B/L and Size/Type, which I need to display in the table (among other forms of data).
I tried to run a query from inside the foreach loop like this:
$preQuery = mysql_query("SELECT * FROM `dispatch_read` WHERE CONTAINER_NUMBER = " . $container . ";");
I then tried to use a while loop inside the foreach loop. This is what the code looked like afterwards:
foreach($containerArray as $container){
$preQuery = mysql_query("SELECT * FROM `dispatch_read` WHERE CONTAINER_NUMBER = " . $container . ";");
while ($preRow = mysql_fetch_assoc($preQuery)){
$message .= '<tr style="background: #D1E5EB; border: 1px solid black;"><td>';
$message .= $container;
$message .= '</td>';
$message .= '<td>';
$message .= $preRow[BOL_NUMBER];
$message .= '</td></tr>';
}
}
But when I do this, when I receive the email, the table no longer displays the containers or anything for that fact.
I know the while loop is wrong. I am thinking that I cannot use the while loop inside the foreach.
I would be grateful for any advice you can give me to fix this problem.