@memberjunction/communication-ms-graph 2.42.1 → 2.44.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +9 -9
  2. package/readme.md +256 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@memberjunction/communication-ms-graph",
3
- "version": "2.42.1",
3
+ "version": "2.44.0",
4
4
  "description": "MemberJunction: Microsoft Graph Provider for the MJ Communication Framework",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -21,14 +21,14 @@
21
21
  },
22
22
  "dependencies": {
23
23
  "@azure/identity": "^4.4.1",
24
- "@memberjunction/communication-types": "2.42.1",
25
- "@memberjunction/ai": "2.42.1",
26
- "@memberjunction/ai-openai": "2.42.1",
27
- "@memberjunction/aiengine": "2.42.1",
28
- "@memberjunction/core": "2.42.1",
29
- "@memberjunction/core-entities": "2.42.1",
30
- "@memberjunction/global": "2.42.1",
31
- "@memberjunction/sqlserver-dataprovider": "2.42.1",
24
+ "@memberjunction/communication-types": "2.44.0",
25
+ "@memberjunction/ai": "2.44.0",
26
+ "@memberjunction/ai-openai": "2.44.0",
27
+ "@memberjunction/aiengine": "2.44.0",
28
+ "@memberjunction/core": "2.44.0",
29
+ "@memberjunction/core-entities": "2.44.0",
30
+ "@memberjunction/global": "2.44.0",
31
+ "@memberjunction/sqlserver-dataprovider": "2.44.0",
32
32
  "@microsoft/microsoft-graph-client": "^3.0.7",
33
33
  "@azure/msal-node": "^2.14.0",
34
34
  "html-to-text": "^9.0.5",
package/readme.md CHANGED
@@ -1,5 +1,258 @@
1
- # @memberjunction/communication-core
1
+ # @memberjunction/communication-ms-graph
2
2
 
3
- The `@memberjunction/communication-core` library provides the fundamental objects that are used for the MemberJunction Communications framework. Other packages within the @memberjunction/communication-* world use this package as a foundation.
3
+ Microsoft Graph Provider for the MemberJunction Communication Framework. This package provides email communication capabilities through Microsoft Graph API, allowing you to send, receive, reply to, and forward emails using Azure Active Directory service accounts.
4
4
 
5
-
5
+ ## Overview
6
+
7
+ The `@memberjunction/communication-ms-graph` package implements the `BaseCommunicationProvider` interface from the MemberJunction communication framework, specifically for Microsoft Graph email operations. It uses OAuth 2.0 client credentials flow for authentication and supports full email functionality including HTML/text content, attachments, and thread management.
8
+
9
+ ## Features
10
+
11
+ - **Send Emails**: Send single or bulk emails with HTML/text content
12
+ - **Receive Emails**: Fetch emails with filtering, pagination, and read status management
13
+ - **Reply to Emails**: Reply to existing email threads
14
+ - **Forward Emails**: Forward emails to multiple recipients
15
+ - **HTML to Text Conversion**: Automatic conversion of HTML email content to plain text
16
+ - **Thread Management**: Track email conversations using thread IDs
17
+ - **Service Account Support**: Uses Azure AD service accounts for authentication
18
+
19
+ ## Installation
20
+
21
+ ```bash
22
+ npm install @memberjunction/communication-ms-graph
23
+ ```
24
+
25
+ ## Configuration
26
+
27
+ This provider requires the following environment variables to be set:
28
+
29
+ ```env
30
+ # Azure AD Application Registration
31
+ AZURE_CLIENT_ID=your-client-id
32
+ AZURE_TENANT_ID=your-tenant-id
33
+ AZURE_CLIENT_SECRET=your-client-secret
34
+
35
+ # Azure Endpoints
36
+ AZURE_AAD_ENDPOINT=https://login.microsoftonline.com
37
+ AZURE_GRAPH_ENDPOINT=https://graph.microsoft.com
38
+
39
+ # Service Account
40
+ AZURE_ACCOUNT_EMAIL=serviceaccount@yourdomain.com
41
+ AZURE_ACCOUNT_ID=optional-account-id
42
+ ```
43
+
44
+ ### Azure AD Setup
45
+
46
+ 1. Register an application in Azure Active Directory
47
+ 2. Grant the following Microsoft Graph API permissions:
48
+ - `Mail.Read`
49
+ - `Mail.ReadWrite`
50
+ - `Mail.Send`
51
+ - `User.Read.All`
52
+ 3. Create a client secret for the application
53
+ 4. Grant admin consent for the permissions
54
+
55
+ ## Usage
56
+
57
+ ### Basic Setup
58
+
59
+ ```typescript
60
+ import { MSGraphProvider } from '@memberjunction/communication-ms-graph';
61
+ import { ProcessedMessage } from '@memberjunction/communication-types';
62
+
63
+ // The provider is automatically registered with the MemberJunction framework
64
+ // using the @RegisterClass decorator with name 'Microsoft Graph'
65
+ const provider = new MSGraphProvider();
66
+ ```
67
+
68
+ ### Sending an Email
69
+
70
+ ```typescript
71
+ const message: ProcessedMessage = {
72
+ To: 'recipient@example.com',
73
+ Subject: 'Test Email',
74
+ ProcessedBody: 'This is a plain text email',
75
+ ProcessedHTMLBody: '<h1>This is an HTML email</h1>',
76
+ CCRecipients: ['cc@example.com'],
77
+ BCCRecipients: ['bcc@example.com']
78
+ };
79
+
80
+ const result = await provider.SendSingleMessage(message);
81
+
82
+ if (result.Success) {
83
+ console.log('Email sent successfully');
84
+ } else {
85
+ console.error('Failed to send email:', result.Error);
86
+ }
87
+ ```
88
+
89
+ ### Receiving Emails
90
+
91
+ ```typescript
92
+ import { GetMessagesParams } from '@memberjunction/communication-types';
93
+ import { GetMessagesContextDataParams } from '@memberjunction/communication-ms-graph';
94
+
95
+ const params: GetMessagesParams<GetMessagesContextDataParams> = {
96
+ NumMessages: 10,
97
+ UnreadOnly: true,
98
+ ContextData: {
99
+ Email: 'optional-service-account@domain.com', // Optional, defaults to AZURE_ACCOUNT_EMAIL
100
+ ReturnAsPlainText: true, // Converts HTML to plain text
101
+ MarkAsRead: true, // Marks messages as read after fetching
102
+ Filter: "(importance eq 'high')", // Optional OData filter
103
+ Top: 20 // Optional, overrides NumMessages
104
+ }
105
+ };
106
+
107
+ const result = await provider.GetMessages(params);
108
+
109
+ if (result.Success) {
110
+ result.Messages.forEach(message => {
111
+ console.log(`From: ${message.From}`);
112
+ console.log(`Subject: ${message.Subject}`);
113
+ console.log(`Body: ${message.Body}`);
114
+ console.log(`Thread ID: ${message.ThreadID}`);
115
+ });
116
+ }
117
+ ```
118
+
119
+ ### Replying to an Email
120
+
121
+ ```typescript
122
+ import { ReplyToMessageParams } from '@memberjunction/communication-types';
123
+
124
+ const replyParams: ReplyToMessageParams = {
125
+ MessageID: 'original-message-id',
126
+ Message: {
127
+ To: 'recipient@example.com',
128
+ ProcessedBody: 'This is my reply',
129
+ CCRecipients: ['cc@example.com'],
130
+ BCCRecipients: ['bcc@example.com']
131
+ }
132
+ };
133
+
134
+ const result = await provider.ReplyToMessage(replyParams);
135
+
136
+ if (result.Success) {
137
+ console.log('Reply sent successfully');
138
+ }
139
+ ```
140
+
141
+ ### Forwarding an Email
142
+
143
+ ```typescript
144
+ import { ForwardMessageParams } from '@memberjunction/communication-types';
145
+
146
+ const forwardParams: ForwardMessageParams = {
147
+ MessageID: 'original-message-id',
148
+ Message: 'Please see the forwarded message below',
149
+ ToRecipients: ['forward-to@example.com'],
150
+ CCRecipients: ['cc@example.com'],
151
+ BCCRecipients: ['bcc@example.com']
152
+ };
153
+
154
+ const result = await provider.ForwardMessage(forwardParams);
155
+
156
+ if (result.Success) {
157
+ console.log('Message forwarded successfully');
158
+ }
159
+ ```
160
+
161
+ ## API Reference
162
+
163
+ ### MSGraphProvider
164
+
165
+ The main class that implements email communication through Microsoft Graph.
166
+
167
+ #### Methods
168
+
169
+ ##### `SendSingleMessage(message: ProcessedMessage): Promise<MessageResult>`
170
+ Sends a single email message.
171
+
172
+ ##### `GetMessages(params: GetMessagesParams<GetMessagesContextDataParams>): Promise<GetMessagesResult<Message>>`
173
+ Retrieves emails from the service account's mailbox with optional filtering.
174
+
175
+ ##### `ReplyToMessage(params: ReplyToMessageParams): Promise<ReplyToMessageResult>`
176
+ Replies to an existing email thread.
177
+
178
+ ##### `ForwardMessage(params: ForwardMessageParams): Promise<ForwardMessageResult>`
179
+ Forwards an email to specified recipients.
180
+
181
+ ### Types
182
+
183
+ #### `GetMessagesContextDataParams`
184
+ Context data specific to MS Graph operations:
185
+
186
+ ```typescript
187
+ type GetMessagesContextDataParams = {
188
+ Email?: string; // Service account email (optional)
189
+ ReturnAsPlainText?: boolean; // Convert HTML to plain text
190
+ MarkAsRead?: boolean; // Mark messages as read after fetching
191
+ Filter?: string; // OData filter for message query
192
+ Top?: number; // Number of messages to return
193
+ }
194
+ ```
195
+
196
+ ## Dependencies
197
+
198
+ ### Core Dependencies
199
+ - `@memberjunction/communication-types`: Base types and interfaces
200
+ - `@memberjunction/core`: Core MemberJunction functionality
201
+ - `@memberjunction/core-entities`: Entity management
202
+ - `@memberjunction/global`: Global utilities and decorators
203
+
204
+ ### Microsoft Graph Dependencies
205
+ - `@microsoft/microsoft-graph-client`: Microsoft Graph API client
206
+ - `@microsoft/microsoft-graph-types`: TypeScript types for Graph API
207
+ - `@azure/identity`: Azure authentication library
208
+ - `@azure/msal-node`: Microsoft Authentication Library
209
+
210
+ ### Utility Dependencies
211
+ - `html-to-text`: HTML to plain text conversion
212
+ - `axios`: HTTP client
213
+ - `dotenv`: Environment variable management
214
+ - `env-var`: Environment variable validation
215
+
216
+ ## Integration with MemberJunction
217
+
218
+ This provider integrates seamlessly with the MemberJunction communication framework:
219
+
220
+ 1. **Automatic Registration**: The provider is automatically registered using the `@RegisterClass` decorator
221
+ 2. **Entity Support**: Works with MemberJunction entities for message persistence
222
+ 3. **AI Integration**: Compatible with AI-powered message processing through `@memberjunction/ai` packages
223
+ 4. **Template Support**: Can be used with the MemberJunction template engine for dynamic content
224
+
225
+ ## Build and Development
226
+
227
+ ```bash
228
+ # Build the package
229
+ npm run build
230
+
231
+ # Development mode with watch
232
+ npm start
233
+
234
+ # Run tests (not yet implemented)
235
+ npm test
236
+ ```
237
+
238
+ ## Error Handling
239
+
240
+ The provider includes comprehensive error handling:
241
+ - All methods return result objects with `Success` boolean and error details
242
+ - Errors are logged using MemberJunction's logging system
243
+ - Network and authentication errors are gracefully handled
244
+
245
+ ## Security Considerations
246
+
247
+ - Uses OAuth 2.0 client credentials flow
248
+ - Requires admin-consented permissions
249
+ - Service account credentials should be stored securely
250
+ - Supports principle of least privilege through scoped permissions
251
+
252
+ ## License
253
+
254
+ ISC License - see LICENSE file for details.
255
+
256
+ ## Author
257
+
258
+ MemberJunction.com