Email HTML page contents using PHP

May 17, 2006 {2 years, 5 months ago.} (posted by Melanie)

Earlier post explained on how to send a plain email using PHP. This time, we are discussing about sending page contents to email. This technique is based on output buffering. Basically, it would first create read a html file and store in buffer using ob_start(). Next step is to include desire HTML page using include function and then store the contents of that HTML page using ob_get_contents(). Finally use mail() function to send email.

While output buffering is turned on no output is sent from then script. The output is stored in an internal buffer.

< ?
ob_start();
include_once("samplepage.html");
$buffer = ob_get_contents();
ob_end_clean();
 
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.SENDER_NAME.' <'.SENDER_EMAIL.'> ' . "\r\n";
 
$to_email = 'email@example.net';
$subject = 'Your Desired Email Subject';
 
mail($to_email, $subject, $buffer, $headers);
?>

Download Sample

  1. Download (rar file)
  2. Download (zip file)


Recommended Reading


Post a comment or question



  1. (required)(required)

  2. (required)(required)(will not be published)


Search on site