EmailEventHandler

EmailEventHandler

EmailEventHandler

The EmailEventHandler defines how the EmailPlugin will respond to a given event.

A handler is created by creating a new EmailEventListener and calling the .on() method to specify which event to respond to.

Example

const confirmationHandler = new EmailEventListener('order-confirmation')
  .on(OrderStateTransitionEvent)
  .filter(event => event.toState === 'PaymentSettled')
  .setRecipient(event => event.order.customer.emailAddress)
  .setSubject(`Order confirmation for #{{ order.code }}`)
  .setTemplateVars(event => ({ order: event.order }));

This example creates a handler which listens for the OrderStateTransitionEvent and if the Order has transitioned to the 'PaymentSettled' state, it will generate and send an email.

Handling other languages

By default, the handler will respond to all events on all channels and use the same subject (“Order confirmation for #12345” above) and body template. Where the server is intended to support multiple languages, the .addTemplate() method may be used to defined the subject and body template for specific language and channel combinations.

Example

const extendedConfirmationHandler = confirmationHandler
  .addTemplate({
    channelCode: 'default',
    languageCode: LanguageCode.de,
    templateFile: 'body.de.hbs',
    subject: 'Bestellbest├Ątigung f├╝r #{{ order.code }}',
  })

Signature

class EmailEventHandler<T extends string = string, Event extends EventWithContext = EventWithContext> {
  constructor(listener: EmailEventListener<T>, event: Type<Event>)
  filter(filterFn: (event: Event) => boolean) => EmailEventHandler<T, Event>;
  setRecipient(setRecipientFn: (event: Event) => string) => EmailEventHandler<T, Event>;
  setTemplateVars(templateVarsFn: SetTemplateVarsFn<Event>) => EmailEventHandler<T, Event>;
  setSubject(defaultSubject: string) => EmailEventHandler<T, Event>;
  setFrom(from: string) => EmailEventHandler<T, Event>;
  addTemplate(config: EmailTemplateConfig) => EmailEventHandler<T, Event>;
  setMockEvent(event: Omit<Event, 'ctx'>) => EmailEventHandler<T, Event>;
}

Members

constructor

method
type:
(listener: EmailEventListener<T>, event: Type<Event>) => EmailEventHandler

filter

method
type:
(filterFn: (event: Event) => boolean) => EmailEventHandler<T, Event>
Defines a predicate function which is used to determine whether the event will trigger an email. Multiple filter functions may be defined.

setRecipient

method
type:
(setRecipientFn: (event: Event) => string) => EmailEventHandler<T, Event>
A function which defines how the recipient email address should be extracted from the incoming event.

setTemplateVars

method
type:
(templateVarsFn: SetTemplateVarsFn<Event>) => EmailEventHandler<T, Event>
A function which returns an object hash of variables which will be made available to the Handlebars template and subject line for interpolation.

setSubject

method
type:
(defaultSubject: string) => EmailEventHandler<T, Event>
Sets the default subject of the email. The subject string may use Handlebars variables defined by the setTemplateVars() method.

setFrom

method
type:
(from: string) => EmailEventHandler<T, Event>
Sets the default from field of the email. The from string may use Handlebars variables defined by the setTemplateVars() method.

addTemplate

method
type:
(config: EmailTemplateConfig) => EmailEventHandler<T, Event>
Add configuration for another template other than the default "body.hbs". Use this method to define specific templates for channels or languageCodes other than the default.

setMockEvent

method
type:
(event: Omit<Event, 'ctx'>) => EmailEventHandler<T, Event>
Optionally define a mock Event which is used by the dev mode mailbox app for generating mock emails from this handler, which is useful when developing the email templates.