Send transactional emails using Twilio Sendgrid 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 @sendgrid/mail package:
npm install @sendgrid/mail
Create an EmailSender
Next you need to create a custom EmailSender class which uses Sendgrid:
// src/config/sendgrid-email-sender.ts
import { EmailSender, EmailDetails } from '@vendure/email-plugin';
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
class SendgridEmailSender implements EmailSender {
async send(email: EmailDetails) {
await sgMail.send({
to: email.recipient,
from: email.from,
subject: email.subject,
html: email.body,
});
}
}
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 { SendgridEmailSender } from './config/sendgrid-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 SendgridEmailSender(),
}),
],
};
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.