How to Create, Preview, and Send Custom Email Templates in Node.js

Apr 17, 2022 | Programming

Email communication remains an essential aspect of business interactions, and crafting the perfect email is made easier with custom email templates. In this guide, we’ll explore how to create, preview, and send email templates using the Email Templates library in Node.js. Whether you’re a developer or a business individual needing effective email communication, our user-friendly walkthrough will get you started.

Table of Contents

Install

Start by installing the necessary libraries. While we recommend using Pug for rendering templates, you can opt for any other template engine you prefer.

npm install email-templates preview-email pug

Preview

With the built-in preview-email feature, you can quickly see how your emails will look in a browser or iOS Simulator without sending them out. By default, in the development environment, your emails will be automatically rendered and opened in your browser.

Usage

Debugging

If you encounter configuration issues, you can enable detailed logging by setting the NODE_DEBUG flag:

NODE_DEBUG=email-templates node app.js

Basic

To send emails, utilize the following structure where you define a transport option, you can replace it with your specific configuration:

const Email = require('email-templates');
const email = new Email({
  message: {
    from: 'test@example.com',
    send: true,
    transport: {
      jsonTransport: true
    }
  }
});

email.send({
  template: 'mars',
  message: {
    to: 'elon@spacex.com',
  },
  locals: {
    name: 'Elon'
  }
}).then(console.log).catch(console.error);

Here’s an analogy to help you understand better: think of your email template as a recipe. Just like a recipe requires ingredients (locals) and cooking instructions (template format), your email template needs local variables and a defined structure to turn into a tasty dish (the email).

Attachments

If you want to send attachment files along with your message, follow this setup:

const Email = require('email-templates');
const email = new Email({
  message: {
    from: 'test@example.com',
    attachments: [
      {
        filename: 'text1.txt',
        content: 'hello world!'
      }
    ]
  }
});

email.send({
  template: 'mars',
  message: {
    to: 'elon@spacex.com',
  },
  locals: {
    name: 'Elon'
  }
}).then(console.log).catch(console.error);

Automatic Inline CSS via Stylesheets

Quickly include an external CSS file in your templates, which will automatically inline the styles for your email:

link(rel='stylesheet', href='css/app.css', data-inline)

Options

Customize your email instance further by adjusting options for message defaults, caching, and even language localization. For example:

const email = new Email({
  message: {
    from: 'test@example.com',
  },
  locals: {
    name: 'Elon'
  },
  i18n: {} // Localization support
});

Tips

To optimize your templates, consider:

  • Purge unused CSS to keep email size small and loading fast.
  • Use efficient stylesheet loading patterns to enhance performance.

Plugins

Enhance functionality by integrating various Nodemailer plugins as required.

Breaking Changes

For the latest updates and changes, visit the Releases page.

  • Forward Email – Free, encrypted, open-source email forwarding service for custom domains.

Contributors

Special thanks to the contributors who developed and maintained this valuable resource.

License

This project is licensed under the MIT License.

Troubleshooting

Should you encounter any challenges while implementing these email templates, consider the following troubleshooting ideas:

  • Ensure that the Node.js version is compatible with the package requirements.
  • Check that the template paths you defined are correct and the templates exist in your project structure.
  • Review error messages in your console for clues regarding issues.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox