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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@sologence/nest-js-email-sendgrid

v2.0.0

Published

NestJS SendGrid email integration

Downloads

229

Readme

NestJS SendGrid Integration

A robust NestJS module for seamless SendGrid email integration, providing an easy-to-use interface for sending emails using SendGrid's API.

Features

  • 🚀 Easy integration with NestJS applications
  • 📧 Support for template-based emails
  • 🎨 Custom HTML and plain text emails
  • ⚡ Type-safe email parameters
  • 🔒 Secure API key configuration
  • 🎭 Email masking support for secure logging

Installation

npm install @sologence/nest-js-email-sendgrid

Quick Start

  1. Import the module in your app.module.ts. You can use either synchronous registration or async registration:

Synchronous Registration

import { SendgridModule } from '@sologence/nest-js-email-sendgrid';

@Module({
  imports: [
    SendgridModule.register({
      apiKey: 'YOUR_SENDGRID_API_KEY',
      defaultFromEmail: '[email protected]',
      isGlobal: true, // optional, defaults to false
      masking: true //to enable masking while logging the email default false
    }),
  ],
})
export class AppModule {}

Asynchronous Registration

import { SendgridModule } from '@sologence/nest-js-email-sendgrid';

@Module({
  imports: [
    SendgridModule.registerAsync({
      imports: [ConfigModule], // optional: import modules that are needed for config
      useFactory: async (configService: ConfigService) => ({
        apiKey: configService.get('SENDGRID_API_KEY'),
        defaultFromEmail: configService.get('SENDGRID_FROM_EMAIL'),
        isGlobal: true, // optional, defaults to false
        masking: true //to enable masking while logging the email default false
      }),
      inject: [ConfigService], // optional: services to inject into useFactory
    }),
  ],
})
export class AppModule {}
  1. Inject and use the service in your components:
import { SendgridService } from '@sologence/nest-js-email-sendgrid';

@Injectable()
export class YourService {
  constructor(private readonly sendgridService: SendgridService) {}

  async sendWelcomeEmail(to: string) {
    await this.sendgridService.sendEmailFromTemplate({
      to,
      from: '[email protected]',
      templateId: 'your-template-id',
      dynamicTemplateData: {
        name: 'John Doe',
      },
    });
  }
}

API Reference

SendgridService Methods

sendEmailFromTemplate(params: EmailParams)

Send emails using SendGrid templates.

await sendgridService.sendEmailFromTemplate({
  to: '[email protected]',
  from: '[email protected]',
  templateId: 'template-id',
  dynamicTemplateData: {
    // Your template variables
  },
});

sendEmailCustomHtmlBody(params: EmailParams)

Send emails with custom HTML content.

await sendgridService.sendEmailCustomHtmlBody({
  to: '[email protected]',
  from: '[email protected]',
  subject: 'Hello',
  html: '<h1>Hello World!</h1>',
});

sendEmailCustomText(params: EmailParams)

Send plain text emails.

await sendgridService.sendEmailCustomText({
  to: '[email protected]',
  from: '[email protected]',
  subject: 'Hello',
  text: 'Hello World!',
});

Sending Emails with S3 Attachments

The module now supports sending emails with attachments directly from S3 URLs using Nodemailer transport:

// Inject the service
constructor(private readonly sendgridService: SendgridService) {}

// Send email with S3 attachment
await this.sendgridService.sendEmailWithS3Attachment({
  to: '[email protected]',
  from: '[email protected]', // optional if defaultFromEmail is set
  subject: 'Document Attached',
  text: 'Please find the attached document',
  url: 'https://your-bucket.s3.amazonaws.com/path/to/file',
  fileName: 'document.pdf' // optional, defaults to 'attachment'
});

S3 Attachment Parameters

| Parameter | Type | Required | Description | |-----------|------|----------|-------------| | to | string | string[] | Yes | Recipient email address(es) | | from | string | No | Sender email address (falls back to defaultFromEmail) | | subject | string | Yes | Email subject | | text | string | Yes | Email body text | | url | string | Yes | Full URL to the S3 file | | fileName | string | No | Custom filename for the attachment |

Response

The method returns a Promise that resolves to the Nodemailer send result object.

Configuration Options

| Option | Type | Description | | ----------- | -------- | ---------------------------- | | apiKey | string | Your SendGrid API key | | defaultFrom | string? | Default sender email address | | sandboxMode | boolean? | Enable SendGrid sandbox mode | | masking | boolean? | Enable email masking in logs |

Error Handling

The service throws typed errors that you can catch and handle:

try {
  await sendgridService.sendEmailFromTemplate(params);
} catch (error) {
  if (error instanceof SendGridError) {
    // Handle SendGrid specific errors
  }
  // Handle other errors
}

Contributing

We welcome contributions! Please feel free to submit a Pull Request.

License

MIT License - see the LICENSE file for details.

Support

For support, please create an issue in the GitHub repository or contact our support team.