Email the contents of a dynamic webpage generated by php

I have a project in which I generate a report from a database and print it out into a table on the screen. I want to make it possible for the user to email the report that was just generated to someone else. I thought I could re-create the table in the $message of the email. Unfortunately I haven’t been able to get the variables/arrays to output the values. I have attached a screenshot of the report to show how I want it to look in the email.
Here is a sample of what I am trying to do:


<?php
if (isset($_POST['sendmail'])){
								
								
$message = "<h5>Start Date: " . date('F j, Y',strtotime($time1)) . " || End Date: " . date('F j, Y',strtotime($time2)) . "</h5>"
. "<table width='950' cellpadding='2' cellspacing='1' border='1' class='table'>"
. "<tr>"
. "<td class='bold'>Wire Gauge</td>"
. "<td colspan='3' class='bold'>Beam Pull</td>"
. "<td colspan='3' class='bold'>Fracture Point</td>"
. "</tr>"
. "<tr>
  <td>&nbsp;</td>
  <td class='bold'>MIN</td>
  <td class='bold'>MAX</td>
  <td class='bold'>AVG</td>
  <td class='bold'>Anchor Bend</td>
  <td class='bold'>Break Back</td>
  <td class='bold'>Weld</td>
</tr>
<tr>
  <td class='bold'>2 Gauge Loop</td>
  <td>" . $beamPullmin['2.0'] . "</td>"
  . "<td>" . echo $beamPullmax['2.0'] . "</td>"
  . "<td>" . echo $beamPullavg['2.0'] . "</td>"
  . "<td></td>
  <td></td>
  <td></td>
</tr>
</table>";

	$to = "melinda@mccawphotographics.com";
	
	$headers .= "X-Mailer: PHP4\
" ; // mailer
	$headers .= "X-Priority: 3\
" ;  // priority
	$headers .= "Return-Path:  $to\
";
	$headers .= "Origin: ".$_SERVER['REMOTE_ADDR']."\
";
	$headers .= "Content-Type: text/html; charset=us-ascii\
";
	
	
mail("melinda@mccawphotographics.com", "QC Reports", $message,$headers);
}
    ?>

Am I even close to being on the right track to solving this? If so, how do I get it to output the values? Is there an easier or better solution?

Unfortunately I haven’t been able to get the variables/arrays to output the values.

I guess you would have to perform the same database query that you performed originally, but that part of the script is missing?

Is the $beamPullxxx variables the stuff you are grabbing from your database

Yes, it is. Do you need to see the sql query or code for the report?

and where do you populate this variable within this part of the script with actual data? you’ve got to populate it the same way you’ve populated the original report.

if you don’t know how, then showing us the part of the script that deals with the report might be best - it may also be useful if you could explain us your overall setting and how the script is designed so far (both times with regards to the report output).

Yes.
You will need to preform the query within there. You are basically trying to tell php to give you variables that only exist inside a loop outside a loop. So beamPullxxx does not exist in PHP’s eyes when you called them there.

I am still a beginner at php and mysql so I am not sure I totally understand what you are saying. Are you saying I need to run the same query within the $message variable for sending the email?

Let me try to explain the project better. I have a form with 2 drop down menus. The first is start date and the 2nd is End date. The user selects the start and end date for the report they want to run. Next they click the “Get Report” Button and the report is echoed on the page below. What I want to do is add the functionality to allow the user to email the report that is now on the screen to their superior.

Here is the code for my query:


$conn = mysql_connect("localhost","user","password") or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db("database", $conn) or trigger_error("SQL", E_USER_ERROR);
																
//query of min, max and avg for wire gauge 2, 3, 4 and 4.5 with endType Loop
$sql="SELECT wireGauge, MIN(beamPull) AS `min`, MAX(beamPull)AS `max`, AVG(beamPull) AS `avg`
FROM welded_wire_ties
WHERE time between '$time1' and '$time2'
AND endType = 'Loop'
GROUP BY wireGauge";
								
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
								
while($row = mysql_fetch_array($result)) {
$beamPullmin[$row['wireGauge']] = $row['min'];
$beamPullmax[$row['wireGauge']] = $row['max'];
$beamPullavg[$row['wireGauge']] = $row['avg'];
}

Here is the code to output the report (I am just showing part of the table since it is large and redundant in many parts):


