views:

745

answers:

3

I want to convert a web pages which is heavily CSS styled is written in PHP to static html so that I can embed it in an email.

I have managed to do this but for achieving this I had to convert the whole page into a string and then assign that string as email body. The layout of the page does not look as good as original as I have not been able embed CSS.

This approach has a few problems like if any changes have to be made to the layout of the page I have to redo the whole process for generating an embeddable page.

I want an easier way which will allow me to keep the original page editable in Dreamweaver visually.

A: 

This is a hard thing to do automatically for several reasons:

  1. If your thought HTML support in browsers was bad, email programs are an order of magnitude worse. Basically you should write HTML like it was 1999, so HTML 3.2/4.0, no CSS;
  2. You have a choice of including images as external links or embed them directly in the email. External links take up less space but many mailers block them as they're used by spammers to tag live addresses (by making each image URL they send unique and thus they can figure out which email was opened). Embedded images use a slightly different reference format;
  3. CSS support should basically be treated as nonexistant. All CSS must be internal;
  4. It is best practice, when sending HTML email, to also send a plain-text version for clients that either don't have HTML support or the user has disabled it (yes this does happen). And there is no good way of turning a complex HTML page into a plain text equivalent. It basically has to be done by hand; and
  5. Email content is different to Webpage content. Webpages typically have search boxes, menus, sidebars, headers, footers and so on. These are all things you're not interested in emailing. You're only interested in the content of the page. So blogs work quite well for this because the content of a blog post can easily be extracted. It's harder with arbitrary pages.

So there are various ways you could do this like using cURL to get the Webpage, using output buffering to capture the page by using require/include, etc but all these methods suffer from one or more of the above problems. I've generally found the only way to do HTML email is to hand-roll it.

cletus
That is bad news for me. I have already done the hard work by converting everything by hand but our client keeps on asking for a few layout changes every few days.
Yogi Yang 007
+5  A: 

You can use output buffers. If you have an html page, such as:

   <html>
      <head>
         <title>Blah</title>
      </head>
      <body>
        Some text here
      </body>
   </html>

Then if you put, at the top of the html file:

<?php ob_start(); ?>

And right at the bottom, after the last tag, put:

<?php 
   $string = ob_get_contents(); 

   //do whatever you need to do to the html, save it to a seperate file, email it, etc

   ob_flush();
?>

What this basically means is that the $string variable will end up with the entire static html of the page, after it has been dynamically generated. You can then use that string in an email. Although really, html pages don't work exactly the same in emails, so you may want to rethink the approach.

Kazar
This did the trick. Thanks for this tip.You have saved me hours of untold brain dead work.
Yogi Yang 007
A: 

I am not sure if this would help or not but why don't you use Iframes?

good luck

ahmed
Use iframes to do what?
Charles
to embed the PHP page in the email. would this works?
ahmed