fastify-ohmysmtp

Fastify plugin for OhMySMTP

Usage no npm install needed!

<script type="module">
  import fastifyOhmysmtp from 'https://cdn.skypack.dev/fastify-ohmysmtp';
</script>

README

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: 'test@example.com',
  to: 'test@example.com',
  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: 'robots@example.com',
          to: 'admin@example.com',
          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.