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

@goodrequest/grmailer

v0.1.0

Published

Mail client for GR projects. Transpiled as both ESM & CJS module.

Downloads

0

Readme

GR Mailer

Mail client for GR projects. Transpiled as both ESM & CJS module.

Documentation for ejs templates used by this library can be found on Goodrequest web wiki

ESM vs. CJS module

Here is the link used to setup this hybrid package.

TODO: write differences between ESM & CJS modules

Installation

npm i --save @goodrequest/grmailer

Initialization

createInstance static method returns tuple with GRMailer instance and verifyConnection promise. This promise can be ignored, or program can exit (with error code) when promise fails. Decision behind this is, that one can create & export mailer instance without waiting for promise, but sometimes it is useful to know, that email client cannot be reached.

import config from 'config'
import { GRMailer } from '@goodrequest/grmailer'

const emailConfig = config.get('email')

const [instance, promise] = GRMailer.createInstance(emailConfig)
promise.catch((err) => {
    console.error('[GRMailer]', err)
	process.exit(1)
})

You can pass additional options:

import config from 'config'
import { GRMailer } from '@goodrequest/grmailer'

import { emailLogger } from '../utils/logger'

const emailConfig = config.get('email')

GRMailer.createInstance(emailConfig, {
	logger: emailLogger
})ik

Options

Initialization config interface:

export interface IEmailConfig {
	host: string
	port: number
	secure: boolean
	auth: {
		user: string
		pass: string
	}
	from: string
	tls: {
		rejectUnauthorized: boolean
	}
}

Initialization options interface:

export interface IEmailOptions {
	logger?: any
	emailLogFileName?: string
	disableConnectionMessage?: boolean
	disableConnectionVerification?: boolean
}
  • emailLogFileName: filename for winston, string interpolation can be used
  • disableConnectionMessage: when set to true, no connection message will be shown after connecting
  • disableConnectionVerification: when set to true, verifyEmailConnection will not be called when creating new mailer instance

ENV

Config JSON:

email: {
		host: process.env.SMTP_HOST,
		port: Number(process.env.SMTP_PORT),
		secure: Number(process.env.SMTP_PORT) === 465,
		auth: {
			user: process.env.SMTP_USER,
			pass: process.env.SMTP_PASSWORD
		},
		from: process.env.SMTP_FROM_ADDRESS,
		tls: {
			rejectUnauthorized: false
		}
	},

ENV example

SMTP_HOST=0.0.0.0
SMTP_PORT=1025
SMTP_USER=admin
SMTP_PASSWORD=admin
[email protected]

Usage

1. Remove old code & add library

link to commit in the Backend project template which removes old email templates and add this library. Code can be found in the libs/email branch

2. Create instance

Like shown in the Initialization chapter

It creates class instance based on provided config, which means you can use multiple mail providers, or one with different accounts from one application. createInstance return tuple of [GRMailer instance, Promise].

Promise should be used to verify connection to email provider. It is the same promise returned from verifyEmailConnection method.

3. Save & export instance

It is recommended export instance from one file (e.g. emailService). So it can be imported from anywhere in the project.

4. Use Mailer

There are methods that uses already existing templates, or you can

Output

After connecting to email client, library will output green message.

When connection fails, library will output red error message.

Output can be disabled by setting disableConnectionMessage to false

API

  • static createInstance(config: IEmailConfig, options?: IEmailOptions): [GRMailer, Promise<void>]: Static method used to create mailer class instances
  • verifyEmailConnection(): Promise<void>: Used to verify connection to email client
  • sendMail(emailData: SendMailOptions): Promise<any>: General method for sending emails
  • sendMailWithTemplate(templatePath: string, to: string, translations: IPasswordResetTexts): Promise<any>: General method for sending emails, when you need to use different ejs file.
  • sendPasswordResetEmail(to: string, translations: IPasswordResetTexts): Promise<any>: Method for sending password reset emails. It uses passwordReset.ejs template to render email
  • sendInvitationEmail(to: string, translations: IInvitationEmailTexts): Promise<any>: Method for sending invitation emails. It uses invitation.ejs template to render email
  • sendVerificationEmail(to: string, translations: IEmailVerificationTexts): Promise<any>: Method for sending verification emails. It uses emailVerification.ejs template to render email
  • sendUniversalEmail(to: string, translations: IUniversalEmailTexts): Promise<any>: Method for sending other types of emails. It uses universal.ejs template to render email

Notes

  • Chalk needs to on version < 5.0.0, since it is only ESM module from version 5.0.0 onwards
  • This library does not contain tests, since we usually don't tests email methods in projects, but just mock them for tests.