echo ("&lt;h5&gt;Start Date: " . date('F j, Y',strtotime($time1)) . " || End Date: " . date('F j, Y',strtotime($time2)) . "&lt;/h5&gt;");
echo ("&lt;table width='950' cellpadding='2' cellspacing='1' border='1' class='table'&gt;");
        echo ("&lt;tr&gt;");
                echo ("&lt;td&gt;");
                echo $beamPullmin['2.0'];
                echo ("&lt;/td&gt;");
                echo ("&lt;td&gt;");
                echo $beamPullmax['2.0'];
                echo ("&lt;/td&gt;");
                echo ("&lt;td&gt;");
                echo $beamPullavg['2.0'];
                echo ("&lt;/td&gt;");
        echo ("&lt;/tr&gt;");
        echo ("&lt;tr&gt;");
                echo ("&lt;td&gt;");
                echo $beamPullmin['3.0'];
                echo ("&lt;/td&gt;");
                echo ("&lt;td&gt;");
                echo $beamPullmax['3.0'];
                echo ("&lt;/td&gt;");
                echo ("&lt;td&gt;");
                echo $beamPullavg['3.0'];
                echo ("&lt;/td&gt;");
        echo ("&lt;/tr&gt;");
ech ("&lt;/table&gt;");

Thanks!

You can redirect the output into a normal variable.

ob_start();
echo "<table>";
echo "<....>";
echo "<....>";
echo "</table>";
$output=ob_get_contents();
ob_end_clean();

echo $output;

and then email if its needed.

So would I wrap this code around the table that outputs the data and then put the $output variable in the message of the email such as $message = “$output”;?

I actually tried that and it didn’t work. Any thoughts on what I did wrong?

Test this code on a short example, and see the results.

You should see the examples and better explanations here: ob_start()
And: ob_get_contents()

I actually have the echo $output working. It echoes the table and all data on the page. When I try to put the variable of $output into the message part of my email and test it I get a blank email without the table and the data. Can you see anything wrong with how my mailer is working? Here is my code:


if (isset($_POST['sendmail'])){
								
$message = "$output";
$to = "email@address.com";

$headers .= "X-Mailer: PHP4\
" ; // mailer
$headers .= "X-Priority: 3\
" ;  // priority
$headers .= "Return-Path:  $to\
";
$headers .= "Origin: ".$_SERVER['REMOTE_ADDR']."\
";
$headers .= "Content-Type: text/html; charset=us-ascii\
";
	
mail("email@address.com", "QC Reports for $time1 to $time2", $message,$headers);
	
}

make sure the $output has the table data:

The table doesn’t appear:

echo htmlspecialchars($output);

And add its values to the $message, like:

$message = $output;  // qoutes only been used if you're creating strings

Just figured out that if I take my mail out of my isset statement that it works perfectly. Any idea why it won’t work inside the isset statement?

test your $_POST array , and check the contents you get:

print "<pre>";
print_r($_POST);
print "</pre>";

especally your submit button is in with the index name: sendmail ?

Any idea why it won’t work inside the isset statement?

Variable is not set

I tested my $_POST array and got the following:
Array
(
[sendmail] => Email Report
)

When you say “Variable is not set” are you talking about $message?

To explain what happens: I have a form with 2 drop downs “time1” and “time2” (start date and end date) Once the user select the start and end date for the report they click “Get Report” (named “send_data1”). If there are no validation errors the report is printed out as well as the “Email Reports” button (named “sendmail”). The problem I am having is getting the email to contain the exact table that was on the screen.
My current code looks like this:


