Back to plugins & integrations

SendGrid icon

SendGrid

Send transactional emails using Twilio Sendgrid with Vendure’s EmailPlugin

npm install @vendure/email-plugin

Integration type

Core
Core open-source plugins built by the Vendure team

Category

Email

Last published

3 days ago

Downloads in past month

12,633
README.md

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(),
    }),
  ],
};