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

@t00nday/nestjs-mailer

v1.2.9

Published

A simple nestjs for mailing and mail templating.

Downloads

4

Readme

Nestjs Mailer

This is a mailer package built for nestjs leveraging the power of the observer pattern (i.e. Observable package). It is a simple nestjs API to the email-templates npm package which in turn uses nodemailer but simplified to use your preferred template engine. It also uses the juice npm package to inline the css from your css file.

Installation

Installation is as simple as running:

npm install -s @t00nday/nestjs-mailer email-templates

or

yarn add @t00nday/nestjs-mailer email-templates

Usage

A basic usage example:

  1. Register the module as a dependency:

This could be done synchronously using the register() method:

./app.module.ts

import { Module } from '@nestjs/common';
import { MailerModule } from '@t00nday/nestjs-mailer';

@Module({
    imports: [
        // ... other modules
        MailerModule.register({
            message: {
                from: '[email protected]',
                // ...
            },
            // ...
        }),
    ],
})
export class AppModule {}

The module could also be registered asynchronously using any of the approaches provided by the registerAsync() method:

Examples below:

  • Using factory provider approach

./app.module.ts

// prettier-ignore
import { 
    MailerModule, 
    MailerModuleOptions 
} from '@t00nday/nestjs-mailer';
import { Module } from '@nestjs/common';

@Module({
    imports: [
        // ... other modules
        MailerModule.registerAsync({
            useFactory: (): MailerModuleOptions => ({
                message: {
                    from: '[email protected]',
                    // ...
                },
                // ...
            }),
        }),
    ],
})
export class AppModule {}
  • Using class or existing provider approach:

./mailer-config.service.ts

import {
    MailerModuleOptions,
    MailerOptionsFactory,
} from '@t00nday/nestjs-mailer';
import { Injectable } from '@nestjs/common';

@Injectable()
export class MailerConfigService implements MailerOptionsFactory {
    createMailerOptions(): MailerModuleOptions {
        return {
            message: {
                from: '[email protected]',
                // ...
            },
            // ...
        };
    }
}

The MailerConfigService SHOULD implement the MailerOptionsFactory, MUST declare the createMailerOptions() method and MUST return MailerModuleOptions object.

./app.module.ts

// prettier-ignore
import {
    MailerModule,
    MailerModuleOptions
} from '@t00nday/nestjs-mailer';
import { Module } from '@nestjs/common';

import { MailerConfigService } from './mailer-config.service.ts';

@Module({
    imports: [
        // ... other modules
        MailerModule.registerAsync({
            useClass: MailerConfigService,
        }),
    ],
})
export class AppModule {}
  1. Inject the MailerService as a dependency:

./app.service.ts

import { Observable } from 'rxjs';
import { Injectable } from '@nestjs/common';

@Injectable()
export class AppService {
    constructor(
        // ...other dependencies...
        private readonly mailerService: MailerService,
    ) {}

    sendMail(): Observable<string> {
        this.mailerService.send({
            message: {
                to: '[email protected]',
                // ...
            },
            template: 'welcome',
            // ...
        });
    }
}

Configuration

As mentioned above, this package is built as an API to the email-templates npm package hence the configuration is just the same as that of the original package. The bootstrap MailerModuleOptions is an alias for the EmailConfig (this can be found by intalling @types/email-templates usingnpm i -D @types/email-templates) declaration. This is properly documented here.

API Methods

As highlighted above, after registering the module, the MailerService is the gateway to access the module APIs hence, the instruction above to inject it into our class. This attempts to cover the methods provided by the mailer service - most similar to the implementations by the email-templates package and some not covered by the packages documentation.

The following methods share the same implementation architecture as the original packages so can be called as referenced in the email-templates package:

  • send<T = Record<string, any>(options: EmailOptions<T>): Observable<string>

  • juiceResource(html: string): Observable<string>

  • render<T = Record<string, any>>(view: string, locals: T): Observable<string>

  • renderAll(): Observable<string>

Other methods not documented in the package are detailed below:

  • getTemplatePath(template: string): Observable<string>: This returns the path to a template (the only string argument provided to the method). This is returned as a Observable<string>.

  • templateExists(view: string): Observable<boolean>: This return an Observable<boolean> indicating if a template for a view exists or otherwise.

  • checkAndRender<T = Record<string, any>>(type: keyof EmailMessage, template: string, locals: T): Observable<string>: This renders and returns Observable<string> if a template exists with the type (i.e. html, subject, text).

Further documentation and understanding of the email-templates package can be found on the packages documentation.

Contributing

Please read the contribution's guide.