Skip to content

Design and manage your liquid email templates in Strapi CMS

License

Notifications You must be signed in to change notification settings

dynolabs/strapi-liquid-templates

Repository files navigation

Liquid email templates

Liquid Email Templates for Strapi

Plugin for design and manage liquidjs email templates in Strapi.

Version License

The Strapi Liquid Templates Plugin is a custom plugin for Strapi which allows to design and manage your liquidjs email templates.

⚙️ Installation

To install the The Strapi Liquid Templates Plugin, simply run one of the following command:

npm install strapi-liquid-templates
yarn add strapi-liquid-templates

⚡️ Usage

Template Types

Type Description
Base Template Layout templates which can be used with your custom templates or core templates
Custom Template Email templates which can be used with emails
Core Template Predefined email templates by users permissions plugin of strapi

1. Base Templates

Base Templates are the layout templates which can be used with your custom templates or core templates.

How to Create a Base Template

After installation, click on Liquid Email Templates at the dashboard sidebar menu.

base templates empty

Click on New Template button, and fill the required details for your base template.

base templates create

Press on Save to save your base template.

base templates create

2. Custom Templates

How to Create a Custom Template

Click on Custom Templates from the Template Types.

custom templates empty

Click on New Template button, and fill the required details for your custom template. You can use your base template as the layout by selecting it from the dropdown menu.

custom templates create

You can preview your template in desktop and mobile views by clicking on the Expand icon in the Preview section.

Desktop custom templates desktop preview

Mobile custom templates mobile preview

Once you're done with it, press on Save to save your custom template.

3. Core Templates

There are two core templates available for customization.

  1. Email address confirmation
  2. Reset password

How to Edit a Core Template

Click on Core Templates from the Template Types.

custom templates empty

Click the template you want to edit, and fill the required details for your core template. You can use your base template as the layout by selecting it from the dropdown menu.

custom templates create

Once you're done with it, press on Save to save your core template.

How to Test

Request a password reset for a user which was created under User collection type.

Send Emails Programmatically

You can send emails programmatically by using the Strapi Liquid Templates plugin. Here is an example of how to send an email:

// import type { Core } from '@strapi/strapi';

export default {
  /**
   * An asynchronous register function that runs before
   * your application is initialized.
   *
   * This gives you an opportunity to extend code.
   */
  register(/* { strapi }: { strapi: Core.Strapi } */) {},

  /**
   * An asynchronous bootstrap function that runs before
   * your application gets started.
   *
   * This gives you an opportunity to set up your data model,
   * run jobs, or perform some special logic.
   */
  async bootstrap(/* { strapi }: { strapi: Core.Strapi } */) {
    try {
      await strapi
        .plugin('liquid-templates')
        .service('email')
        .sendLiquidEmail(
          {
            // required
            // This can also be an array of email addresses
            to: 'to@example.com',
            // Optional
            cc: ['cc-one@example.com', 'cc-two@example.com'],
            // Optional
            bcc: ['bcc@example.com'],
            // optional if /config/plugins.js -> email.settings.defaultFrom is set
            from: 'from@example.com',
            // optional if /config/plugins.js -> email.settings.defaultReplyTo is set
            replyTo: 'reply@example.com',
            // optional array of files
            attachments: [],
          },
          {
            // required - Reference Id defined in the custom template (won't change on import)
            referenceId: 'account-approved',
            // If provided here will override the template's subject.
            // Variables can be included as `Congratulations {{ USER.firstName }}! Your Account has been approved!`
            subject: `Account Approved`,
          },
          {
            // this object must include all variables you're using in your email template
            USER: { firstName: 'John', lastName: 'Doe' },
          }
        );
      strapi.log.info('Email sent');
    } catch (error) {
      strapi.log.error(error);
    }
  },
};

👍 Contribute

If you want to say Thank You and/or support the active development of Strapi Liquid Email Templates:

  1. Add a GitHub Star to the project.
  2. Support the project by donating a cup of coffee.

🧾 License

This plugin is licensed under the MIT License. See the LICENSE file for more information.