Skip to content

Fastify plugin for OhMySMTP/MailPace

License

Notifications You must be signed in to change notification settings

47ng/fastify-ohmysmtp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🚧 MIGRATION NOTICE 🚧

OhMySMTP has rebranded to MailPace. This package will change name soon. If you're considering using it, you might want to wait for v2 to be published under fastify-mailpace. This repository will be renamed.

See issue #7 for progress.


fastify-ohmysmtp

NPM MIT License Continuous Integration Coverage Status

Fastify plugin for OhMySMTP

Features

  • Send emails
  • Webhooks
  • Inbound emails (coming soon)

Installation

$ yarn add fastify-ohmysmtp
# or
$ npm i fastify-ohmysmtp

Usage

Minimal example:

import Fastify from 'fastify'
import fastifyOhMySMTP from 'fastify-ohmysmtp'

const server = Fastify()

server.register(fastifyOhMySMTP, {
  apiToken: 'my-api-token'
})

server.ohmysmtp.sendEmail({
  from: '[email protected]',
  to: '[email protected]',
  subject: 'test',
  htmlbody: '<h1>HTML Email</h1>'
})

Environment Variables

You can provide the API token via the configuration or via the OHMYSMTP_API_TOKEN environment variable.

Webhooks

You can enable reception of webhook events in the configuration object:

server.register(fastifyOhMySMTP, {
  apiToken: 'my-api-token',
  webhooks: {
    // Where to attach the webhook endpoint (POST)
    path: '/webhook',

    /*
     * An object map of handlers, where keys are the event types, listed here:
     * https://docs.ohmysmtp.com/guide/webhooks#email-events
     *
     * Values are async handlers that take as argument:
     * - The payload object of the webhook event
     * - The request object from Fastify
     * - The Fastify instance
     */
    handlers: {
      'email.spam': async (event, req, fastify) => {
        req.log.info(event, 'Spam detected')
        await fastify.ohmysmtp.sendEmail({
          from: '[email protected]',
          to: '[email protected]',
          subject: 'Spam detected',
          textbody: `Check event ${event.id} on OhMySMTP`
        })
      }
    },

    // You can pass additional Fastify route props here:
    routeConfig: {
      logLevel: 'warn'
    }
  }
})

License

MIT - Made with ❤️ by François Best

Using this package at work ? Sponsor me to help with support and maintenance.