Article
· Jan 4, 2022 2m read

Email templating with CSP

Hi Devs!

Recently, I needed to create templates for newsletters.
I couldn't find any tools, so I decided to use the CSP (Caché Server Pages) to build the templates.

Let's figure out how to make a newsletter similar to Weekly OEX Digest.


Weekly OEX Digest

There are strict requirements for HTML email messages.
Basic requirements:

  1. The page should be static;
  2. You have to use inline styles for mailer compatibility (so much code);
  3. Use custom fonts, some images, etc., is not allowed

CSP is well suited for this task because it allows you to do the complete generation on the server-side and produce a result as a static page.

The idea

  • A page with an email template is created and placed in the web application folder;
  • Use %Net.HTTPRequest to get an email generated from a template;
  • The letter is processed and sent to the recipient.

Basic elements and tags to build a template

To write the template, we need to understand the markup of the CSP:

Using these tags, you can quickly build a template body ready for generation

Transfer data to the template and error handling

You can pass data to the template using URL parameters.
You can also request data directly in the template in the OnPreHTTP method.

<script method = "OnPreHTTP" language="cache" runat="server" returntype="%Boolean">

...

</script>

This approach can be used to handle request parameters, query and prepare data, or handle errors.
The code in the method uses regular ObjectScript, and all variables declared inside this method will also be available from the template.

You can also return an error inside this method (the page will also return an error instead of generating). For example:

if (headers.%Size() = 0) {
    set %response.Status = "400"
    return 0
}

    With this knowledge of the CSP, you can easily implement templates for your newsletters.

    You can find the code of template for the Weekly OEX Digest example in the repository: CSPEmailTemplate

    Discussion (0)1
    Log in or sign up to continue