Tutorials » How to write a simple email template
Web applications that send emails to their customers will need a way to generate dynamic content for email subject, email body or email attachments. Webpagebytes CMS is a great framework to generate dynamic content and so it can be used to create content to be sent via emails. All the features of Webpagebytes CMS can be used in the generated content for emails: real time editing of the content, images management, Freemarker scripting, page modules, localization and so on.
In order to generate a simple dynamic email body take the following tasks
- Create a site page with the body template
- Add dynamic data to the application model
- Fetch the body content and send it via email
Create a site page with the body template
Step 1
Writing an email template in Webpagebytes CMS it's simple as writing a web page, with the mention that there will be no site url to link to the email template site page. The email template is an internal resource that the web application will not want to expose via a site url.
In Webpagebytes CMS administration interface navigate to 'Site pages' section and create a new site page with the below HTML content.
The code in bold represent the Freemarker script that will generate the dynamic data in the content. The dynamic data values are taken from the application model via wpbAppModel key.
Add dynamic data to the application model
Step 2
The email will have to be sent from a server backend plugin (controller), which will have to populate the model with the dynamic data. The following code fragment can be an example for the current email template.
Fetch the body content and send it via email
Step 3
At this step the server controller will fetch the email subject content and will sent it via an email server. Webpagebytes CMS does not include an email server and so
the web application will have to use an external email server (either hosted in house or hosted on a 3rd party provider).
The following code fragment is an example of the email body fetch operation.
The email body template site page is taken from a customization parameter named 'emailBodyGuid'.
The users will see the following email