@saiuttej/nestjs-mailer
TypeScript icon, indicating that this package has built-in type declarations

0.0.11 • Public • Published

NestJS Mailer

Installation

Install via NPM:

npm install @saiuttej/nestjs-mailer

Install via Yarn:

yarn add @saiuttej/nestjs-mailer

Install via PNPM:

pnpm add @saiuttej/nestjs-mailer

Quick Start

Register Module

@Module({
  imports: [
    EmailsModule.forRoot({
      clients: [],
    }),
  ],
})
class AppModule {}

Quite often you might want to asynchronously pass module options instead of passing them beforehand. In such case, use forRootAsync() method like many other Nest.js libraries.

@Module({
  imports: [
    EmailsModule.forRootAsync({
      useFactory: (...deps) => {
        return {
          clients: [],
        };
      },
      inject: [...dependencies],
    }),
  ],
})
class AppModule {}

Clients Type

1. SES

Configuring SES

EmailsModule.forRoot({
  clients: [
    {
      type: EmailClientTypes.SES,
      key: 'unique-client-key',
      SES: {
        config: {
          credentials: {
            accessKeyId: 'aws-access-key',
            secretAccessKey: 'aws-secret-access-key',
          },
        },
        defaultSenderEmail: 'default-sender-email@gmail.com',
      },
    },
  ],
});

2. Mailtrap

EmailsModule.forRoot({
  clients: [
    {
      type: EmailClientTypes.MAILTRAP,
      key: 'unique-client-key',
      MAILTRAP: {
        config: {
          token: 'mailtrap-token',
        },
        defaultSenderEmail: 'default-sender-email@gmail.com',
      },
    },
  ],
});

EmailClientTypes is an enum that contains all the supported email clients.

Sending Emails

import { Injectable } from '@nestjs/common';
import {
  AdditionalSendEmailProps,
  EmailBodyProps,
  EmailClientOptions,
  EmailService,
} from '@saiuttej/nestjs-mailer';

@Injectable()
export class AppService {
  constructor(private readonly emailsService: EmailService) {}

  async sendEmail() {
    /**
     * It is the configuration for send email
     * It can have two types of values:
     * 1. string - The client key which is registered in the EmailsModule.
     * 2. EmailClientConfig - The client configuration object.
     */
    const client: string | EmailClientOptions = 'unique-client-key';

    /**
     * It is the email data object which contains the email details.
     * it contains the properties like
     * from, to, cc, bcc, subject, body, attachments, headers
     */
    const emailData: EmailBodyProps = {
      from: 'sender-email-id@gmail.com' /* optional */,
      to: ['to-email-ids@gmail.com'] /* required */,
      cc: [] /* optional */,
      bcc: [] /* optional */,
      subject: 'Email Subject' /* required */,
      body: 'Hello, World!' /* required - supports html, text and buffer */,
      attachments: [] /* optional - supports attachments */,
      headers: {} /* optional */,
    };

    /**
     * It is the additional options for sending email,
     * it contains different email sending options for the client.
     */
    const options: AdditionalSendEmailProps = {};

    await this.emailsService.send({
      client,
      emailData,
      options,
    });
  }
}

Contributing

Contributions welcome! See Contributing.

Author

B Sai Uttej

License

Licensed under the MIT License - see the LICENSE file for details.

Package Sidebar

Install

npm i @saiuttej/nestjs-mailer

Weekly Downloads

7

Version

0.0.11

License

MIT

Unpacked Size

31.2 kB

Total Files

23

Last publish

Collaborators

  • saiuttej