Send transactional emails using Twilio Sendgrid with Vendure’s EmailPlugin
Integration type
Category
Repository
Package
Last published
Downloads in past month
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
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,
});
}
}
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(),
}),
],
};