if (isset($_POST['send_data1'])){
								
$time1=$_POST['time1'];
$time2=$_POST['time2'];
$error=NULL;
								
// validation of time1
if ($_POST['time1'] == 'null') {
$time1 = FALSE;
$error = "&lt;span class='red&gt;Please select a Start Date.&lt;/span&gt;";
}
								
// validation of time2
else if ($_POST['time2'] == 'null') {
$time2 = FALSE;
$error = "&lt;span class='red&gt;Please select an End Date.&lt;/span&gt;";
}
								
if(is_null($error)){
								
$wireGauge=$_POST['wireGauge'];
$time1=$_POST['time1'];
$time2=$_POST['time2'];

$conn = mysql_connect("localhost","user","password") or trigger_error("SQL", E_USER_ERROR);

$db = mysql_select_db("database", $conn) or trigger_error("SQL", E_USER_ERROR);



//query of min, max and avg for wire gauge 2, 3, 4 and 4.5 with endType Loop

$sql="SELECT wireGauge, MIN(beamPull) AS `min`, MAX(beamPull)AS `max`, AVG(beamPull) AS `avg`
FROM welded_wire_ties
WHERE time between '$time1' and '$time2'
AND endType = 'Loop'
GROUP BY wireGauge";

$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
while($row = mysql_fetch_array($result)) {
$beamPullmin[$row['wireGauge']] = $row['min'];
$beamPullmax[$row['wireGauge']] = $row['max'];
$beamPullavg[$row['wireGauge']] = $row['avg'];
}

ob_start();

echo ("&lt;h5&gt;Start Date: " . date('F j, Y',strtotime($time1)) . " || End Date: " . date('F j, Y',strtotime($time2)) . "&lt;/h5&gt;");
echo ("&lt;table width='950' cellpadding='2' cellspacing='1' border='1' class='table'&gt;");
        echo ("&lt;tr&gt;");
                echo ("&lt;td&gt;");
                echo $beamPullmin['2.0'];
                echo ("&lt;/td&gt;");
                echo ("&lt;td&gt;");
                echo $beamPullmax['2.0'];
                echo ("&lt;/td&gt;");
                echo ("&lt;td&gt;");
                echo $beamPullavg['2.0'];
                echo ("&lt;/td&gt;");
        echo ("&lt;/tr&gt;");
        echo ("&lt;tr&gt;");
                echo ("&lt;td&gt;");
                echo $beamPullmin['3.0'];
                echo ("&lt;/td&gt;");
                echo ("&lt;td&gt;");
                echo $beamPullmax['3.0'];
                echo ("&lt;/td&gt;");
                echo ("&lt;td&gt;");
                echo $beamPullavg['3.0'];
                echo ("&lt;/td&gt;");
        echo ("&lt;/tr&gt;");
echo ("&lt;/table&gt;");
echo ("&lt;form action=\\"reports-email2.php\\" method=\\"post\\" name=\\"Email-report\\" id=\\"Email-report\\"&gt;");
echo ("&lt;table width=\\"350\\" border=\\"0\\" cellspacing=\\"1\\" cellpadding=\\"2\\"&gt;");
	echo ("&lt;tr&gt;");
		echo ("&lt;td width=\\"100\\"&gt;&lt;input type=\\"submit\\" name=\\"send_report\\" value=\\"Email Report\\"&gt;&lt;/td&gt;");
	echo ("&lt;/tr&gt;");
echo ("&lt;/table&gt;");
echo ("&lt;/form&gt;");

$output=ob_get_contents();
ob_end_clean();
									
echo $output;
     }
}

if (isset($_POST['sendmail'])){

$message = $output;
$to = "email@address.com";

$headers .= "X-Mailer: PHP4\
" ; // mailer
$headers .= "X-Priority: 3\
" ;  // priority
$headers .= "Return-Path:  $to\
";
$headers .= "Origin: ".$_SERVER['REMOTE_ADDR']."\
";
$headers .= "Content-Type: text/html; charset=iso-8859-1\
";

mail("email@address.com", "QC Reports for $time1 to $time2", $message,$headers);
}


You have two submit buttons, and have two cases.

in a case you press:
send_data1 the dates will be countered (maybe)

Then you have another form and another submit button: sendmail

These two forms don’t have a union. And you might don’t save the entered date1 and date2 back to the form => the form values then erased on the next submit when you press email.

If i will have a problem with my projects, i would post the relevant codes to the others who would like to help , or i give a test link where we can see how your form outputted.

I don’t like the “minimal info” games.

When you say “Variable is not set” are you talking about $message?

doesn’t matter.
The isset() function checks if particular variable set. if it returns false, that just means that passed variable is not set.

If you would like to see the form in action here is a link to it: http://www.mccawphotographics.com/qc/reports-email4.php I provided the code that I thought was relevant in my last post. I have also attached the entire php file.

I agree that the two forms don’t have a union. But being very new to php I don’t yet understand how make the two forms have a union. Any help in figuring this out would be greatly appreciated.

You should sate the $_POST[“time1”] and $_POST[“time2”] into your "send email "form:

<?php
$time1=isset($_POST["time1"]) ? htmlspecialchars($_POST["time1"]) : '';
$time2=isset($_POST["time2"]) ? htmlspecialchars($_POST["time2"]) : '';
?>
<input type="hidden" name="time1" value="<?php echo $time1; ?>">
<input type="hidden" name="time2" value="<?php echo $time2; ?>">
<input type="hidden" name="send_mail" value="1">
<input type="submit" name="send_data1" value="Send mail">
</form>

The form action is ok.

So what would it be do:
if you select a time1 and time2 this selects the report, and you will see the Email report button. If you press you will repeat the previous post with the exact data, but in a hidden field you tell the processor program to send the email too.