2.48.0 • Published 5 months ago

@memberjunction/communication-sendgrid v2.48.0

Weekly downloads
-
License
ISC
Repository
-
Last release
5 months ago

@memberjunction/communication-sendgrid

SendGrid email provider implementation for the MemberJunction Communication Framework. This package enables sending emails through SendGrid's API within the MJ ecosystem.

Overview

The @memberjunction/communication-sendgrid package provides a SendGrid-based implementation of the BaseCommunicationProvider interface. It supports sending emails with various features including:

  • Plain text and HTML email sending
  • CC and BCC recipients
  • Scheduled sending
  • Custom sender names
  • Automatic retry and error handling

Note: This provider currently only supports sending messages. Message retrieval, forwarding, and replying functionalities are not implemented.

Installation

npm install @memberjunction/communication-sendgrid

Configuration

The SendGrid provider requires an API key to authenticate with SendGrid's services. Set the following environment variable:

COMMUNICATION_VENDOR_API_KEY__SENDGRID=your_sendgrid_api_key_here

You can obtain an API key from your SendGrid account dashboard.

Usage

Basic Setup

import { SendGridProvider } from '@memberjunction/communication-sendgrid';
import { RegisterClass } from '@memberjunction/global';

// The provider is automatically registered via the @RegisterClass decorator
// You can access it through the MJ class factory system

Sending an Email

import { ProcessedMessage, MessageResult } from '@memberjunction/communication-types';
import { ClassFactory } from '@memberjunction/global';
import { BaseCommunicationProvider } from '@memberjunction/communication-types';

// Get an instance of the SendGrid provider
const provider = ClassFactory.CreateInstance<BaseCommunicationProvider>(
    BaseCommunicationProvider, 
    'SendGrid'
);

// Create a message
const message: ProcessedMessage = {
    From: 'sender@example.com',
    FromName: 'Sender Name',
    To: ['recipient@example.com'],
    CCRecipients: ['cc@example.com'],
    BCCRecipients: ['bcc@example.com'],
    ProcessedSubject: 'Test Email',
    ProcessedBody: 'This is a plain text email',
    ProcessedHTMLBody: '<p>This is an <strong>HTML</strong> email</p>',
    SendAt: new Date('2024-12-25 10:00:00') // Optional: Schedule for future sending
};

// Send the message
const result: MessageResult = await provider.SendSingleMessage(message);

if (result.Success) {
    console.log('Email sent successfully');
} else {
    console.error('Failed to send email:', result.Error);
}

Integration with MJ Communication Engine

The SendGrid provider integrates seamlessly with the MJ Communication Engine:

import { CommunicationEngine } from '@memberjunction/communication-engine';

// The engine will automatically use registered providers
const engine = new CommunicationEngine();

// Send messages through the engine which will route to SendGrid
// based on your communication provider configuration

API Reference

SendGridProvider

The main class that implements the SendGrid email functionality.

Methods

SendSingleMessage(message: ProcessedMessage): Promise<MessageResult>

Sends a single email message through SendGrid.

Parameters:

  • message: ProcessedMessage object containing:
    • From: Sender email address (required)
    • FromName: Sender display name (optional)
    • To: Array of recipient email addresses (required)
    • CCRecipients: Array of CC recipients (optional)
    • BCCRecipients: Array of BCC recipients (optional)
    • ProcessedSubject: Email subject (required)
    • ProcessedBody: Plain text email body (optional)
    • ProcessedHTMLBody: HTML email body (optional)
    • SendAt: Date object for scheduled sending (optional)

Returns:

  • MessageResult object with:
    • Success: Boolean indicating success/failure
    • Error: Error message if failed
    • Message: The original message object
GetMessages(params: GetMessagesParams): Promise<GetMessagesResult>

Not implemented. Throws an error indicating SendGrid doesn't support message retrieval.

ForwardMessage(params: ForwardMessageParams): Promise<ForwardMessageResult>

Not implemented. Throws an error indicating SendGrid doesn't support message forwarding.

ReplyToMessage(params: ReplyToMessageParams): Promise<ReplyToMessageResult>

Not implemented. Throws an error indicating SendGrid doesn't support message replying.

LoadProvider Function

export function LoadProvider(): void

Utility function to ensure the provider class is included in the bundle and not removed by tree shaking.

Dependencies

  • @memberjunction/global: Core MJ utilities and class registration
  • @memberjunction/core: Core MJ functionality including logging
  • @memberjunction/core-entities: Core entity definitions
  • @memberjunction/communication-types: Communication framework type definitions
  • @sendgrid/mail: Official SendGrid Node.js SDK

Error Handling

