Vendure - Overwriting email templates

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system.
We'll be looking at two different layers of customization for customizing these emails.
We'll first have to add the email plugin, which we'll use to modify things.
npm install @vendure/email-plugin
Now we can open our vendure-config.ts file and load the plugin.
export const config: VendureConfig = {
plugins: [
EmailPlugin.init({
handlers: defaultEmailHandlers,
templatePath: path.join(__dirname, '../static/email/templates'),
globalTemplateVars: {
fromAddress: '"example" <[email protected]`>',
},
transport: {
type: 'smtp',
host: 'smtp.example.com',
port: 587,
auth: {
user: 'username',
pass: 'password',
},
},
}),
],
};
From this config, you can change quite a few variables. For a complete list, check out the official documentation.
This config can change almost everything that has to do with the existing templates.
You can change the copy and format of each email by modifying them in static/email/templates.
At one stage, I needed to support a brand-new email flow. By default, this was the email being sent on payment to give you an order confirmation, but I needed it to be sent pre-payment as payment instructions were manual for this shop.
So to add a new flow, we can create custom email event handlers on specific actions in our system.
This is the example of the order placed handler.
const orderPlacedHandler = new EmailEventListener('order-placed')
.on(OrderPlacedEvent)
.loadData(async ({ event, injector }) => {
transformOrderLineAssetUrls(event.ctx, event.order, injector);
const shippingLines = await hydrateShippingLines(event.ctx, event.order, injector);
return { shippingLines };
})
.setRecipient(event => event.order.customer!.emailAddress)
.setSubject(`Order confirmation for #{{ order.code }}`)
.setFrom(`{{ fromAddress }}`)
.setTemplateVars(event => ({ order: event.order, shippingLines: event.data.shippingLines }));
And to use it, we return this handler in our config.
EmailPlugin.init({
...
handlers: [orderPlacedHandler],
}),
When creating this new template, ensure that you add a new physical email in your template folder.
In this case, static/email/templates/order-placed/body.hbs.
Note: You can also subscribe to your own custom events.
Pretty cool, as it's straightforward to create and customize email flows within Vendure.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter