0.0.9 • Published 10 months ago

@saiuttej/nestjs-mailer v0.0.9

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

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.

0.0.9

10 months ago

0.0.8

10 months ago

0.0.7

10 months ago

0.0.6

10 months ago

0.0.5

10 months ago

0.0.4

10 months ago

0.0.3

10 months ago

0.0.2

10 months ago

0.0.1

10 months ago