title: "EmailEventHandler" isDefaultIndex: false
import MemberInfo from '@site/src/components/MemberInfo'; import GenerationInfo from '@site/src/components/GenerationInfo'; import MemberDescription from '@site/src/components/MemberDescription';
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)
.setFrom('{{ fromAddress }}')
.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.
The string argument passed into the EmailEventListener constructor is used to identify the handler, and
also to locate the directory of the email template files. So in the example above, there should be a directory
<app root>/static/email/templates/order-confirmation which contains a Handlebars template named body.hbs.
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 define the subject and body template for specific language and channel combinations.
The language is determined by looking at the languageCode property of the event's ctx (RequestContext) object.
Example
const extendedConfirmationHandler = confirmationHandler
.addTemplate({
channelCode: 'default',
languageCode: LanguageCode.de,
templateFile: 'body.de.hbs',
subject: 'Bestellbestätigung für #{{ order.code }}',
})
Let's say you have a plugin which defines a new event type, QuoteRequestedEvent. In your plugin you have defined a mutation
which is executed when the customer requests a quote in your storefront, and in your resolver, you use the EventBus to publish a
new QuoteRequestedEvent.
You now want to email the customer with their quote. Here are the steps you would take to set this up:
import { EmailEventListener } from `@vendure/email-plugin`;
import { QuoteRequestedEvent } from `./events`;
const quoteRequestedHandler = new EmailEventListener('quote-requested')
.on(QuoteRequestedEvent)
.setRecipient(event => event.customer.emailAddress)
.setSubject(`Here's the quote you requested`)
.setFrom('{{ fromAddress }}')
.setTemplateVars(event => ({ details: event.details }));
Next you need to make sure there is a template defined at <app root>/static/email/templates/quote-requested/body.hbs. The path
segment quote-requested must match the string passed to the EmailEventListener constructor.
The template would look something like this:
{{> header title="Here's the quote you requested" }}
<mj-section background-color="#fafafa">
<mj-column>
<mj-text color="#525252">
Thank you for your interest in our products! Here's the details
of the quote you recently requested:
</mj-text>
<--! your custom email layout goes here -->
</mj-column>
</mj-section>
{{> footer }}
You can find pre-made templates on the MJML website.
Finally, you need to register the handler with the EmailPlugin:
import { defaultEmailHandlers, EmailPlugin } from '@vendure/email-plugin';
import { quoteRequestedHandler } from './plugins/quote-plugin';
const config: VendureConfig = {
// Add an instance of the plugin to the plugins array
plugins: [
EmailPlugin.init({
handler: [...defaultEmailHandlers, quoteRequestedHandler],
// ... etc
}),
],
};
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>;
setLanguageCode(setLanguageCodeFn: (event: Event) => LanguageCode | undefined) => EmailEventHandler<T, Event>;
setTemplateVars(templateVarsFn: SetTemplateVarsFn<Event>) => EmailEventHandler<T, Event>;
setSubject(defaultSubject: string | SetSubjectFn<Event>) => EmailEventHandler<T, Event>;
setFrom(from: string) => EmailEventHandler<T, Event>;
setOptionalAddressFields(optionalAddressFieldsFn: SetOptionalAddressFieldsFn<Event>) => ;
setMetadata(optionalSetMetadataFn: SetMetadataFn<Event>) => ;
setAttachments(setAttachmentsFn: SetAttachmentsFn<Event>) => ;
addTemplate(config: EmailTemplateConfig) => EmailEventHandler<T, Event>;
loadData(loadDataFn: LoadDataFn<Event, R>) => EmailEventHandlerWithAsyncData<R, T, Event, EventWithAsyncData<Event, R>>;
setMockEvent(event: Omit<Event, 'ctx' | 'data'>) => EmailEventHandler<T, Event>;
}