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

@webundsoehne/email

v2.1.1

Published

Email Sending Module for NestJS with Queue capabilities

Downloads

94

Readme

Web & Söhne is Austria's leading expert in programming and implementing complex and large web projects.

Introduction

  • The aim of this module is to provide an easy-to-use interface for sending Emails in a non-blocking manner in NestJS. You can either send Emails directly using the promise-based NodemailerModule or queueing the mails externally using EmailQueueService, which uses a Bull queue in the background.

Requirements

  • NestJS v8.0.0+
  • [Optional] for Email queueing: One or many redis instance(s)

Usage - Simple Nodemailer Service

  • Import NodemailerModule and initialize it with NodemailerModule.init(data)
import { NodemailerModule } from '@webundsoehne/email'

@Module({
    imports: [NodemailerModule.init(nodemailerOptions: EmailConfigOptions)]
})
export class ServerModule {}

You can then inject the NodemailerService in your Injectable Class of choice and use the async sendEmail method to send an Email.

import { NodemailerService } from '@webundsoehne/email'

@Injectable()
export class GreetingService {
    constructor(private mailService: NodemailerService) {}
    
    async sendGreet(): Promise<void> {
        await mailService.sendEmail({
          from: '[email protected]',
          to: '[email protected]',
          subject: 'Hello World!',
          text: 'Have a nice day!',
          html: '<b>Have a nice day!</b>'
        })
    }
}
  • After you have initialized NodemailerModule it will provide a NodemailerService in global scope. Make sure to only initialize the NodemailerModule once!

Initialization object (EmailConfigOptions)

  • This object must be provided during initialization

Type | Required | Description ------- | --------------- | ------------ EmailConfigOptions (partial) nodemailer SMTP transport options | true | used to construct a nodemailer SMTP transport - all possible options please see https://nodemailer.com/smtp/

e.g.

{
    host: 'smtp.host.com',
    port: 587,
    auth: { user: 'bob', pass: 'bob' }
}

Advanced usage - Email Queue

  • To get started import EmailModule and initialize it with EmailModule.init(data)
import { EmailModule } from '@webundsoehne/email'

@Module({
    imports: [EmailModule.init(data: EmailInitData)]
})
export class ServerModule {}
  • After you have initialized EmailModule it will provide a EmailQueueService in global scope. Make sure to only initialize the EmailModule once!

Initialization object (EmailInitData)

  • This object must be provided during initialization

Key | Type | Required | Description --------------------- | ------- | --------------- | ------------ nodemailerOptions | EmailConfigOptions (partial) (nodemailer SMTP transport options) | true | used to construct a nodemailer SMTP transport - all possible options please see https://nodemailer.com/smtp/ bullQueueOptions | BullModuleOptions | true | used to register a new BullQueue (queue name: email) - this is where you provide the details for your redis instance(s): possible options please see https://docs.nestjs.com/techniques/queues#queues defaultBullOptions | JobOptions | false | these options will be passed to every job you append to the queue (you also have the possibility to specify the options for every job you append on your own) customQueueProcessor | EmailQueueProcessor | false | You can use this class to override behavior of the queue processor (or add your own event listeners see https://docs.nestjs.com/techniques/queues#event-listeners)

Send Emails

You do not have to import the module more than once! EmailQueueService will be registered in global scope!

import { EmailQueueService, Email } from '@webundsoehne/email'

@Injectable()
export class MyService {
    constructor (private mailService: EmailQueueService) {}

    public sendMail(data: Email, options?: JobOptions) {
        this.mailService.sendEmail(data, options)
    }
}
  • after you call sendMail(data: Email) a new job will be automatically appended to the queue!
  • options are bull JobOptions (if you provided them during initialization you can overwrite them per email you want to send here)

Email interface

Key | Type | Required | Description --------------------- | ------- | --------------- | ------------ from | string | false | Set sender's address to | string | true | Set recipient address subject | string | true | Set subject text | string | true | Set email text bcc | string | false | Set bcc (blindcopy) email address cc | string | false | Set cc email address html | string | false | Set HTML E-Mail content attachments | EmailAttachment[] | false | Set file attached to email

Stay in touch