The provider includes comprehensive error handling:

  • Network errors are caught and returned with descriptive messages
  • SendGrid API errors include response body details
  • All errors are logged using the MJ logging system
  • Failed sends return a MessageResult with Success: false and error details

Limitations

  1. Send-only: This provider only supports sending emails. It cannot retrieve, forward, or reply to messages.
  2. No attachment support: Current implementation doesn't include file attachments.
  3. No template support: SendGrid templates are not currently supported.
  4. Basic tracking: Subscription tracking is disabled by default.

Development

Building

npm run build

Project Structure

src/
 index.ts           # Public API exports
 SendGridProvider.ts # Main provider implementation
 config.ts          # Configuration and environment setup

Testing

Currently, no automated tests are configured. To add tests:

  1. Update the test script in package.json
  2. Add test files following MJ testing conventions
  3. Mock SendGrid API calls for unit tests

Integration Notes

  • The provider automatically registers itself with the MJ class factory system using the @RegisterClass decorator
  • It's designed to work within the MJ Communication Framework ecosystem
  • The provider name "SendGrid" must match your Communication Provider configuration in the MJ database

License

ISC - See LICENSE file in the repository root for details.

Support

For issues related to:

  • This package: Create an issue in the MemberJunction repository
  • SendGrid API: Refer to SendGrid documentation
  • MJ Communication Framework: Consult the MJ documentation

Contributing

Contributions are welcome! Please follow the MJ contribution guidelines and ensure all changes maintain backward compatibility.

2.27.1

8 months ago

2.23.2

9 months ago

2.46.0

5 months ago

2.23.1

9 months ago

2.27.0

8 months ago

2.34.0

6 months ago

2.30.0

7 months ago

2.19.4

9 months ago

2.19.5

9 months ago

2.19.2

9 months ago

2.19.3

9 months ago

2.19.0

9 months ago

2.19.1

9 months ago

2.15.2

9 months ago

2.34.2

6 months ago

2.34.1

6 months ago

2.38.0

5 months ago

2.45.0

5 months ago

2.22.1

9 months ago

2.22.0

9 months ago

2.41.0

5 months ago

2.22.2

9 months ago

2.26.1

8 months ago

2.26.0

8 months ago

2.33.0

6 months ago

2.18.3

9 months ago

2.18.1

9 months ago

2.18.2

9 months ago

2.18.0

9 months ago

2.37.1

6 months ago

2.37.0

6 months ago

2.14.0

10 months ago

2.21.0

9 months ago

2.44.0

5 months ago

2.40.0

5 months ago

2.29.0

8 months ago

2.29.2

8 months ago

2.29.1

8 months ago

2.25.0

8 months ago

2.48.0

5 months ago

2.32.0

7 months ago

2.32.2

7 months ago

2.32.1

7 months ago

2.17.0

9 months ago

2.13.4

10 months ago

2.36.0

6 months ago

2.13.2

11 months ago

2.13.3

10 months ago

2.13.0

11 months ago

2.36.1

6 months ago

2.13.1

11 months ago

2.43.0

5 months ago

2.20.2

9 months ago

2.20.3

9 months ago

2.20.0

9 months ago

2.20.1

9 months ago

2.28.0

8 months ago

2.47.0

5 months ago

2.24.1

8 months ago

2.24.0

8 months ago

2.31.0

7 months ago

2.12.0

12 months ago

2.39.0

5 months ago

2.16.1

9 months ago

2.35.1

6 months ago

2.35.0

6 months ago

2.16.0

9 months ago

2.42.1

5 months ago

2.42.0

5 months ago

2.23.0

9 months ago

2.11.0

12 months ago

2.10.0

1 year ago

2.9.0

1 year ago

2.8.0

1 year ago

2.7.0

1 year ago

2.6.1

1 year ago

2.5.2

1 year ago

2.6.0

1 year ago

2.7.1

1 year ago

2.5.1

1 year ago

2.5.0

1 year ago

2.4.1

1 year ago

2.4.0

1 year ago

2.3.3

1 year ago

2.3.2

1 year ago

2.3.1

1 year ago

2.3.0

1 year ago

2.2.1

1 year ago

2.2.0

1 year ago

2.1.5

1 year ago

2.1.4

1 year ago

2.1.3

1 year ago

2.1.2

1 year ago

2.1.1

1 year ago

2.0.0

1 year ago

1.8.1

1 year ago

1.8.0

1 year ago

1.7.1

1 year ago

1.7.0

1 year ago

1.6.1

1 year ago

1.6.0

1 year ago

1.5.3

1 year ago

1.5.2

1 year ago

1.5.1

1 year ago

1.5.0

1 year ago

1.4.1

1 year ago