NOTE: This is outdated information that applies only to Tapestry 4.

HTML Email

If you would like to use a Tapestry page to render an html email, you can use the following procedures, which were most recently posted to the tapestry-users mailing list by Dave Kallstrom. These are directly copied from his emails.

For Tapestry 4.0.x

BaseHTMLMessagePage sendPage = (BaseHTMLMessagePage) getHtmlPage(inner);
ByteArrayOutputStream out = new ByteArrayOutputStream();
IMarkupWriter writer = new MarkupWriterImpl("text/html", new
PrintWriter(out), new AsciiMarkupFilter());
inner.activate(sendPage);
inner.renderPage(writer);
writer.flush();
subjectAndBody[0] = sendPage.getSubject();
subjectAndBody[1] = out.toString();
inner.cleanup();
getRequestGlobals(cycle).store(cycle);

I also have the following method in my bookmarks, but I don't have an attribution. Could be the same user, but I'm not at all sure.

// page is the page with the email template 
  IPage prevPage = cycle.getPage(); 
  page.setProperty("user", user); 
  page.setProperty("url", url); 
  ByteArrayOutputStream out = new ByteArrayOutputStream(); 
  IMarkupWriter writer = page.getResponseWriter(out); 
  cycle.activate(page); 
  cycle.renderPage(writer); 
  writer.flush(); 
  String body = out.toString(); 
  cycle.activate(prevPage); 

I'm not so sure about activating a different page in the cycle and then reactivating the original, though. I suspect the upper method is more correct.

However, these methods no longer worked in 4.1, s when Dave Kallstrom figured out a new methodology, he posted it to the list:

       ResponseBuilder defaultBuilder =
           getRequestGlobals(cycle).getResponseBuilder();
       IEngine engine = cycle.getEngine();
       RequestCycleFactory factory = getRequestCycleFactory(cycle);

       IRequestCycle inner = factory.newRequestCycle(engine);
       String[] subjectAndBody = new String[2];
       BaseHTMLMessagePage sendPage = 
           (BaseHTMLMessagePage) getHtmlPage(inner);

       ByteArrayOutputStream out = new ByteArrayOutputStream();
       IMarkupWriter writer = 
           new MarkupWriterImpl("text/html", 
                                new PrintWriter(out), 
                                new AsciiMarkupFilter());
       ResponseBuilder builder = 
           new EmailResponseBuilder(writer,
                                    engine.getInfrastructure().getAssetFactory(), 
                                    false, 
                                    cycle);

       getRequestGlobals(cycle).store(builder);

       inner.activate(sendPage);
       inner.renderPage(builder);
       writer.flush();
       subjectAndBody[0] = sendPage.getSubject();
       subjectAndBody[1] = out.toString();
       inner.cleanup();

He added the following comment, too.

Notice the storing of the EmailResponseBuilder in requestGlobals and then
replacing it with the original DefaultResponseBuilder.
I'm not sure if all of this was necessary but it's the only way I could get
it to work. I will investigate using hivemind to wire up the
EmailResponseBuilder and also the HtmlEmailPage. But for now this is how I
got it to work.

SendingHtmlEmailWithTap (last edited 2011-01-02 19:49:52 by BobHarner)