npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fastify-mail

v1.0.5

Published

Nodemailer for Fastify

Downloads

14

Readme

fastify-mail

js-standard-style Version

Fastify nodemailer plugin, with this you can share the same nodemailer transporter in every part of your server.

Under the hood the it wraps nodemailer transporter and the options that you pass to register will be passed to the transporter. For configuration/usage details please check the nodemailer documentation.

Install

npm i fastify-mail --save
yarn add fastify-mail

Versions

The plugin supports the following Fastify and Nodemailer versions. Please refer to corresponding branch in PR and issues.

version | branch | fastify | nodemailer | End of support --------|--------|---------|------------|---------------
1.x | [maain](https://github.com/Zebedeu/fastify-mail | 4.x | 6.x |

Usage

Add it to you project with register and you are done! You can access the transporter via fastify.nodemailer and sendMail() via fastify.nodemailer.sendMail().

const fastify = require('fastify')()

fastify.register(require('fastify-mail'), {
  pool: true,
  host: 'smtp.example.com',
  port: 465,
  secure: true, // use TLS
  auth: {
      user: 'username',
      pass: 'password'
  }
})

fastify.get('/sendmail/:email', (req, reply, next) => {
  let { nodemailer } = fastify
  let recipient = req.params.email

  nodemailer.sendMail({
    from: '[email protected]',
    to: recipient,
    subject: 'foo',
    text: 'bar'
  }, (err, info) => {
    if (err) next(err)
    reply.send({
      messageId: info.messageId
    })
  })
})

fastify.listen(3000, err => {
  if (err) throw err
  console.log(`server listening on ${fastify.server.address().port}`)
})

New: You can also access the transporter via reply.nodemailer.sendMail().

fastify.get('/sendmail/:email', (req, reply, next) => {
  let recipient = req.params.email

  reply.nodemailer.sendMail({
    from: '[email protected]',
    to: recipient,
    subject: 'foo',
    text: 'bar'
  }, (err, info) => {
    if (err) next(err)
    reply.send({
      messageId: info.messageId
    })
  })
})

Custom transports

By default, passing an object as options to the plugin will configure nodemailer's main transport (SMTP).

If you need a custom transport, simply initialize the transport, and pass it to the plugin instead of an options object. For example, using the nodemailer-sparkpost-transport:

const fastify = require('fastify')()

const sparkPostTransport = require('nodemailer-sparkpost-transport')

const sparkPostTransportOptions = {
  sparkPostApiKey: 'MY_API_KEY'
}

fastify.register(require('fastify-mail'), sparkPostTransport(sparkPostTransportOptions))

// or

const sparkPostTransportInstance = sparkPostTransport(sparkPostTransportOptions)

fastify.register(require('fastify-mail'), sparkPostTransportInstance)

Then, later:

fastify.get('/sendmail', (req, reply, next) => {

  const sendOptions = {
    content: {
      template_id: 'my_template_id',
      use_draft_template: false
    },
    "recipients": [
      {
        "address": {
          "email": "[email protected]",
          "name": "John Doe"
        },
        "substitution_data": {
          username: "John Doe"
        }
      }
    ]
  };

  fastify.nodemailer.sendMail(sendOptions, (err, info) => {
    if (err) next(err)
    reply.send({
      messageId: info.messageId
    })
  })

})

Multiple transports

Since fastify-mail fully supports Fastify's built-in encapsulation feature, all you need to do is register this plugin with your custom transporter and the corresponding route in a new context.

License

Licensed under MIT.