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

loopback-component-mailer

v1.3.0

Published

Mailer component for loopback

Downloads

6

Readme

loopback-component-mailer

This component adds a mailer to loopback. Templating is done with handlebars. The queue is handled by kue/redis. The sole transport is currently sendgrid, but the module is built to be easily extendable.

Requirements

Redis >= 2.6.12

Usage

Installation

  1. Install in you loopback project:

npm install --save loopback-component-mailer

  1. Create a component-config.json file in your server folder (if you don't already have one)

  2. Configure options inside component-config.json. (see configuration section)

{
  "loopback-component-mailer": {
    "{option}": "{value}"
  }
}
  1. Create a folder for storing templates.

The default location is /server/mailer/templates. This can be set in component-config.json (see below)

Configuration

Options:

  • templatePath

    [String] : The location of of the email templates relative to the project root. (default: '/server/mailer/templates/')

  • namespace

    [String] : The name used for attaching to the loopback app object. (default: 'mailer')

  • redis

    [Object] : Config for connection to redis server. (default: { host: 127.0.0.1, port: 6379 })

  • email

    [Object] : Config email transport, currently only sendgrid and mailhog are supported. (default: { apiKey: '', transport: 'sendgrid', from: '', subject: '' })

    The apiKey is required for sendgrid only.

    You can use Mailhog to capture and display emails in local development. The simplest way is to spin up Mailhog in a docker container:

    docker run -p 8025:8025 -p 1025:1025 mailhog/mailhog

    You can then browse to localhost:8025 to see the emails that have been sent. If you run mailhog on non-default host or port (localhost:1025) then add the host and port into the configuration in your component-config.json:

    {
      "loopback-component-mailer": {
        "email": {
          "host": "not-localhost",
          "port": 1212,
          "transport": "mailhog"
          }
       }
    }

Templates

Mail templates are in handlebars format. The subject line for the email should be the first line in the template file and is identified using the following format:

SUBJECT::Your Email Subject

You can also use handlebars in the subject line, for example:

SUBJECT::Your Email regarding {{topic}}

Template files should have the extension .hbs

HTML Header/Footer

A static HTML header and/or footer can be added to HTML emails. Specify the path to the header/footer file(s) in the component config.

Example:

{
  "loopback-component-mailer": {
    "email": {
      "apiKey": "<yourApiKey>",
      "header": "/server/mailer/templates/header.html",
      "footer": "/server/mailer/templates/footer.html"
    }
  }
}

Sending mail from Loopback

When using the default namespace mailer, you can add to the mail queue with the following:

  app.mailer.send(templateName, emailData, callback)

Arguments

  • templateName:[string] the name of your email template file
  • emailData: [object] example:
  {
    to: <[email protected]>, // can be an array of recipients
    from: <[email protected]>, // only required if different from the default set in component-config.json
    bcc: <[email protected]>, // not required, can be an array of recipients
    cc: <[email protected]>, // not required, can be an array of recipients
    replyto: <[email protected]>, // not required
    msgVariables: {
      subjectVariable: 'New Subject',
      to: 'Myself',
      text: 'Integration Test'
    }
  }
  • to: the email address of the recipient
  • from: the email address of the sender (if different from the default set in component-config.json)
  • bcc: the email address(es) of the blind carbon copy recipient(s)
  • cc: the email address(es) of the carbon copy recipient(s)
  • replyto: the email address to set for reply-to
  • msgVariables: variables for use in the template file
  • callback: [function] this function will be called when the mail has been added to the queue