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

nodemailer-mongoose

v0.1.2

Published

Nodemailer with MongoDB Queue

Downloads

1

Readme

nodemailer-mongoose

Nodemailer with a MongoDB queue. nodemailer-mongoose stores mails in the database and nodemailer sends them. This package is mostly useful when you have a large number of messages.

yarn add nodemailer-mongoose

Add a mail to the queue

Multiple clients can send mails without configure a server, as long as they use the same database.

/* 
  This function adds the message to the queue. It will not delivered directly.
  Uses options fom Nodemailer (https://nodemailer.com/message/).
*/
let newQueueItem = await mailer.sendMail({
  from: 	'[email protected]',
  to: 		'[email protected]',
  subject: 	'nodemailer-mongoose',
  text:		'This is the first mail delivered with nodemailer-mongoose.'
});

Deliver mails from the queue

You'll need to set up ONE client with smtp configuration, to process the queue.

/* 
  Configure the connection to an SMTP server.
  Uses options fom Nodemailer (https://nodemailer.com/smtp/). Already uses pooling. 
*/
mailer.createTransport({
  host:  'smtp.some.mail',
  port:  587,
  auth: {
    user:  '[email protected]',
    pass:  'sup3rs3cr3tpa55w0rd#'
  }
});
/* 
  This function reads all new mails from the database and try to send them.
  Failed mails will be tagged as FAILED.  
*/
mailer.deliver();  // delivers the default way (only new mails, refresh the queue every 30 seconds)
mailer.deliver('FAILED', 320);  // retry all failed mails every 320 seconds 

const agent = mailer.deliver(); // it returns an intverval
clearInterval(intervalID);  	// so it can be stoped like an interval

Full example

A simple client to save and deliver mails.

import * as mailer from 'nodemailer-mongoose';

// connect to mongodb use options from mongoose.connect()
mailer.connectToDatabase('mongodb://localhost:27017/nodemailer-mongoose');

// configure the connection
mailer.createTransport({
  host:  'smtp.some.mail',
  port:  587,
  auth: {
    user:  '[email protected]',
    pass:  'sup3rs3cr3tpa55w0rd#'
  }
});

// start to deliver mails and retry failed ones
mailer.deliver();
mailer.deliver('FAILED', 3600); // 3600sec = 1h

// add a mail to the queue
await mailer.sendMail({
  from: 	'[email protected]',
  to: 		'[email protected]',
  subject: 	'nodemailer-mongoose',
  text:		'This is the first mail delivered with nodemailer-mongoose.'
});

Advanced helpers

/* 
  This function removes FAILED messages from the queue.
  Default are mails at least 720 h old AND three attempts to send them.
*/
await mailer.removeFailed()
await mailer.removeFailed(MAX AGE IN HOURS, MAX SEND TRIES)
/* 
  This function removes all sent messages from the queue.
*/
await mailer.removeSent()
/* 
  This function returns all mails by the tag in the queue or history.
  SENT -> all succesfully send mails (the message is remove after delivery)
  FAILED -> all failed messages
  QUEUED -> messages not processed by deliver()
*/
let mails = await mailer.getPerState('SENT')
/* 
  This is the transporter configured by createTransport().
  Use it to attach plugins.
*/
mailer.transporter