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

vicdotdev-mailer-hub

v0.1.3

Published

A naive mailing lib with multiple email service providers integrated.

Downloads

12

Readme

MailerHub

This package integrates multiple email service providers e.g. MailGun, SendGrid, when you use this package to send an email, it will try to send an email with each of its integrated services until success. That means, as long as you have at least one email service provider up and running, the email will be sent successfully.

As this package is still at its unstable version, you are not encourage to use at the moment.

Currently MailGun and SendGrid are supported, more email services might be added in later.

Getting Started

  • install the package
  npm install --save @victordotdevelop/mailer-hub
  • set up mailer hub with your own api keys (you can sign up at MailGun and SendGrid's site)
  • then try to send an email
    const mailerHubLib = require('vicdotdev-mailer-hub');
    const makeMailerHubBuilder = mailerHubLib.makeMailerHubBuilder;
    const ErrorTypes = mailerHubLib.ErrorTypes;

    // set up mailers, i.e. email service provider
    const builder = makeMailerHubBuilder();
    const mailHub = builder
    .addMailGun({
        apiKey: 'key-xxxxxxxxxxxxxxxxxxxxxxxxxxx',
        domain: 'sandboxxxxxxxxxxxxxxxxxxxxxxxxx.mailgun.org',
        senderAcc: 'postmaster',
    })
    .addSendGrid({
        apiKey: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
        domain: 'sendgrid.sandbox',
        senderAcc: 'postmaster',
    })
    .create();

    // send an email
    mailHub.send({
        to: '[email protected]',
        cc: '[email protected]',
        bcc: '[email protected]',
        subject: 'Sample code: how to send email with this package',
        text: `And more information in paragrahs`,
    })
    .then(() => {
      console.log('email was sent sucessfully');
    })
    .catch((err) => {
      if (err instanceof ErrorTypes.IncompleteMailError) {
        // this means you did not provide enough fields: to, subject, text
      }
      if (err instanceof ErrorTypes.AllMailerFailError) {
        // this means all of your email services provider failed to send this email
      }
    })

Note that you can also pass an array of email addresses for to, cc, bcc

TypeScript support

This project was written in typescript. You can look for function signatures from the .d.ts.

Development

If you want to further develop this package by your own:

  git clone <this repo github clone address>
  cd <this repo>
  npm intall

Run the tests

Note that you have to to rename ./src/tests/data/secret/xxxxx.secret.exmaple.ts back to ./src/tests/data/secret/xxxxx.secret.ts. Then fill in your own api keys and domain settings in the files. Then you can run

  npm test

Caution: If you are using MailGun's sandbox domain, running these tests will use up your sandbox message limit pretty quickly, by that time your tests against MailGun service will fail, and you will see console error messages Error: message limit reached

Build

  npm run build

Directory Structure

Below shows a glance of the source code folder

  src                                       
  ├── errors.ts                               # publicly exposed error types
  ├── index.ts                                # lib entrance
  ├── interfaces.ts                           # interfaces (data model)
  ├── mailer-hub.ts                           # main component, the abstraction layer of multiple email services
  ├── mailers                                 # specific email service providers
  │   ├── mailgun.ts
  │   └── sendgrid.ts
  └── tests                                   # test files and data
      ├── data
      │   ├── emails.ts
      │   └── secret
      │       ├── mailgun.secret.exmaple.ts
      │       ├── mailgun.secret.ts           # this is not committed to git repo. you have to fill in your own.
      │       ├── sendgrid.secret.example.ts
      │       └── sendgrid.secret.ts          # this is not committed to git repo. you have to fill in your own.
      ├── index.spec.ts
      ├── mailer-hub.spec.ts
      ├── mailers
      │   ├── mailgun.spec.ts
      │   └── sendgrid.spec.ts
      └── send-mail.spec.ts

Major Components

Below shows the major components in this package.

major components

  • IMailerHub: This is the object to be used by other parties to send an email
  • MailerHubBuilder: Users are supposed to input their configuration of different email service provider(i.e. _mailer) and build a IMailerHub object
  • IMailer: one IMailer represents exatly one specific email service provider.
  • sendOnce: a core function responsible for sending ONE successful email among many email servicer providers(they are called mailers in code). This function plays the logic of fail over.

TODOs

  • Validate credentials of different email service providers upon the creation of
  • Log each time of send email and how many services were tried. Expose the log for pakcage users to retrive. E.g. via events;

Acknowledgements

https://www.twilio.com/blog/2017/06/writing-a-node-module-in-typescript.html