/

Email template

Customize default email templates


Email template configuration

Customizing the default email templates is simple:

All you have to do is overwrite the existing message template listed below using MessaginDirector at start up. existing message templates are defined here:

  • ACCOUNT_ACTION
  • DELIVERY
  • ORDER_CONFIRMATION
  • ORDER_REJECTION
  • QUOTATION_STATUS
  • ENROLLMENT_STATUS

Internally unchained uses mjml format or simple text for email templating.

first import MessagingDirector from @unchainedshop/core-messaging. for demonstration purpose lets just use simple text based template but feel free to create full fledge mjml format template or use any other kind of rendering engine. See the existing templates for reference.

import { MessagingDirector } from '@unchainedshop/core-messaging';

const template = `
Hello, thank you for visiting Unchained store {{date}} We hope you become part of our community in the future :).
Order number: {{orderNumber}}`

what is left now is use this to overweight any of the existing email template. in this case we will over ACCOUNT_ACTION.

next write the function that will combine this template with variables used in it using MessagingDirector.renderToText static function and return email configuration array of object/s.

Pro Tip: Messaging in Unchained is not tied to e-mails, the type that is return inside the config array is just a worker type and the input is just added to the work queue. So you could write your own "SMSTOBOSS" work type and call some Twilio API from there. You could even combine SMSTOBOSS wit the default e-mail and send the message via two channels.

const generateOrderConfirmationTemplate ({ order }) => {
  const orderNumber = order.orderNumber;
  const templateVariables = {
    orderNumber,
    date: new Date().toString()
  };
  const text = MessagingDirector.renderToText(template, templateVariables);
  return [
    {
      type: 'EMAIL',
      input: {
        from: 'sender@domain.com',
        to: 'reciever@domain.com',
        subject: 'Custom unchained email',
        text,
      },
    },
  ];
};

lastly we use MessagingDirector.configureTemplate after startPlatform on system boot to overwrite the default email template.

import { MessageTypes } from '@unchainedshop/platform';

...
startPlatform({...});
...
MessagingDirector.configureTemplate(
  MessageTypes.ORDER_CONFIRMATION,
  generateOrderConfirmationTemplate,
);
Edit on GitHub