fastify-ohmysmtp
v1.1.0
Published
Fastify plugin for OhMySMTP
Downloads
9
Maintainers
Readme
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.