Send transactional emails using Resend with Vendure’s EmailPlugin
Installation
The Vendure EmailPlugin is usually installed by default, but if you don’t have it in your project follow the installation instructions to install and configure the plugin in your project.
Next, install the resend package:
npm install resend
Create an EmailSender
Next you need to create a custom EmailSender class which uses Resend:
// src/config/resend-email-sender.ts
import { Logger } from '@vendure/core'
import { EmailDetails, EmailSender } from '@vendure/email-plugin'
import { Resend } from 'resend'
export class ResendEmailSender implements EmailSender {
protected resend: Resend
constructor(apiKey: string) {
this.resend = new Resend(apiKey)
}
async send(email: EmailDetails) {
const { error, data } = await this.resend.emails.send({
to: email.recipient,
from: email.from,
subject: email.subject,
html: email.body,
})
if (error) {
Logger.error(error.message, 'ResendEmailSender')
} else {
Logger.debug(`Email sent: ${data?.id}`, 'ResendEmailSender')
}
}
}
Configure the EmailPlugin
The final step is to configure the EmailPlugin to use this custom sender:
// src/vendure-config.ts
import { VendureConfig } from '@vendure/core';
import { defaultEmailHandlers, EmailPlugin } from '@vendure/email-plugin';
import { ResendEmailSender } from './config/resend-email-sender';
export const config: VendureConfig = {
// ...
plugins: [
EmailPlugin.init({
// ...
// the transport type "none" is used when using a
// custom EmailSender
transport: { type: 'none' },
emailSender: new ResendEmailSender(process.env.RESEND_API_KEY),
}),
],
};
Make sure you have the RESEND_API_KEY
environment variable defined, which contains a valid Resend API key.
Create your first commerce experience with Vendure in less than 2 minutes
Vendure is a registered trademark. Our trademark policy ensures that our brand and products are protected. Feel free to reach out if you have any questions about our trademarks.
Documentation
Newsletter
Get the latest product news and announcements delivered directly to your inbox.