Email Functionality in payload- 3 (Use an email service & Use a custom NodeMailer transport )

Use an email service

Many third party mail providers are available and offer benefits beyond basic SMTP. As an example, your payload init could look like this if you wanted to use SendGrid.com, though the same approach would work for any other NodeMailer transports shown here or provided by another third party.

import payload from 'payload'
import nodemailerSendgrid from 'nodemailer-sendgrid'


const sendGridAPIKey = process.env.SENDGRID_API_KEY


payload.init({
  ...(sendGridAPIKey
    ? {
        email: {
          transportOptions: nodemailerSendgrid({
            apiKey: sendGridAPIKey,
          }),
          fromName: 'Admin',
          fromAddress: 'admin@example.com',
        },
      }
    : {}),
})

Use a custom NodeMailer transport

To take full control of the mail transport you may wish to use nodemailer.createTransport() on your server and provide it to Payload init.

import payload from 'payload'
import nodemailer from 'nodemailer'


const payload = require('payload')
const nodemailer = require('nodemailer')


const transport = await nodemailer.createTransport({
  host: process.env.SMTP_HOST,
  port: 587,
  auth: {
    user: process.env.SMTP_USER,
    pass: process.env.SMTP_PASS,
  },
})


payload.init({
  email: {
    fromName: 'Admin',
    fromAddress: 'admin@example.com',
    transport,
  },
  // ...
})

Leave a comment

Your email address will not be published. Required fields are marked *