@talosjs/mailer 1.0.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.
- package/LICENSE +21 -0
- package/README.md +418 -0
- package/dist/index.d.ts +66 -0
- package/dist/index.js +13263 -0
- package/dist/index.js.map +31 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Talos
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
# @talosjs/mailer
|
|
2
|
+
|
|
3
|
+
Transactional email service supporting Nodemailer SMTP and Resend API -- send templated emails with attachments and delivery tracking.
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+

|
|
7
|
+

|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
✅ **Resend API** - Send transactional emails via the Resend service
|
|
12
|
+
|
|
13
|
+
✅ **React Templates** - Build email templates using React components with server-side rendering
|
|
14
|
+
|
|
15
|
+
✅ **Layout System** - Pre-built email layout with header, body, and footer components
|
|
16
|
+
|
|
17
|
+
✅ **Environment Configuration** - Configure sender and API keys via environment variables
|
|
18
|
+
|
|
19
|
+
✅ **Container Integration** - Decorator-based registration with dependency injection
|
|
20
|
+
|
|
21
|
+
✅ **Error Handling** - Comprehensive error handling with custom MailerException
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
bun add @talosjs/mailer
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
### Basic Usage with Resend
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
import { ResendMailer } from '@talosjs/mailer';
|
|
35
|
+
|
|
36
|
+
const mailer = new ResendMailer({
|
|
37
|
+
apiKey: 'your-resend-api-key'
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
await mailer.send({
|
|
41
|
+
to: ['user@example.com'],
|
|
42
|
+
subject: 'Welcome to our platform!',
|
|
43
|
+
content: <WelcomeEmail userName="John" />
|
|
44
|
+
});
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Using Nodemailer SMTP
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import { NodeMailerAdapter } from '@talosjs/mailer';
|
|
51
|
+
|
|
52
|
+
const mailer = new NodeMailerAdapter({
|
|
53
|
+
host: 'smtp.example.com',
|
|
54
|
+
port: 587,
|
|
55
|
+
secure: false,
|
|
56
|
+
auth: {
|
|
57
|
+
user: 'your-username',
|
|
58
|
+
pass: 'your-password'
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
await mailer.send({
|
|
63
|
+
to: ['user@example.com'],
|
|
64
|
+
subject: 'Hello from Nodemailer!',
|
|
65
|
+
content: <SimpleEmail message="Hello World" />
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### With Environment Variables
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
import { ResendMailer } from '@talosjs/mailer';
|
|
73
|
+
|
|
74
|
+
// Automatically uses MAILER_RESEND_API_KEY environment variable
|
|
75
|
+
const mailer = new ResendMailer();
|
|
76
|
+
|
|
77
|
+
await mailer.send({
|
|
78
|
+
to: ['user@example.com'],
|
|
79
|
+
subject: 'Test Email',
|
|
80
|
+
content: <TestEmail />
|
|
81
|
+
});
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
**Environment Variables:**
|
|
85
|
+
- `MAILER_RESEND_API_KEY` - Resend API key
|
|
86
|
+
- `MAILER_SMTP_HOST` - SMTP server host
|
|
87
|
+
- `MAILER_SMTP_PORT` - SMTP server port
|
|
88
|
+
- `MAILER_SMTP_USER` - SMTP username
|
|
89
|
+
- `MAILER_SMTP_PASS` - SMTP password
|
|
90
|
+
|
|
91
|
+
### Using the Email Layout
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
import { MailerLayout } from '@talosjs/mailer';
|
|
95
|
+
|
|
96
|
+
function WelcomeEmail({ userName }: { userName: string }) {
|
|
97
|
+
return (
|
|
98
|
+
<MailerLayout
|
|
99
|
+
title="Welcome"
|
|
100
|
+
previewText="Welcome to our platform!"
|
|
101
|
+
>
|
|
102
|
+
<h1>Hello, {userName}!</h1>
|
|
103
|
+
<p>Thank you for joining us.</p>
|
|
104
|
+
<a href="https://example.com/dashboard">Get Started</a>
|
|
105
|
+
</MailerLayout>
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Custom Sender Information
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
import { ResendMailer } from '@talosjs/mailer';
|
|
114
|
+
|
|
115
|
+
const mailer = new ResendMailer();
|
|
116
|
+
|
|
117
|
+
await mailer.send({
|
|
118
|
+
to: ['user@example.com'],
|
|
119
|
+
subject: 'Important Update',
|
|
120
|
+
content: <UpdateEmail />,
|
|
121
|
+
from: {
|
|
122
|
+
name: 'Support Team',
|
|
123
|
+
address: 'support@example.com'
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## API Reference
|
|
129
|
+
|
|
130
|
+
### Classes
|
|
131
|
+
|
|
132
|
+
#### `ResendMailer`
|
|
133
|
+
|
|
134
|
+
Email adapter using the Resend API for sending emails.
|
|
135
|
+
|
|
136
|
+
**Constructor:**
|
|
137
|
+
```typescript
|
|
138
|
+
new ResendMailer(options?: { apiKey?: string })
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
**Parameters:**
|
|
142
|
+
- `options.apiKey` - Resend API key (optional if set via environment variable)
|
|
143
|
+
|
|
144
|
+
**Methods:**
|
|
145
|
+
|
|
146
|
+
##### `send(config: SendConfig): Promise<void>`
|
|
147
|
+
|
|
148
|
+
Sends an email using the Resend API.
|
|
149
|
+
|
|
150
|
+
**Parameters:**
|
|
151
|
+
- `config.to` - Array of recipient email addresses
|
|
152
|
+
- `config.subject` - Email subject line
|
|
153
|
+
- `config.content` - React node to render as email body
|
|
154
|
+
- `config.from` - Optional sender information
|
|
155
|
+
|
|
156
|
+
**Example:**
|
|
157
|
+
```typescript
|
|
158
|
+
const mailer = new ResendMailer();
|
|
159
|
+
|
|
160
|
+
await mailer.send({
|
|
161
|
+
to: ['recipient@example.com'],
|
|
162
|
+
subject: 'Welcome!',
|
|
163
|
+
content: <WelcomeEmail />
|
|
164
|
+
});
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
---
|
|
168
|
+
|
|
169
|
+
#### `NodeMailerAdapter`
|
|
170
|
+
|
|
171
|
+
Email adapter using Nodemailer for SMTP-based email sending.
|
|
172
|
+
|
|
173
|
+
**Constructor:**
|
|
174
|
+
```typescript
|
|
175
|
+
new NodeMailerAdapter(options?: NodeMailerOptionsType)
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
**Parameters:**
|
|
179
|
+
- `options.host` - SMTP server hostname
|
|
180
|
+
- `options.port` - SMTP server port
|
|
181
|
+
- `options.secure` - Use TLS (default: false for port 587)
|
|
182
|
+
- `options.auth.user` - SMTP username
|
|
183
|
+
- `options.auth.pass` - SMTP password
|
|
184
|
+
|
|
185
|
+
**Methods:**
|
|
186
|
+
|
|
187
|
+
##### `send(config: SendConfig): Promise<void>`
|
|
188
|
+
|
|
189
|
+
Sends an email using SMTP.
|
|
190
|
+
|
|
191
|
+
**Parameters:**
|
|
192
|
+
Same as `ResendMailer.send()`
|
|
193
|
+
|
|
194
|
+
---
|
|
195
|
+
|
|
196
|
+
#### `MailerLayout`
|
|
197
|
+
|
|
198
|
+
React component providing a pre-styled email layout.
|
|
199
|
+
|
|
200
|
+
**Props:**
|
|
201
|
+
- `title` - Email title (for document head)
|
|
202
|
+
- `previewText` - Preview text shown in email clients
|
|
203
|
+
- `children` - Email content
|
|
204
|
+
|
|
205
|
+
**Example:**
|
|
206
|
+
```typescript
|
|
207
|
+
<MailerLayout
|
|
208
|
+
title="Order Confirmation"
|
|
209
|
+
previewText="Your order has been confirmed"
|
|
210
|
+
>
|
|
211
|
+
<h1>Thank you for your order!</h1>
|
|
212
|
+
<p>Order #12345 has been confirmed.</p>
|
|
213
|
+
</MailerLayout>
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
### Interfaces
|
|
217
|
+
|
|
218
|
+
#### `IMailer`
|
|
219
|
+
|
|
220
|
+
```typescript
|
|
221
|
+
interface IMailer {
|
|
222
|
+
send: (config: {
|
|
223
|
+
to: string[];
|
|
224
|
+
subject: string;
|
|
225
|
+
content: React.ReactNode;
|
|
226
|
+
from?: { name: string; address: string };
|
|
227
|
+
}) => Promise<void>;
|
|
228
|
+
}
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
### Types
|
|
232
|
+
|
|
233
|
+
#### `MailerClassType`
|
|
234
|
+
|
|
235
|
+
```typescript
|
|
236
|
+
type MailerClassType = new (...args: any[]) => IMailer;
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
## Advanced Usage
|
|
240
|
+
|
|
241
|
+
### Integration with Talos App
|
|
242
|
+
|
|
243
|
+
```typescript
|
|
244
|
+
import { App } from '@talosjs/app';
|
|
245
|
+
import { ResendMailer } from '@talosjs/mailer';
|
|
246
|
+
|
|
247
|
+
const app = new App({
|
|
248
|
+
mailer: ResendMailer,
|
|
249
|
+
// ... other config
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
await app.run();
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### Using in Controllers
|
|
256
|
+
|
|
257
|
+
```typescript
|
|
258
|
+
import { Route } from '@talosjs/routing';
|
|
259
|
+
import type { IController, ContextType } from '@talosjs/controller';
|
|
260
|
+
|
|
261
|
+
@Route.http({
|
|
262
|
+
name: 'api.users.invite',
|
|
263
|
+
path: '/api/users/invite',
|
|
264
|
+
method: 'POST',
|
|
265
|
+
description: 'Send invitation email'
|
|
266
|
+
})
|
|
267
|
+
class InviteUserController implements IController {
|
|
268
|
+
public async index(context: ContextType): Promise<IResponse> {
|
|
269
|
+
const { email, name } = context.payload;
|
|
270
|
+
|
|
271
|
+
await context.mailer?.send({
|
|
272
|
+
to: [email],
|
|
273
|
+
subject: 'You are invited!',
|
|
274
|
+
content: <InvitationEmail recipientName={name} />
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
return context.response.json({ sent: true });
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
### Container Registration with Decorator
|
|
283
|
+
|
|
284
|
+
```typescript
|
|
285
|
+
import { container } from '@talosjs/container';
|
|
286
|
+
import { ResendMailer, decorator } from '@talosjs/mailer';
|
|
287
|
+
|
|
288
|
+
// Register with container using decorator
|
|
289
|
+
@decorator.mailer()
|
|
290
|
+
class MyMailerService extends ResendMailer {
|
|
291
|
+
// Custom implementation
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// Resolve from container
|
|
295
|
+
const mailer = container.get(MyMailerService);
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
### Error Handling
|
|
299
|
+
|
|
300
|
+
```typescript
|
|
301
|
+
import { ResendMailer, MailerException } from '@talosjs/mailer';
|
|
302
|
+
|
|
303
|
+
const mailer = new ResendMailer();
|
|
304
|
+
|
|
305
|
+
try {
|
|
306
|
+
await mailer.send({
|
|
307
|
+
to: ['user@example.com'],
|
|
308
|
+
subject: 'Test',
|
|
309
|
+
content: <TestEmail />
|
|
310
|
+
});
|
|
311
|
+
} catch (error) {
|
|
312
|
+
if (error instanceof MailerException) {
|
|
313
|
+
console.error('Mailer Error:', error.message);
|
|
314
|
+
// Handle email-specific error
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
### Building Custom Email Templates
|
|
320
|
+
|
|
321
|
+
```typescript
|
|
322
|
+
import { MailerLayout } from '@talosjs/mailer';
|
|
323
|
+
|
|
324
|
+
interface OrderConfirmationProps {
|
|
325
|
+
orderId: string;
|
|
326
|
+
items: Array<{ name: string; price: number }>;
|
|
327
|
+
total: number;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
function OrderConfirmationEmail({ orderId, items, total }: OrderConfirmationProps) {
|
|
331
|
+
return (
|
|
332
|
+
<MailerLayout
|
|
333
|
+
title="Order Confirmation"
|
|
334
|
+
previewText={`Order #${orderId} confirmed`}
|
|
335
|
+
>
|
|
336
|
+
<div style={{ fontFamily: 'Arial, sans-serif' }}>
|
|
337
|
+
<h1 style={{ color: '#333' }}>Order Confirmed!</h1>
|
|
338
|
+
<p>Order ID: <strong>{orderId}</strong></p>
|
|
339
|
+
|
|
340
|
+
<table style={{ width: '100%', borderCollapse: 'collapse' }}>
|
|
341
|
+
<thead>
|
|
342
|
+
<tr>
|
|
343
|
+
<th style={{ textAlign: 'left', padding: '8px' }}>Item</th>
|
|
344
|
+
<th style={{ textAlign: 'right', padding: '8px' }}>Price</th>
|
|
345
|
+
</tr>
|
|
346
|
+
</thead>
|
|
347
|
+
<tbody>
|
|
348
|
+
{items.map((item, index) => (
|
|
349
|
+
<tr key={index}>
|
|
350
|
+
<td style={{ padding: '8px' }}>{item.name}</td>
|
|
351
|
+
<td style={{ textAlign: 'right', padding: '8px' }}>
|
|
352
|
+
${item.price.toFixed(2)}
|
|
353
|
+
</td>
|
|
354
|
+
</tr>
|
|
355
|
+
))}
|
|
356
|
+
</tbody>
|
|
357
|
+
<tfoot>
|
|
358
|
+
<tr>
|
|
359
|
+
<td style={{ padding: '8px', fontWeight: 'bold' }}>Total</td>
|
|
360
|
+
<td style={{ textAlign: 'right', padding: '8px', fontWeight: 'bold' }}>
|
|
361
|
+
${total.toFixed(2)}
|
|
362
|
+
</td>
|
|
363
|
+
</tr>
|
|
364
|
+
</tfoot>
|
|
365
|
+
</table>
|
|
366
|
+
|
|
367
|
+
<p style={{ marginTop: '20px' }}>
|
|
368
|
+
Thank you for your purchase!
|
|
369
|
+
</p>
|
|
370
|
+
</div>
|
|
371
|
+
</MailerLayout>
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
### Sending to Multiple Recipients
|
|
377
|
+
|
|
378
|
+
```typescript
|
|
379
|
+
import { ResendMailer } from '@talosjs/mailer';
|
|
380
|
+
|
|
381
|
+
const mailer = new ResendMailer();
|
|
382
|
+
|
|
383
|
+
await mailer.send({
|
|
384
|
+
to: [
|
|
385
|
+
'user1@example.com',
|
|
386
|
+
'user2@example.com',
|
|
387
|
+
'user3@example.com'
|
|
388
|
+
],
|
|
389
|
+
subject: 'Team Announcement',
|
|
390
|
+
content: <AnnouncementEmail />
|
|
391
|
+
});
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
## License
|
|
395
|
+
|
|
396
|
+
This project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details.
|
|
397
|
+
|
|
398
|
+
## Contributing
|
|
399
|
+
|
|
400
|
+
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
|
|
401
|
+
|
|
402
|
+
### Development Setup
|
|
403
|
+
|
|
404
|
+
1. Clone the repository
|
|
405
|
+
2. Install dependencies: `bun install`
|
|
406
|
+
3. Run tests: `bun run test`
|
|
407
|
+
4. Build the project: `bun run build`
|
|
408
|
+
|
|
409
|
+
### Guidelines
|
|
410
|
+
|
|
411
|
+
- Write tests for new features
|
|
412
|
+
- Follow the existing code style
|
|
413
|
+
- Update documentation for API changes
|
|
414
|
+
- Ensure all tests pass before submitting PR
|
|
415
|
+
|
|
416
|
+
---
|
|
417
|
+
|
|
418
|
+
Made with ❤️ by the Talos team
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { EContainerScope } from "@talosjs/container";
|
|
2
|
+
type MailerClassType = new (...args: any[]) => IMailer;
|
|
3
|
+
interface IMailer {
|
|
4
|
+
send: (config: {
|
|
5
|
+
to: string[];
|
|
6
|
+
subject: string;
|
|
7
|
+
content: React.ReactNode;
|
|
8
|
+
from?: {
|
|
9
|
+
name: string;
|
|
10
|
+
address: string;
|
|
11
|
+
};
|
|
12
|
+
}) => Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
declare const decorator: {
|
|
15
|
+
mailer: (scope?: EContainerScope) => (target: MailerClassType) => void;
|
|
16
|
+
};
|
|
17
|
+
import { Exception } from "@talosjs/exception";
|
|
18
|
+
declare class MailerException extends Exception {
|
|
19
|
+
constructor(message: string, key: string, data?: Record<string, unknown>);
|
|
20
|
+
}
|
|
21
|
+
import { LocaleType } from "@talosjs/translation";
|
|
22
|
+
declare const MailerLayoutBody: ({ children, backgroundColor }: {
|
|
23
|
+
children: React.ReactNode;
|
|
24
|
+
backgroundColor?: string;
|
|
25
|
+
}) => React.JSX.Element;
|
|
26
|
+
declare const MailerLayoutFooter: ({ backgroundColor, instagram, tiktok, linkedin, facebook, slack }: {
|
|
27
|
+
backgroundColor?: string;
|
|
28
|
+
instagram?: string;
|
|
29
|
+
tiktok?: string;
|
|
30
|
+
linkedin?: string;
|
|
31
|
+
facebook?: string;
|
|
32
|
+
slack?: string;
|
|
33
|
+
}) => React.JSX.Element;
|
|
34
|
+
declare const MailerLayoutHeader: ({ children, backgroundColor }: {
|
|
35
|
+
children?: React.ReactNode;
|
|
36
|
+
backgroundColor?: string;
|
|
37
|
+
}) => React.JSX.Element;
|
|
38
|
+
type PropsType = {
|
|
39
|
+
locale?: LocaleType;
|
|
40
|
+
fontFamily?: string;
|
|
41
|
+
backgroundColor?: string;
|
|
42
|
+
children?: React.ReactNode;
|
|
43
|
+
};
|
|
44
|
+
type MailerLayoutComponentType = React.FC<PropsType> & {
|
|
45
|
+
Header: typeof MailerLayoutHeader;
|
|
46
|
+
Body: typeof MailerLayoutBody;
|
|
47
|
+
Footer: typeof MailerLayoutFooter;
|
|
48
|
+
};
|
|
49
|
+
declare const MailerLayout: MailerLayoutComponentType;
|
|
50
|
+
import { AppEnv } from "@talosjs/app-env";
|
|
51
|
+
declare class ResendMailer implements IMailer {
|
|
52
|
+
private readonly env;
|
|
53
|
+
private readonly client;
|
|
54
|
+
private from?;
|
|
55
|
+
constructor(env: AppEnv);
|
|
56
|
+
send(config: {
|
|
57
|
+
to: string[];
|
|
58
|
+
subject: string;
|
|
59
|
+
content: React.ReactNode;
|
|
60
|
+
from?: {
|
|
61
|
+
name: string;
|
|
62
|
+
address: string;
|
|
63
|
+
};
|
|
64
|
+
}): Promise<void>;
|
|
65
|
+
}
|
|
66
|
+
export { decorator, ResendMailer, MailerLayout, MailerException, MailerClassType, IMailer };
|