@sibiltech/sibil-mail 1.0.2 → 1.0.3
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.
- package/README.md +325 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,2 +1,325 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
1
|
+
# @sibiltech/sibil-mail
|
|
2
|
+
|
|
3
|
+
A production-ready npm package for sending emails using nodemailer with TypeScript support. Part of the Sibil platform for sending and receiving emails.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- TypeScript support with full type definitions
|
|
8
|
+
- Simple, class-based API with `SibilMail` instance
|
|
9
|
+
- Automatic retry with configurable backoff
|
|
10
|
+
- Comprehensive error handling with custom error classes
|
|
11
|
+
- Support for HTML and plain text emails
|
|
12
|
+
- Attachment support
|
|
13
|
+
- CC and BCC support
|
|
14
|
+
- Connection verification with retry
|
|
15
|
+
- Provider-specific optimizations (Gmail, Outlook, Yahoo)
|
|
16
|
+
- Email validation utilities
|
|
17
|
+
- Template utilities for variable replacement
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install @sibiltech/sibil-mail
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Quick Start
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { SibilMail } from '@sibiltech/sibil-mail';
|
|
29
|
+
|
|
30
|
+
// Create a mailer instance
|
|
31
|
+
const mailer = new SibilMail({
|
|
32
|
+
host: 'smtp.gmail.com',
|
|
33
|
+
port: 587,
|
|
34
|
+
secure: false,
|
|
35
|
+
auth: {
|
|
36
|
+
user: 'your-email@gmail.com',
|
|
37
|
+
pass: 'your-app-password',
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// Send an email
|
|
42
|
+
const result = await mailer.send({
|
|
43
|
+
from: 'your-email@gmail.com',
|
|
44
|
+
to: 'recipient@example.com',
|
|
45
|
+
subject: 'Hello from Sibil Mail',
|
|
46
|
+
text: 'This is a plain text email',
|
|
47
|
+
html: '<p>This is an HTML email</p>',
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
console.log(result.success ? 'Sent!' : result.error);
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## API Reference
|
|
54
|
+
|
|
55
|
+
### `SibilMail` class
|
|
56
|
+
|
|
57
|
+
Create an instance with SMTP configuration. Each instance manages its own transporter and retry behavior.
|
|
58
|
+
|
|
59
|
+
#### Constructor: `new SibilMail(config: SmtpConfig, retryConfig?)`
|
|
60
|
+
|
|
61
|
+
**Parameters:**
|
|
62
|
+
|
|
63
|
+
- `config.host` (string, required): SMTP server hostname
|
|
64
|
+
- `config.port` (number, required): SMTP server port (e.g. 587, 465, 25)
|
|
65
|
+
- `config.secure` (boolean, optional): Use TLS (typically true for port 465)
|
|
66
|
+
- `config.auth.user` (string, required): SMTP username/email
|
|
67
|
+
- `config.auth.pass` (string, required): SMTP password or app password
|
|
68
|
+
- `config.tls` (object, optional): TLS options (e.g. `rejectUnauthorized: false`)
|
|
69
|
+
- `retryConfig` (optional): `{ maxRetries?: number; retryDelay?: number }` for send/verify retries
|
|
70
|
+
|
|
71
|
+
**Example:**
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
const mailer = new SibilMail({
|
|
75
|
+
host: 'smtp.gmail.com',
|
|
76
|
+
port: 587,
|
|
77
|
+
secure: false,
|
|
78
|
+
auth: {
|
|
79
|
+
user: 'your-email@gmail.com',
|
|
80
|
+
pass: 'your-app-password',
|
|
81
|
+
},
|
|
82
|
+
tls: {
|
|
83
|
+
rejectUnauthorized: false, // For self-signed certificates
|
|
84
|
+
},
|
|
85
|
+
}, { maxRetries: 3, retryDelay: 1000 });
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
#### `mailer.send(options: EmailOptions): Promise<EmailResult>`
|
|
89
|
+
|
|
90
|
+
Sends an email with validation and automatic retry.
|
|
91
|
+
|
|
92
|
+
**Parameters:**
|
|
93
|
+
|
|
94
|
+
- `options.from` (string, required): Sender email address
|
|
95
|
+
- `options.to` (string | string[], required): Recipient email address(es)
|
|
96
|
+
- `options.subject` (string, required): Email subject
|
|
97
|
+
- `options.text` (string, optional): Plain text content
|
|
98
|
+
- `options.html` (string, optional): HTML content (at least one of `text` or `html` is required)
|
|
99
|
+
- `options.cc` (string | string[], optional): CC recipients
|
|
100
|
+
- `options.bcc` (string | string[], optional): BCC recipients
|
|
101
|
+
- `options.replyTo` (string, optional): Reply-to address
|
|
102
|
+
- `options.attachments` (array, optional): Email attachments
|
|
103
|
+
|
|
104
|
+
**Returns:** `Promise<EmailResult>` with `{ success, messageId?, error?, response? }`
|
|
105
|
+
|
|
106
|
+
**Example:**
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
// Simple email
|
|
110
|
+
const result = await mailer.send({
|
|
111
|
+
from: 'you@example.com',
|
|
112
|
+
to: 'recipient@example.com',
|
|
113
|
+
subject: 'Hello',
|
|
114
|
+
text: 'Hello world!',
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
// HTML email
|
|
118
|
+
await mailer.send({
|
|
119
|
+
from: 'you@example.com',
|
|
120
|
+
to: 'recipient@example.com',
|
|
121
|
+
subject: 'Welcome',
|
|
122
|
+
html: '<h1>Welcome!</h1><p>Thanks for joining us.</p>',
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
// Multiple recipients and CC
|
|
126
|
+
await mailer.send({
|
|
127
|
+
from: 'you@example.com',
|
|
128
|
+
to: ['user1@example.com', 'user2@example.com'],
|
|
129
|
+
subject: 'Newsletter',
|
|
130
|
+
html: '<p>Check out our latest updates!</p>',
|
|
131
|
+
cc: 'manager@example.com',
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
// With attachments
|
|
135
|
+
await mailer.send({
|
|
136
|
+
from: 'you@example.com',
|
|
137
|
+
to: 'recipient@example.com',
|
|
138
|
+
subject: 'Report',
|
|
139
|
+
text: 'Please find the report attached.',
|
|
140
|
+
attachments: [
|
|
141
|
+
{ filename: 'report.pdf', path: './reports/report.pdf' },
|
|
142
|
+
{ filename: 'data.txt', content: 'Some file content' },
|
|
143
|
+
],
|
|
144
|
+
});
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
#### `mailer.verifyConnection(): Promise<boolean>`
|
|
148
|
+
|
|
149
|
+
Verifies the SMTP connection. Useful for checking credentials at startup.
|
|
150
|
+
|
|
151
|
+
**Example:**
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
try {
|
|
155
|
+
await mailer.verifyConnection();
|
|
156
|
+
console.log('SMTP connection verified successfully!');
|
|
157
|
+
} catch (error) {
|
|
158
|
+
console.error('SMTP verification failed:', error);
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
#### `mailer.getTransporter()`
|
|
163
|
+
|
|
164
|
+
Returns the underlying nodemailer transporter instance.
|
|
165
|
+
|
|
166
|
+
#### `mailer.updateConfig(config: SmtpConfig): void`
|
|
167
|
+
|
|
168
|
+
Updates SMTP configuration and recreates the transporter.
|
|
169
|
+
|
|
170
|
+
#### `mailer.getConfig()`
|
|
171
|
+
|
|
172
|
+
Returns current SMTP configuration (password masked).
|
|
173
|
+
|
|
174
|
+
#### `mailer.close(): Promise<void>`
|
|
175
|
+
|
|
176
|
+
Closes the transporter connection. Call when done using the mailer instance.
|
|
177
|
+
|
|
178
|
+
---
|
|
179
|
+
|
|
180
|
+
### Standalone utilities
|
|
181
|
+
|
|
182
|
+
You can also use transport and validation helpers without the class:
|
|
183
|
+
|
|
184
|
+
```typescript
|
|
185
|
+
import {
|
|
186
|
+
createSmtpTransport,
|
|
187
|
+
verifySmtpConnection,
|
|
188
|
+
closeTransport,
|
|
189
|
+
isValidEmail,
|
|
190
|
+
validateEmails,
|
|
191
|
+
replaceVariables,
|
|
192
|
+
createEmailTemplate,
|
|
193
|
+
} from '@sibiltech/sibil-mail';
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
---
|
|
197
|
+
|
|
198
|
+
## Error Handling
|
|
199
|
+
|
|
200
|
+
The package provides custom error classes for better error handling:
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
import {
|
|
204
|
+
SibilMail,
|
|
205
|
+
SmtpConnectionError,
|
|
206
|
+
EmailValidationError,
|
|
207
|
+
EmailSendError,
|
|
208
|
+
SibilMailError,
|
|
209
|
+
} from '@sibiltech/sibil-mail';
|
|
210
|
+
|
|
211
|
+
const mailer = new SibilMail({ /* config */ });
|
|
212
|
+
|
|
213
|
+
try {
|
|
214
|
+
await mailer.send({
|
|
215
|
+
from: 'you@example.com',
|
|
216
|
+
to: 'recipient@example.com',
|
|
217
|
+
subject: 'Test',
|
|
218
|
+
text: 'Test email',
|
|
219
|
+
});
|
|
220
|
+
} catch (error) {
|
|
221
|
+
if (error instanceof EmailValidationError) {
|
|
222
|
+
console.error('Invalid email:', error.invalidEmails);
|
|
223
|
+
} else if (error instanceof SmtpConnectionError) {
|
|
224
|
+
console.error('Connection failed:', error.message);
|
|
225
|
+
} else if (error instanceof EmailSendError) {
|
|
226
|
+
console.error('Send failed:', error.message, error.originalError);
|
|
227
|
+
} else if (error instanceof SibilMailError) {
|
|
228
|
+
console.error('Sibil Mail error:', error.message, error.code);
|
|
229
|
+
} else {
|
|
230
|
+
console.error('Unexpected error:', error);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
## Type Definitions
|
|
236
|
+
|
|
237
|
+
All types are exported for use in your TypeScript projects:
|
|
238
|
+
|
|
239
|
+
```typescript
|
|
240
|
+
import type {
|
|
241
|
+
SmtpConfig,
|
|
242
|
+
EmailOptions,
|
|
243
|
+
EmailResult,
|
|
244
|
+
EmailAttachment,
|
|
245
|
+
TransportFactory,
|
|
246
|
+
} from '@sibiltech/sibil-mail';
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
## Common SMTP Providers
|
|
250
|
+
|
|
251
|
+
### Gmail
|
|
252
|
+
|
|
253
|
+
```typescript
|
|
254
|
+
new SibilMail({
|
|
255
|
+
host: 'smtp.gmail.com',
|
|
256
|
+
port: 587,
|
|
257
|
+
secure: false,
|
|
258
|
+
auth: {
|
|
259
|
+
user: 'your-email@gmail.com',
|
|
260
|
+
pass: 'your-app-password', // Use App Password, not regular password
|
|
261
|
+
},
|
|
262
|
+
});
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
### Outlook / Hotmail
|
|
266
|
+
|
|
267
|
+
```typescript
|
|
268
|
+
new SibilMail({
|
|
269
|
+
host: 'smtp-mail.outlook.com',
|
|
270
|
+
port: 587,
|
|
271
|
+
secure: false,
|
|
272
|
+
auth: {
|
|
273
|
+
user: 'your-email@outlook.com',
|
|
274
|
+
pass: 'your-password',
|
|
275
|
+
},
|
|
276
|
+
});
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
### SendGrid
|
|
280
|
+
|
|
281
|
+
```typescript
|
|
282
|
+
new SibilMail({
|
|
283
|
+
host: 'smtp.sendgrid.net',
|
|
284
|
+
port: 587,
|
|
285
|
+
secure: false,
|
|
286
|
+
auth: {
|
|
287
|
+
user: 'apikey',
|
|
288
|
+
pass: 'your-sendgrid-api-key',
|
|
289
|
+
},
|
|
290
|
+
});
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
### Mailgun
|
|
294
|
+
|
|
295
|
+
```typescript
|
|
296
|
+
new SibilMail({
|
|
297
|
+
host: 'smtp.mailgun.org',
|
|
298
|
+
port: 587,
|
|
299
|
+
secure: false,
|
|
300
|
+
auth: {
|
|
301
|
+
user: 'your-mailgun-username',
|
|
302
|
+
pass: 'your-mailgun-password',
|
|
303
|
+
},
|
|
304
|
+
});
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
## Development
|
|
308
|
+
|
|
309
|
+
```bash
|
|
310
|
+
# Install dependencies
|
|
311
|
+
npm install
|
|
312
|
+
|
|
313
|
+
# Build the project
|
|
314
|
+
npm run build
|
|
315
|
+
|
|
316
|
+
# Run tests
|
|
317
|
+
npm test
|
|
318
|
+
|
|
319
|
+
# Watch / run dev scripts
|
|
320
|
+
npm run dev
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
## License
|
|
324
|
+
|
|
325
|
+
See [LICENSE](LICENSE) for terms. For licensing inquiries, contact sibiltech at info@sibiltech.com.
|