You can store an entire page as if it were opened in a browser, including all dynamic
data. Here’s how…
(Please use caution copying/pasting. Quotes can be a problem with the current plugin)
For this example we are going to build a form and the results using phpMailer in an html format:
form.php
results.php
preview.php
sendmail.php
form.php:
< ?php
//Start a session
session_start();
header("Cache-control: private"); // IE 6 Fix.
?>
<html>
<head>
<title>Form</title>
</head>
<body>
<form method="POST" action="preview.php">
First Name: <input type="text" name="fName" value="<?php print("$_SESSION[fName]"); ?/>" />
<br />
Last Name: <input type="text" name="lName"value="<?php print("$_SESSION[lName]"); ?/>" />
<input type="submit" name="submitButton" value="submit" />
</form>
</body>
</html>
preview.php:
< ?php
//Start a session
session_start();
header("Cache-control: private"); // IE 6 Fix.
?>
<html>
<head>
<title>Your Results</title>
</head>
<body>
< ?php
include("results.php");
?>
</body>
</html>
results.php:
< ?php
//Start a session
session_start();
header("Cache-control: private"); // IE 6 Fix.
?>
<html>
<head>
<title></title>
<style type="text/css">
.firstname{
font: 12px verdana green;
}
.lastname{
font: 14px arial red;
}
</style>
</head>
<body>
< ?php print("<p class=\"firstname\">Hello $fName"); ?>
< ?php print("<p class=\"lastname\">Your last name is $fName"); ?>
</body>
</html>
And now the sendmail.php and gathering the output in a $var:
< ?php
//Start a session
session_start();
header("Cache-control: private"); // IE 6 Fix.
// call the mail class
require("class.phpmailer.php");
$mail = new PHPMailer();
// Set mail headers & connection
$mail->IsSMTP(); // tell phpMailer were using smtp
$mail->Host = "smtp.server.com"; // SMTP server
$mail->SMTPAuth =true; // turn on smtp auth
$mail->Username = "mail@domain.com"; //smtp username
$mail->Password = "password"; //smtp password
$mail->From = "user@domain.com";
$mail->FromName = "$fName";
$mail->AddAddress("fromAddress@domain.com"); // From address
$mail->Subject = "Form Results";
$mail->IsHTML(true); // tell phpMailer we're sending html email
// *** Grab Output Of HTML File as if it were opened in browser ***
ob_start();
include("results.php");
$htmlbody = ob_get_contents(); // adding output to the variable
ob_end_clean();
// *** Set the htmlbody variable in the email ***
$mail->Body = "$htmlbody";
// return results
if(!$mail->Send()){
print("Mail was NOT sent<br />");
print("Mailer Error: " . $mail->ErrorInfo );
} else {
print("Thank you. Your mail was sent.");
?>
The following code is what grabs the output as if it were opened in a browser. The mail form was just an example. I’m sure there are other uses for this.
// *** Grab Output Of HTML File as if it were opened in browser ***
ob_start();
include("results.php");
$htmlbody = ob_get_contents(); // adding output to the variable
ob_end_clean();
Comments? Suggestions? See errors that should be changed? Feel free to comment.
phpMailer, php html email, php email, php get contents, php form data