@nima__/nestjs-mailgun

NestJS provider for sending emails with mailgun

Usage no npm install needed!

<script type="module">
  import nimaNestjsMailgun from 'https://cdn.skypack.dev/@nima__/nestjs-mailgun';
</script>

README

NestJS Mailgun

NPM Version Package License NPM Downloads

Introduction

This is a simple wrapper of mailgun-js. It only supports sending and verifying emails, but later more will be added. Just ping me or open pull request and contribute :)

Installation

npm install @kkarimi/nestjs-mailgun

Usage

Importing module

import { MailgunModule } from '@kkarimi/nestjs-mailgun';
@Module({
  imports: [
    MailgunModule.forRoot({
      DOMAIN: '<Your Domain>',
      API_KEY: '<Your API_KEY>',
      HOST: '<Your Host>', // default: 'api.mailgun.net'. Note that if you are using the EU region the host should be set to 'api.eu.mailgun.net'
    }),
  ],
  providers: [],
  exports: [],
})
export class YourModule {}

Importing module Async

import { MailgunModule } from '@kkarimi/nestjs-mailgun';
@Module({
  imports: [
    MailgunModule.forAsyncRoot({
      useFactory: async () => {
        return {
          DOMAIN: '<Your Domain>',
          API_KEY: '<Your API_KEY>',
          HOST: '<Your Host>', // default: 'api.mailgun.net'. Note that if you are using the EU region the host should be set to 'api.eu.mailgun.net'
        };
      },
    }),
  ],
  providers: [],
  exports: [],
})
export class YourModule {}

Interfaces

interface EmailOptions {
  from: string;
  to: string | string[];
  subject: string;
  text?: string;
  html?: string;
  template?: string;
  attachment?;
  'h:X-Mailgun-Variables'?: string;
}

Calling Send Method

import { MailgunService } from '@kkarimi/nestjs-mailgun';
import { EmailOptions } from '@kkarimi/nestjs-mailgun'

@Injectable()
export class YourService {
  constructor(private mailgunService: MailgunService) {
    const options: EmailOptions = {
      from: '',
      to: '',
      subject: '',
      text: '',
      html: '',
      attachment:''
      'h:X-Mailgun-Variables': '{"key":"value"}'
    };

    await this.mailgunService.sendEmail(options);


    // OR can use the class

    const email = new MailgunEmailModel('from', 'to', 'subject', 'text', 'html', 'template','attachment', { key: 'value' });

    await this.mailgunService.sendEmail(email);
  }

Calling Verify Method

To check if an email is real or not.

import { MailgunService } from '@kkarimi/nestjs-mailgun';
import { EmailOptions } from '@kkarimi/nestjs-mailgun'

@Injectable()
export class YourService {
  constructor(private mailgunService: MailgunService) {
    await this.mailgunService.verifyEmail('next@examle.com');
  }
}

Contributing

Contributions welcome! See Contributing.

Notes

This project is not endorsed by or affiliated with Mailgun.

Author

Nuno Carvalhão Site

License

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