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

@softkit/mail

v0.2.4

Published

The Mailgun Mail Module is a comprehensive solution for integrating Mailgun's email functionality into NestJS applications. It provides a seamless way to send emails using Mailgun with minimal setup and configuration.

Downloads

31

Readme

Mailgun Mail Module

The Mailgun Mail Module is a comprehensive solution for integrating Mailgun's email functionality into NestJS applications. It provides a seamless way to send emails using Mailgun with minimal setup and configuration.

Features

  • Easy integration with Mailgun's email services in NestJS projects
  • Supports sending text, HTML, and template-based emails
  • Allows dynamic configuration for Mailgun, including support for asynchronous setup
  • Abstracts Mailgun client setup and provides a clean, service-based API for sending emails

Installation

yarn add @softkit/mail

For mailgun install mailgun.js

yarn add mailgun.js@^9.3.0

Usage

Import MailgunMailModule

import { Module } from '@nestjs/common';
import { MailgunMailModule } from '@softkit/mailgun-mail-module';

@Module({
  imports: [
    MailgunMailModule.forRoot({
      username: 'api',
      key: 'YOUR_API_KEY',
      domain: 'YOUR_DOMAIN',
      defaultFromEmail: '[email protected]',
    }),
  ],
})
export class YourAppModule {}

Asynchronous Configuration

If you need to configure the module asynchronously, for example, to inject some config:

@Module({
  imports: [
    MailgunMailModule.forRootAsync({
      useFactory: async (config: SomeMailConfig) => {
        return config;
      },
      inject: [SomeMailConfig],
    }),
  ],
})
export class YourAppModule {}

Add default configuration in your root config class

import { MailgunConfig } from '@softkit/mail';

export class RootConfig {
  @Type(() => MailgunConfig)
  @ValidateNested()
  public readonly mailgun!: MailgunConfig;
}

.env.yaml file

mailgun:
  domain: 'YOUR_DOMAIN'
  key: 'YOUR_API_KEY'
  username: 'api'
  defaultFromEmail: '[email protected]'
  defaultBccList:
    - '[email protected]'
    - '[email protected]'

Using native mailgun client (available in DI) in case if you need some customizations

   @Inject(MAILGUN_CLIENT_TOKEN) private mailgun: IMailgunClient

Enhancing Your Application with a Custom Typed Email Service

Enhance your NestJS application with a type-safe and versatile email service.

Quick Setup Guide

Step 1: Define Email Types

Start by creating an enumeration of the different types of emails your application will handle.

// email.types.ts
export enum EmailTypes {
  SIGNUP_EMAIL = 'SIGNUP_TEMPLATE',
  WELCOME = 'WELCOME_TEMPLATE',
}

Step 2: Specify Email Parameters

For each email type, define specific parameters to ensure that every email contains the right information.

// email-params.dto.ts
export class SignUpEmailParams {
  username: string;
  // Additional signup-specific parameters
}

export class WelcomeEmailParams {
  firstName: string;
  // Additional welcome-specific parameters
}

export type EmailDataParams<T extends EmailTypes> = 
  T extends EmailTypes.SIGNUP_EMAIL ? SignUpEmailParams :
  T extends EmailTypes.WELCOME ? WelcomeEmailParams :
  never;

Step 3: Create the Mail Service

Implement a mail service that uses these types, providing methods for sending various email types.

import { Injectable } from '@nestjs/common';
import { AbstractMailService } from './abstract-mail.service';
import { EmailTypes } from './types/email.types';
import { SendEmailDto, SendEmailResult } from './vo';
import { EmailDataParams } from './types/email-params.dto';

@Injectable()
export class MailService {
  constructor(private mailService: AbstractMailService<EmailTypes>) {}

  public sendTemplateEmail<T extends EmailTypes>(
    templateId: T,
    emailData: SendEmailDto,
    templateVariables: EmailDataParams<T>,
  ): Promise<SendEmailResult> {
    return this.mailService.sendTemplateEmail(
      templateId,
      emailData,
      templateVariables,
    );
  }

  public sendEmail(emailData: SendEmailDto): Promise<SendEmailResult> {
    return this.mailService.sendEmail(emailData);
  }
}