DEV Community

Cover image for Email template service
Krisztián Maurer
Krisztián Maurer

Posted on • Updated on

Email template service

If we are developing a more serious app, we will probably use some kind of email notification or newsletter, or even just a password change email. In this article, I will present my email / pdf template service.

Code: https://github.com/MaurerKrisztian/template-api-tm

Demo: https://template-api-tm-production.up.railway.app/template

The goal was:

  • by entering the template-name and the data of the placeholders, I get back the filled-in html page

Thinking about this a step further, since I use it for emails 90% of the time, I have therefore built in the email send feature.

Example body:

{
   "mailOptions": {
      "to": "test@gmail.com", 
      "subject": "test email"
   }, 
   "template": {
      "name": "test_template", 
      "data": {
         "title": "test title", 
         "description": "This is a test description", 
         "color": "#DEB887"
      }
   }
}
Enter fullscreen mode Exit fullscreen mode

The following must be specified for each request: mailOption, template.name template.data

  • Error management: The data part is easy to mess up, so every template has a validator.

  • The other problem was that if we have several templates, we can't keep in mind what kind of data it expects, and digging through the code is quite time-consuming. That's why I added an endpoint where you can view the most important information: available templates, example data, etc.

https://template-api-tm-production.up.railway.app/template

  • You can access a playground for testing templates with different data with the press of a button it will rerender the template.

  • Emails can only work with inline css, however writing the entire template with inline css is not very nice and not maintainable, so the css specified in the style tag in the templates is automatically converted to inline when sent out.

  • Finally, I also included pdf generation, you can specify what you want: html or pdf when requesting.

This is what your generation process looks like:

  • data validation
  • fills in the template with the data field
  • converting to pdf (if necessary)
  • template inline css transformation
  • email sending

I's not perfect but mine. 🙂 Also my first article. 🥳

Top comments (1)

Collapse
 
tomford profile image
tomford51 • Edited

Nice,thanks. A competently created template really plays a big role and affects the perception of the recipient of your letter. Finding a quality template for cold email template b2b is essential for me, as I correspond with business partners and clients. I use advice on how to create templates for successful business deals