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.
Technorati Tags: phpMailer, php html email, php email, php get contents, php form data


May 27th, 2008 at 12:18 am
I want to do coding in php to send a mail to a client in such way that if some one send mail in html format and client does’nt support for that then it automatically get converted in to text and similerly if mail is in text and client does’nt support for text then it get converted in to html. if any one has done this send code on my mail.
May 27th, 2008 at 7:23 am
Arvind,
The only way to really do that is to have your users choose an option in your site’s account. Have them choose text or HTML preference when they sign up or agree to receive the emails. This is assuming you have a mailing sign up form, which is required, other wise it goes against the CAN-SPAM act.
A script is executed at run time. So once the mail has been sent, it can’t really see what your mail clients preferences are. Make sense?