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

@autotelic/fastify-mail

v0.8.0

Published

A Fastify plugin for rendering and sending emails

Downloads

893

Readme

fastify-mail

A Fastify plugin that uses point-of-view and fastify-nodemailer to template and send email messages.

Usage

npm i @autotelic/fastify-mail

Setup

fastify-mail decorates the reply interface with the mail method and takes two options to get started: pov and transporter

Point-of-View

  • pov.engine should be a template engine object used to configure point-of-view

  • to see a list of engines currently supported by point-of-view with options, view docs here

  • For quick start, fastify-mail only requires the engine. For example, using nunjucks:

    fastify.register(mail, { pov: { engine: { nunjucks: require('nunjucks') } }, transporter: ... })
  • If you'd prefer to register point-of-view on your own, omit the engine option and fastify-mail will not register it.

  • If you configure point-of-view with a different decorator name, add this to the options of fastify-mail

    fastify.register(mail, { pov: { propertyName: 'POV_DECORATOR' }, transporter: ... })

Nodemailer

  • transporter should be an object defining connection data to be used as a nodemailer SMTP transport. View nodemailer's docs here

  • fastify-mail decorates fastify with nodemailer so a transporter must be defined

  • For example, using maildev:

    const transporter = {
      host: 'localhost',
      port: 1025,
      ignoreTLS: true
    }
    
    fastify.register(mail, { pov: { engine: ... }, transporter })

Example

// index.js
const mail = require("@autotelic/fastify-mail")

// register fastify-mail
fastify.register(mail, pov: { {engine: { TEMPLATE_ENGINE_OBJECT } }, transporter: { NODEMAILER_TRANSPORTER_OBJECT } })

// setup test route
fastify.get("/sendmail", async (req, reply) => {

  const message = {
    to: "[email protected]",
    from: "[email protected]",
    cc: "[email protected]",
    bcc: "[email protected]",
    subject: "This is a subject"
  }

  const opts = {
    templatePath: "path/to/my/templates/",
    context: { name: "Test Name" }
  }

  const queued = await fastify.mail.sendMail(message, opts)

  if (queued.error) {
    const { error } = queued
    reply.send(error);
  } else {
    const { messageId } = queued
    reply.send({ messageId })
  }
})

Templates

Each message must have the following templates with the file extension set in point-of-view config:

  • 'html': Contains the html template for the email.
  • 'text': Contains the text template for the email.
.
|--index.js
|--templates
    |-- html.njk
    |-- text.njk

Example Apps

See /examples/mailgun for a working example app using nodemailer-mailgun-transport.

See /examples/maildev for a working example app using MailDev

API

Decorator

This plugin decorates fastify with a mail object containing the following methods:

  • sendMail: 'function' - Calls createMessage to generate an message and uses fastify-nodemailer to send the generated email.

  • Accepts the following arguments:

    • message: 'object' This is a valid 'message' object as per the Nodemailer API

      • from: 'string' - The email address the email is to be sent from.
      • to: 'array' - Comma separated list or an array of recipients email addresses (string) that will appear on the To: field
      • cc: 'array' - Comma separated list or an array of recipients email addresses (string) that will appear on the Cc: field
      • bcc: 'array' - Comma separated list or an array of recipients email addresses (string) that will appear on the Bcc: field
      • replyTo : 'string' - An email address that will appear on the Reply-To: field
      • subject: 'string' - The subject of the email with context injected.
      • html: 'string' - The HTML version of the message as an Unicode string, with context injected.
      • text : 'string' - The plaintext version of the message as an Unicode string, with context injected
    • opts: 'object' - Object containing options:

      • templatePath: 'string' - the relative path to the message's templates.
      • context: 'object' - Object containing context for the message (such as - variables that will be used in copy)
  • Returns: 'object' with following properties:

    • accepted : 'array' of email addresses accepted - eg. [ '[email protected]' ]
    • rejected : 'array' of email addresses rejected - eg. [],
    • envelopeTime
    • messageTime
    • messageSize
    • response
    • envelope
    • messageId
  • createMessage: 'function' - Generates a message object where the data provided is updated to use templates where available with context variables injected

    • Accepts the following arguments:

      • message: 'object'
        • fields as above
      • templatePath: 'string' - the relative path to the message's templates.
      • context: 'object' - Object containing context for the message (such as - variables that will be used in copy)

      For more details on this response see the nodemailer documentation View nodemailer's docs here

Testing

Tap is used for testing. To run tests:

npm test