ts-mailkit 1.1.0 → 2.0.1
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 +42 -42
- package/dist/core/client.d.ts.map +1 -1
- package/dist/core/errors.d.ts.map +1 -1
- package/dist/index.cjs +655 -856
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +636 -811
- package/dist/index.js.map +1 -1
- package/dist/renderers/default/default-renderer.d.ts.map +1 -1
- package/dist/templates/factory.d.ts +0 -60
- package/dist/templates/factory.d.ts.map +1 -1
- package/dist/transports/console/console-sender.d.ts.map +1 -1
- package/dist/transports/memory/memory-sender.d.ts.map +1 -1
- package/dist/transports/zeptomail/zeptomail-sender.d.ts.map +1 -1
- package/package.json +5 -5
package/dist/index.cjs
CHANGED
|
@@ -1,904 +1,703 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var index_exports = {};
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
ConfigurationError: () => ConfigurationError,
|
|
24
|
-
ConsoleEmailSender: () => ConsoleEmailSender,
|
|
25
|
-
DefaultEmailRenderer: () => DefaultEmailRenderer,
|
|
26
|
-
EmailTemplateFactory: () => EmailTemplateFactory,
|
|
27
|
-
MailKit: () => MailKit,
|
|
28
|
-
MailKitError: () => MailKitError,
|
|
29
|
-
MemoryEmailSender: () => MemoryEmailSender,
|
|
30
|
-
TemplateError: () => TemplateError,
|
|
31
|
-
TransportError: () => TransportError,
|
|
32
|
-
ZeptoMailHttpSender: () => ZeptoMailHttpSender,
|
|
33
|
-
buildHtml: () => buildHtml,
|
|
34
|
-
buildPlainText: () => buildPlainText,
|
|
35
|
-
createMailKit: () => createMailKit,
|
|
36
|
-
decodeHtmlEntities: () => decodeHtmlEntities,
|
|
37
|
-
htmlToPlainText: () => htmlToPlainText,
|
|
38
|
-
isValidHttpUrl: () => isValidHttpUrl,
|
|
39
|
-
normalizeRecipient: () => normalizeRecipient,
|
|
40
|
-
normalizeRecipients: () => normalizeRecipients,
|
|
41
|
-
parseEmailAddress: () => parseEmailAddress
|
|
42
|
-
});
|
|
43
|
-
module.exports = __toCommonJS(index_exports);
|
|
44
|
-
|
|
45
|
-
// src/core/types.ts
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
//#region src/core/types.ts
|
|
3
|
+
/**
|
|
4
|
+
* Parses an RFC 5322 formatted address string into an EmailAddress.
|
|
5
|
+
* Supports formats such as:
|
|
6
|
+
* - "user@example.com" -> { address: "user@example.com" }
|
|
7
|
+
* - "Sender Name <sender@example.com>" -> { name: "Sender Name", address: "sender@example.com" }
|
|
8
|
+
* - "\"Sender Name\" <sender@example.com>" -> { name: "Sender Name", address: "sender@example.com" }
|
|
9
|
+
*/
|
|
46
10
|
function parseEmailAddress(input) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
return {
|
|
56
|
-
address: match[2].trim(),
|
|
57
|
-
name: match[1]?.trim() || void 0
|
|
58
|
-
};
|
|
11
|
+
if (!input || typeof input !== "string") return { address: "" };
|
|
12
|
+
const trimmed = input.trim();
|
|
13
|
+
const match = trimmed.match(/^(?:["']?([^"']+)["']?\s+)?<?([^\s<>@]+@[^\s<>@]+)>?$/);
|
|
14
|
+
if (!match) return { address: trimmed };
|
|
15
|
+
return {
|
|
16
|
+
address: match[2].trim(),
|
|
17
|
+
name: match[1]?.trim() || void 0
|
|
18
|
+
};
|
|
59
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* Normalizes a Recipient value into a standardized EmailAddress.
|
|
22
|
+
*/
|
|
60
23
|
function normalizeRecipient(recipient) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
return {
|
|
71
|
-
address: (recipient.address ?? "").trim(),
|
|
72
|
-
name: recipient.name?.trim()
|
|
73
|
-
};
|
|
24
|
+
if (typeof recipient === "string") return { address: recipient.trim() };
|
|
25
|
+
if ("email" in recipient) return {
|
|
26
|
+
address: (recipient.email ?? "").trim(),
|
|
27
|
+
name: recipient.name?.trim()
|
|
28
|
+
};
|
|
29
|
+
return {
|
|
30
|
+
address: (recipient.address ?? "").trim(),
|
|
31
|
+
name: recipient.name?.trim()
|
|
32
|
+
};
|
|
74
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* Normalizes a single or array of Recipients into an array of EmailAddress.
|
|
36
|
+
*/
|
|
75
37
|
function normalizeRecipients(recipients) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
return list.map(normalizeRecipient).filter((r) => r.address.length > 0);
|
|
38
|
+
if (!recipients) return [];
|
|
39
|
+
return (Array.isArray(recipients) ? recipients : [recipients]).map(normalizeRecipient).filter((r) => r.address.length > 0);
|
|
79
40
|
}
|
|
80
41
|
|
|
81
|
-
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/core/errors.ts
|
|
44
|
+
/**
|
|
45
|
+
* Base error class for all ts-mailkit errors.
|
|
46
|
+
*/
|
|
82
47
|
var MailKitError = class extends Error {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
48
|
+
constructor(message) {
|
|
49
|
+
super(message);
|
|
50
|
+
this.name = "MailKitError";
|
|
51
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
52
|
+
}
|
|
88
53
|
};
|
|
54
|
+
/**
|
|
55
|
+
* Thrown when transport configuration is missing or invalid.
|
|
56
|
+
*/
|
|
89
57
|
var ConfigurationError = class extends MailKitError {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
58
|
+
constructor(message) {
|
|
59
|
+
super(message);
|
|
60
|
+
this.name = "ConfigurationError";
|
|
61
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
62
|
+
}
|
|
95
63
|
};
|
|
64
|
+
/**
|
|
65
|
+
* Thrown when an email transport fails to dispatch an email.
|
|
66
|
+
*/
|
|
96
67
|
var TransportError = class extends MailKitError {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
68
|
+
transportName;
|
|
69
|
+
statusCode;
|
|
70
|
+
responseBody;
|
|
71
|
+
cause;
|
|
72
|
+
constructor(message, options) {
|
|
73
|
+
super(message);
|
|
74
|
+
this.name = "TransportError";
|
|
75
|
+
this.transportName = options.transportName;
|
|
76
|
+
this.statusCode = options.statusCode;
|
|
77
|
+
this.responseBody = options.responseBody;
|
|
78
|
+
this.cause = options.cause;
|
|
79
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
80
|
+
}
|
|
110
81
|
};
|
|
82
|
+
/**
|
|
83
|
+
* Thrown when template rendering or DTO validation fails.
|
|
84
|
+
*/
|
|
111
85
|
var TemplateError = class extends MailKitError {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
86
|
+
constructor(message) {
|
|
87
|
+
super(message);
|
|
88
|
+
this.name = "TemplateError";
|
|
89
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
90
|
+
}
|
|
117
91
|
};
|
|
118
92
|
|
|
119
|
-
|
|
93
|
+
//#endregion
|
|
94
|
+
//#region src/renderers/utils/html-to-plaintext.ts
|
|
95
|
+
/**
|
|
96
|
+
* Decodes common HTML entities into their plain text equivalents.
|
|
97
|
+
*/
|
|
120
98
|
function decodeHtmlEntities(text) {
|
|
121
|
-
|
|
99
|
+
return text.replace(/ /gi, " ").replace(/&/gi, "&").replace(/</gi, "<").replace(/>/gi, ">").replace(/"/gi, "\"").replace(/'/gi, "'").replace(/©/gi, "©").replace(/®/gi, "®").replace(/™/gi, "™");
|
|
122
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* Strips HTML tags and normalizes whitespace into readable plain text.
|
|
103
|
+
* Converts <br>, <p>, and <div> into appropriate line breaks.
|
|
104
|
+
*/
|
|
123
105
|
function htmlToPlainText(html) {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
html.replace(/<br\s*\/?>/gi, "\n").replace(/<\/p>/gi, "\n\n").replace(/<\/div>/gi, "\n").replace(/<\/h[1-6]>/gi, "\n\n").replace(/<[^>]*>/g, "")
|
|
127
|
-
).replace(/[ \t]+/g, " ").replace(/\n\s*\n\s*\n+/g, "\n\n").trim();
|
|
106
|
+
if (!html) return "";
|
|
107
|
+
return decodeHtmlEntities(html.replace(/<br\s*\/?>/gi, "\n").replace(/<\/p>/gi, "\n\n").replace(/<\/div>/gi, "\n").replace(/<\/h[1-6]>/gi, "\n\n").replace(/<[^>]*>/g, "")).replace(/[ \t]+/g, " ").replace(/\n\s*\n\s*\n+/g, "\n\n").trim();
|
|
128
108
|
}
|
|
129
109
|
|
|
130
|
-
|
|
110
|
+
//#endregion
|
|
111
|
+
//#region src/renderers/default/default-renderer.ts
|
|
112
|
+
/**
|
|
113
|
+
* Validates whether a string is a valid HTTP or HTTPS URL.
|
|
114
|
+
*/
|
|
131
115
|
function isValidHttpUrl(urlString) {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
116
|
+
if (!urlString || typeof urlString !== "string") return false;
|
|
117
|
+
try {
|
|
118
|
+
const url = new URL(urlString.trim());
|
|
119
|
+
return (url.protocol === "http:" || url.protocol === "https:") && url.hostname.length > 0;
|
|
120
|
+
} catch {
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
139
123
|
}
|
|
124
|
+
/**
|
|
125
|
+
* Builds a plain-text fallback string from an email DTO.
|
|
126
|
+
*/
|
|
140
127
|
function buildPlainText(dto) {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
}
|
|
152
|
-
if (dto.otpCode) {
|
|
153
|
-
parts.push(`Verification Code: ${dto.otpCode}`);
|
|
154
|
-
}
|
|
155
|
-
if (dto.actionText && dto.actionUrl) {
|
|
156
|
-
parts.push(`${dto.actionText}: ${dto.actionUrl}`);
|
|
157
|
-
}
|
|
158
|
-
if (dto.extraMessage) {
|
|
159
|
-
parts.push(htmlToPlainText(dto.extraMessage));
|
|
160
|
-
}
|
|
161
|
-
if (dto.footer) {
|
|
162
|
-
parts.push(`${divider}
|
|
163
|
-
${htmlToPlainText(dto.footer)}`);
|
|
164
|
-
}
|
|
165
|
-
return parts.join("\n\n");
|
|
128
|
+
if (dto.noHtmlMessage) return dto.noHtmlMessage;
|
|
129
|
+
const divider = "──────────────────────────────";
|
|
130
|
+
const parts = [];
|
|
131
|
+
if (dto.header) parts.push(dto.header, divider);
|
|
132
|
+
if (dto.body) parts.push(htmlToPlainText(dto.body));
|
|
133
|
+
if (dto.otpCode) parts.push(`Verification Code: ${dto.otpCode}`);
|
|
134
|
+
if (dto.actionText && dto.actionUrl) parts.push(`${dto.actionText}: ${dto.actionUrl}`);
|
|
135
|
+
if (dto.extraMessage) parts.push(htmlToPlainText(dto.extraMessage));
|
|
136
|
+
if (dto.footer) parts.push(`${divider}\n${htmlToPlainText(dto.footer)}`);
|
|
137
|
+
return parts.join("\n\n");
|
|
166
138
|
}
|
|
139
|
+
/**
|
|
140
|
+
* Builds a responsive, styled HTML email document from an email DTO.
|
|
141
|
+
*/
|
|
167
142
|
function buildHtml(dto, options = {}) {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
);
|
|
197
|
-
}
|
|
198
|
-
if (dto.header) {
|
|
199
|
-
parts.push(
|
|
200
|
-
`<h2 style="margin:0 0 16px;font-size:1.5rem;font-weight:700;color:#111827;letter-spacing:-0.025em;">${dto.header}</h2>`
|
|
201
|
-
);
|
|
202
|
-
}
|
|
203
|
-
if (dto.body) {
|
|
204
|
-
parts.push(`<div style="margin:0 0 20px;font-size:1rem;color:#374151;">${dto.body}</div>`);
|
|
205
|
-
}
|
|
206
|
-
if (dto.otpCode) {
|
|
207
|
-
parts.push(
|
|
208
|
-
`<div style="margin:24px 0;padding:20px;background-color:#f3f4f6;border-radius:8px;text-align:center;">`,
|
|
209
|
-
` <span style="font-size:2rem;letter-spacing:0.5rem;font-weight:700;color:#111827;font-family:monospace;">${dto.otpCode}</span>`,
|
|
210
|
-
`</div>`
|
|
211
|
-
);
|
|
212
|
-
}
|
|
213
|
-
if (dto.actionUrl && dto.actionText) {
|
|
214
|
-
parts.push(
|
|
215
|
-
`<div style="margin:24px 0;text-align:center;">`,
|
|
216
|
-
` <a href="${dto.actionUrl}" style="display:inline-block;padding:12px 28px;background-color:${primaryColor};color:#ffffff;border-radius:6px;text-decoration:none;font-weight:600;font-size:1rem;text-align:center;box-sizing:border-box;">${dto.actionText}</a>`,
|
|
217
|
-
`</div>`
|
|
218
|
-
);
|
|
219
|
-
}
|
|
220
|
-
if (dto.extraMessage) {
|
|
221
|
-
parts.push(
|
|
222
|
-
`<div style="margin:20px 0 0;font-size:0.875rem;color:#6b7280;line-height:1.5;">${dto.extraMessage}</div>`
|
|
223
|
-
);
|
|
224
|
-
}
|
|
225
|
-
if (dto.footer) {
|
|
226
|
-
parts.push(
|
|
227
|
-
`<div style="margin-top:32px;padding-top:20px;border-top:1px solid #e5e7eb;font-size:0.75rem;color:#9ca3af;text-align:center;">${dto.footer}</div>`
|
|
228
|
-
);
|
|
229
|
-
}
|
|
230
|
-
parts.push(`</div>`, `</body>`, `</html>`);
|
|
231
|
-
return parts.join("\n");
|
|
143
|
+
const primaryColor = options.primaryColor || "#4f46e5";
|
|
144
|
+
const fontFamily = options.fontFamily || "-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif";
|
|
145
|
+
const maxWidth = options.maxWidth || "600px";
|
|
146
|
+
const parts = [
|
|
147
|
+
`<!DOCTYPE html>`,
|
|
148
|
+
`<html lang="en">`,
|
|
149
|
+
`<head>`,
|
|
150
|
+
`<meta charset="UTF-8">`,
|
|
151
|
+
`<meta name="viewport" content="width=device-width, initial-scale=1.0">`,
|
|
152
|
+
`<title>${dto.subject}</title>`,
|
|
153
|
+
`</head>`,
|
|
154
|
+
`<body style="margin:0;padding:24px 16px;background-color:#f9fafb;font-family:${fontFamily};color:#1f2937;line-height:1.6;">`,
|
|
155
|
+
`<div style="max-width:${maxWidth};margin:0 auto;background-color:#ffffff;padding:32px 28px;border-radius:8px;box-shadow:0 1px 3px rgba(0,0,0,0.1);border:1px solid #f3f4f6;">`
|
|
156
|
+
];
|
|
157
|
+
const logoCandidate = dto.logoUrl ?? options.logoUrl;
|
|
158
|
+
if (logoCandidate && isValidHttpUrl(logoCandidate)) {
|
|
159
|
+
const validLogo = logoCandidate.trim();
|
|
160
|
+
parts.push(`<div style="margin-bottom:24px;text-align:center;">`, ` <img src="${validLogo}" alt="${dto.fromName || "Logo"}" style="max-height:48px;max-width:200px;height:auto;width:auto;display:inline-block;border:0;outline:none;text-decoration:none;" />`, `</div>`);
|
|
161
|
+
}
|
|
162
|
+
if (dto.imageUrl) parts.push(`<div style="margin-bottom:24px;text-align:center;">`, ` <img src="${dto.imageUrl}" alt="Header" style="max-width:100%;height:auto;border-radius:6px;" />`, `</div>`);
|
|
163
|
+
if (dto.header) parts.push(`<h2 style="margin:0 0 16px;font-size:1.5rem;font-weight:700;color:#111827;letter-spacing:-0.025em;">${dto.header}</h2>`);
|
|
164
|
+
if (dto.body) parts.push(`<div style="margin:0 0 20px;font-size:1rem;color:#374151;">${dto.body}</div>`);
|
|
165
|
+
if (dto.otpCode) parts.push(`<div style="margin:24px 0;padding:20px;background-color:#f3f4f6;border-radius:8px;text-align:center;">`, ` <span style="font-size:2rem;letter-spacing:0.5rem;font-weight:700;color:#111827;font-family:monospace;">${dto.otpCode}</span>`, `</div>`);
|
|
166
|
+
if (dto.actionUrl && dto.actionText) parts.push(`<div style="margin:24px 0;text-align:center;">`, ` <a href="${dto.actionUrl}" style="display:inline-block;padding:12px 28px;background-color:${primaryColor};color:#ffffff;border-radius:6px;text-decoration:none;font-weight:600;font-size:1rem;text-align:center;box-sizing:border-box;">${dto.actionText}</a>`, `</div>`);
|
|
167
|
+
if (dto.extraMessage) parts.push(`<div style="margin:20px 0 0;font-size:0.875rem;color:#6b7280;line-height:1.5;">${dto.extraMessage}</div>`);
|
|
168
|
+
if (dto.footer) parts.push(`<div style="margin-top:32px;padding-top:20px;border-top:1px solid #e5e7eb;font-size:0.75rem;color:#9ca3af;text-align:center;">${dto.footer}</div>`);
|
|
169
|
+
parts.push(`</div>`, `</body>`, `</html>`);
|
|
170
|
+
return parts.join("\n");
|
|
232
171
|
}
|
|
172
|
+
/**
|
|
173
|
+
* Default HTML and plain-text email renderer for ts-mailkit.
|
|
174
|
+
*/
|
|
233
175
|
var DefaultEmailRenderer = class {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
176
|
+
options;
|
|
177
|
+
constructor(options = {}) {
|
|
178
|
+
this.options = options;
|
|
179
|
+
}
|
|
180
|
+
render(dto) {
|
|
181
|
+
return {
|
|
182
|
+
html: buildHtml(dto, this.options),
|
|
183
|
+
text: buildPlainText(dto)
|
|
184
|
+
};
|
|
185
|
+
}
|
|
244
186
|
};
|
|
245
187
|
|
|
246
|
-
|
|
188
|
+
//#endregion
|
|
189
|
+
//#region src/core/client.ts
|
|
190
|
+
/**
|
|
191
|
+
* Main email client and orchestration service for ts-mailkit.
|
|
192
|
+
*/
|
|
247
193
|
var MailKit = class {
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
return await this.transport.send({
|
|
332
|
-
...options,
|
|
333
|
-
from,
|
|
334
|
-
text
|
|
335
|
-
});
|
|
336
|
-
}
|
|
194
|
+
transport;
|
|
195
|
+
renderer;
|
|
196
|
+
defaultFrom;
|
|
197
|
+
defaultBrandName;
|
|
198
|
+
brandNameResolver;
|
|
199
|
+
constructor(options) {
|
|
200
|
+
if (!options.transport) throw new ConfigurationError("MailKit: A transport adapter (IEmailSender) is required.");
|
|
201
|
+
this.transport = options.transport;
|
|
202
|
+
this.renderer = options.renderer ?? new DefaultEmailRenderer();
|
|
203
|
+
this.defaultFrom = options.defaultFrom;
|
|
204
|
+
this.defaultBrandName = options.defaultBrandName;
|
|
205
|
+
this.brandNameResolver = options.brandNameResolver;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Returns the underlying transport instance.
|
|
209
|
+
*/
|
|
210
|
+
getTransport() {
|
|
211
|
+
return this.transport;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Renders an email DTO to HTML and plain text without dispatching.
|
|
215
|
+
*/
|
|
216
|
+
async render(template) {
|
|
217
|
+
return await this.renderer.render(template);
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Sends an email using a strongly-typed template DTO.
|
|
221
|
+
*
|
|
222
|
+
* Example:
|
|
223
|
+
* ```ts
|
|
224
|
+
* await mailer.send({
|
|
225
|
+
* to: 'user@example.com',
|
|
226
|
+
* template: myDto,
|
|
227
|
+
* });
|
|
228
|
+
* ```
|
|
229
|
+
*/
|
|
230
|
+
async send(options) {
|
|
231
|
+
const template = options.template;
|
|
232
|
+
const recipientName = options.toName ?? template.userName;
|
|
233
|
+
let resolvedFromName = options.fromName ?? template.fromName;
|
|
234
|
+
if (!resolvedFromName && this.brandNameResolver) resolvedFromName = await this.brandNameResolver();
|
|
235
|
+
if (!resolvedFromName) resolvedFromName = this.defaultBrandName;
|
|
236
|
+
const rendered = await this.renderer.render(template);
|
|
237
|
+
const plaintextBody = template.noHtmlMessage ?? rendered.text;
|
|
238
|
+
let toRecipients;
|
|
239
|
+
if (typeof options.to === "string" && recipientName) toRecipients = {
|
|
240
|
+
address: options.to,
|
|
241
|
+
name: recipientName
|
|
242
|
+
};
|
|
243
|
+
else toRecipients = options.to;
|
|
244
|
+
let from = options.from ?? this.defaultFrom;
|
|
245
|
+
if (resolvedFromName && from) from = {
|
|
246
|
+
address: normalizeRecipient(from).address,
|
|
247
|
+
name: resolvedFromName
|
|
248
|
+
};
|
|
249
|
+
return await this.sendRaw({
|
|
250
|
+
to: toRecipients,
|
|
251
|
+
from,
|
|
252
|
+
subject: template.subject,
|
|
253
|
+
html: rendered.html,
|
|
254
|
+
text: plaintextBody,
|
|
255
|
+
cc: options.cc,
|
|
256
|
+
bcc: options.bcc,
|
|
257
|
+
replyTo: options.replyTo,
|
|
258
|
+
attachments: options.attachments,
|
|
259
|
+
headers: options.headers,
|
|
260
|
+
tags: options.tags,
|
|
261
|
+
metadata: options.metadata
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Dispatches a raw, pre-rendered email directly through the transport.
|
|
266
|
+
* If `options.text` is omitted, plain text is automatically derived from `options.html`.
|
|
267
|
+
*/
|
|
268
|
+
async sendRaw(options) {
|
|
269
|
+
const from = options.from ?? this.defaultFrom;
|
|
270
|
+
const text = options.text ?? (options.html ? htmlToPlainText(options.html) : void 0);
|
|
271
|
+
return await this.transport.send({
|
|
272
|
+
...options,
|
|
273
|
+
from,
|
|
274
|
+
text
|
|
275
|
+
});
|
|
276
|
+
}
|
|
337
277
|
};
|
|
278
|
+
/**
|
|
279
|
+
* Convenience factory to create a MailKit client instance.
|
|
280
|
+
*/
|
|
338
281
|
function createMailKit(options) {
|
|
339
|
-
|
|
282
|
+
return new MailKit(options);
|
|
340
283
|
}
|
|
341
284
|
|
|
342
|
-
|
|
285
|
+
//#endregion
|
|
286
|
+
//#region src/transports/zeptomail/zeptomail-sender.ts
|
|
287
|
+
/**
|
|
288
|
+
* ZeptoMail (Zoho Mail transactional email HTTP API) transport adapter.
|
|
289
|
+
*
|
|
290
|
+
* Designed for zero dependencies and universal edge compatibility (Node.js, Bun, Deno, Cloudflare Workers)
|
|
291
|
+
* by utilizing the standard global `fetch` API.
|
|
292
|
+
*/
|
|
343
293
|
var ZeptoMailHttpSender = class {
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
} catch (err) {
|
|
459
|
-
throw new TransportError(`ZeptoMailHttpSender: Network request failed: ${err instanceof Error ? err.message : String(err)}`, {
|
|
460
|
-
transportName: this.name,
|
|
461
|
-
cause: err
|
|
462
|
-
});
|
|
463
|
-
}
|
|
464
|
-
const responseText = await response.text();
|
|
465
|
-
let responseData;
|
|
466
|
-
try {
|
|
467
|
-
responseData = responseText ? JSON.parse(responseText) : {};
|
|
468
|
-
} catch {
|
|
469
|
-
responseData = { raw: responseText };
|
|
470
|
-
}
|
|
471
|
-
if (!response.ok) {
|
|
472
|
-
const errorMessage = responseData?.error?.message || responseData?.message || `HTTP ${response.status}: ${responseText || response.statusText}`;
|
|
473
|
-
throw new TransportError(`ZeptoMailHttpSender: Failed to send email - ${errorMessage}`, {
|
|
474
|
-
transportName: this.name,
|
|
475
|
-
statusCode: response.status,
|
|
476
|
-
responseBody: responseText
|
|
477
|
-
});
|
|
478
|
-
}
|
|
479
|
-
const messageId = responseData?.data?.[0]?.message_id;
|
|
480
|
-
return {
|
|
481
|
-
success: true,
|
|
482
|
-
messageId,
|
|
483
|
-
response: responseData
|
|
484
|
-
};
|
|
485
|
-
}
|
|
294
|
+
name = "zeptomail";
|
|
295
|
+
apiUrl;
|
|
296
|
+
authHeader;
|
|
297
|
+
defaultFrom;
|
|
298
|
+
fetchImpl;
|
|
299
|
+
constructor(config) {
|
|
300
|
+
if (!config.apiToken || typeof config.apiToken !== "string") throw new ConfigurationError("ZeptoMailHttpSender: Missing or invalid apiToken.");
|
|
301
|
+
if (!config.from) throw new ConfigurationError("ZeptoMailHttpSender: Missing required default from address.");
|
|
302
|
+
const host = (config.apiHost || "api.zeptomail.com").replace(/^https?:\/\//, "").replace(/\/+$/, "");
|
|
303
|
+
this.apiUrl = `https://${host}/v1.1/email`;
|
|
304
|
+
const token = config.apiToken.trim();
|
|
305
|
+
this.authHeader = token.startsWith("Zoho-") ? token : `Zoho-enczapikey ${token}`;
|
|
306
|
+
this.defaultFrom = normalizeRecipient(config.from);
|
|
307
|
+
if (!this.defaultFrom.address) throw new ConfigurationError("ZeptoMailHttpSender: Configured from address must not be empty.");
|
|
308
|
+
const fetchImpl = config.fetch ?? globalThis.fetch;
|
|
309
|
+
if (typeof fetchImpl !== "function") throw new ConfigurationError("ZeptoMailHttpSender: No global fetch implementation found. Pass a custom fetch to ZeptoMailHttpConfig if running on Node < 18.");
|
|
310
|
+
this.fetchImpl = fetchImpl.bind(globalThis);
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Dispatches an email using Zoho ZeptoMail API v1.1.
|
|
314
|
+
*/
|
|
315
|
+
async send(options) {
|
|
316
|
+
const toRecipients = normalizeRecipients(options.to);
|
|
317
|
+
if (toRecipients.length === 0) throw new TransportError("ZeptoMailHttpSender: At least one recipient (to) must be specified.", { transportName: this.name });
|
|
318
|
+
const resolvedFrom = options.from ? normalizeRecipient(options.from) : this.defaultFrom;
|
|
319
|
+
const payload = {
|
|
320
|
+
from: {
|
|
321
|
+
address: resolvedFrom.address,
|
|
322
|
+
name: resolvedFrom.name
|
|
323
|
+
},
|
|
324
|
+
to: toRecipients.map((r) => ({ email_address: {
|
|
325
|
+
address: r.address,
|
|
326
|
+
name: r.name || ""
|
|
327
|
+
} })),
|
|
328
|
+
subject: options.subject,
|
|
329
|
+
htmlbody: options.html,
|
|
330
|
+
textbody: options.text
|
|
331
|
+
};
|
|
332
|
+
if (options.cc) {
|
|
333
|
+
const ccList = normalizeRecipients(options.cc);
|
|
334
|
+
if (ccList.length > 0) payload.cc = ccList.map((r) => ({ email_address: {
|
|
335
|
+
address: r.address,
|
|
336
|
+
name: r.name || ""
|
|
337
|
+
} }));
|
|
338
|
+
}
|
|
339
|
+
if (options.bcc) {
|
|
340
|
+
const bccList = normalizeRecipients(options.bcc);
|
|
341
|
+
if (bccList.length > 0) payload.bcc = bccList.map((r) => ({ email_address: {
|
|
342
|
+
address: r.address,
|
|
343
|
+
name: r.name || ""
|
|
344
|
+
} }));
|
|
345
|
+
}
|
|
346
|
+
if (options.replyTo) {
|
|
347
|
+
const replyToList = normalizeRecipients(options.replyTo);
|
|
348
|
+
if (replyToList.length > 0) payload.reply_to = replyToList.map((r) => ({
|
|
349
|
+
address: r.address,
|
|
350
|
+
name: r.name
|
|
351
|
+
}));
|
|
352
|
+
}
|
|
353
|
+
if (options.attachments && options.attachments.length > 0) payload.attachments = options.attachments.map((att) => {
|
|
354
|
+
let contentBase64;
|
|
355
|
+
if (typeof att.content === "string") contentBase64 = att.content;
|
|
356
|
+
else if (typeof Buffer !== "undefined") contentBase64 = Buffer.from(att.content).toString("base64");
|
|
357
|
+
else {
|
|
358
|
+
let binary = "";
|
|
359
|
+
const bytes = new Uint8Array(att.content);
|
|
360
|
+
for (let i = 0; i < bytes.byteLength; i++) binary += String.fromCharCode(bytes[i]);
|
|
361
|
+
contentBase64 = btoa(binary);
|
|
362
|
+
}
|
|
363
|
+
return {
|
|
364
|
+
name: att.filename,
|
|
365
|
+
content: contentBase64,
|
|
366
|
+
mime_type: att.mimeType || att.contentType || "application/octet-stream",
|
|
367
|
+
cid: att.cid
|
|
368
|
+
};
|
|
369
|
+
});
|
|
370
|
+
let response;
|
|
371
|
+
try {
|
|
372
|
+
response = await this.fetchImpl(this.apiUrl, {
|
|
373
|
+
method: "POST",
|
|
374
|
+
headers: {
|
|
375
|
+
Accept: "application/json",
|
|
376
|
+
"Content-Type": "application/json",
|
|
377
|
+
Authorization: this.authHeader
|
|
378
|
+
},
|
|
379
|
+
body: JSON.stringify(payload)
|
|
380
|
+
});
|
|
381
|
+
} catch (err) {
|
|
382
|
+
throw new TransportError(`ZeptoMailHttpSender: Network request failed: ${err instanceof Error ? err.message : String(err)}`, {
|
|
383
|
+
transportName: this.name,
|
|
384
|
+
cause: err
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
const responseText = await response.text();
|
|
388
|
+
let responseData;
|
|
389
|
+
try {
|
|
390
|
+
responseData = responseText ? JSON.parse(responseText) : {};
|
|
391
|
+
} catch {
|
|
392
|
+
responseData = { raw: responseText };
|
|
393
|
+
}
|
|
394
|
+
if (!response.ok) {
|
|
395
|
+
const errorMessage = responseData?.error?.message || responseData?.message || `HTTP ${response.status}: ${responseText || response.statusText}`;
|
|
396
|
+
throw new TransportError(`ZeptoMailHttpSender: Failed to send email - ${errorMessage}`, {
|
|
397
|
+
transportName: this.name,
|
|
398
|
+
statusCode: response.status,
|
|
399
|
+
responseBody: responseText
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
return {
|
|
403
|
+
success: true,
|
|
404
|
+
messageId: responseData?.data?.[0]?.message_id,
|
|
405
|
+
response: responseData
|
|
406
|
+
};
|
|
407
|
+
}
|
|
486
408
|
};
|
|
487
409
|
|
|
488
|
-
|
|
410
|
+
//#endregion
|
|
411
|
+
//#region src/transports/memory/memory-sender.ts
|
|
412
|
+
/**
|
|
413
|
+
* In-memory email sender for automated testing, local development, and CI environments.
|
|
414
|
+
* Captures all dispatched emails and provides inspection methods.
|
|
415
|
+
*/
|
|
489
416
|
var MemoryEmailSender = class {
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
417
|
+
name = "memory";
|
|
418
|
+
sentMails = [];
|
|
419
|
+
nextId = 1;
|
|
420
|
+
async send(options) {
|
|
421
|
+
this.sentMails.push({ ...options });
|
|
422
|
+
const messageId = `mem-${this.nextId++}-${Date.now()}`;
|
|
423
|
+
return {
|
|
424
|
+
success: true,
|
|
425
|
+
messageId,
|
|
426
|
+
response: { id: messageId }
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* Returns a copy of all emails sent through this transport instance.
|
|
431
|
+
*/
|
|
432
|
+
getSentMails() {
|
|
433
|
+
return [...this.sentMails];
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* Returns the most recent email sent, or undefined if no emails have been sent.
|
|
437
|
+
*/
|
|
438
|
+
getLastMail() {
|
|
439
|
+
return this.sentMails.length > 0 ? { ...this.sentMails[this.sentMails.length - 1] } : void 0;
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* Clears all recorded sent emails.
|
|
443
|
+
*/
|
|
444
|
+
clear() {
|
|
445
|
+
this.sentMails = [];
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* Returns the total count of sent emails.
|
|
449
|
+
*/
|
|
450
|
+
count() {
|
|
451
|
+
return this.sentMails.length;
|
|
452
|
+
}
|
|
526
453
|
};
|
|
527
454
|
|
|
528
|
-
|
|
455
|
+
//#endregion
|
|
456
|
+
//#region src/transports/console/console-sender.ts
|
|
457
|
+
/**
|
|
458
|
+
* Console logger transport that prints dispatched emails to stdout/stderr.
|
|
459
|
+
* Ideal for development and debugging.
|
|
460
|
+
*/
|
|
529
461
|
var ConsoleEmailSender = class {
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
return {
|
|
569
|
-
success: true,
|
|
570
|
-
messageId,
|
|
571
|
-
response: { logged: true }
|
|
572
|
-
};
|
|
573
|
-
}
|
|
462
|
+
name = "console";
|
|
463
|
+
log;
|
|
464
|
+
includeHtml;
|
|
465
|
+
nextId = 1;
|
|
466
|
+
constructor(options = {}) {
|
|
467
|
+
this.log = options.logger ?? console.log;
|
|
468
|
+
this.includeHtml = options.includeHtml ?? false;
|
|
469
|
+
}
|
|
470
|
+
async send(options) {
|
|
471
|
+
const to = normalizeRecipients(options.to).map((r) => r.name ? `${r.name} <${r.address}>` : r.address).join(", ");
|
|
472
|
+
const from = options.from ? JSON.stringify(options.from) : "(default)";
|
|
473
|
+
const messageId = `console-${this.nextId++}-${Date.now()}`;
|
|
474
|
+
const separator = "─".repeat(60);
|
|
475
|
+
const lines = [
|
|
476
|
+
`\n┌${separator}┐`,
|
|
477
|
+
`│ [ts-mailkit:console] DISPATCHED EMAIL (${messageId})`,
|
|
478
|
+
`├${separator}┤`,
|
|
479
|
+
`│ From: ${from}`,
|
|
480
|
+
`│ To: ${to}`,
|
|
481
|
+
`│ Subject: ${options.subject}`,
|
|
482
|
+
`├${separator}┤`
|
|
483
|
+
];
|
|
484
|
+
if (options.text) {
|
|
485
|
+
lines.push("│ Text Body:");
|
|
486
|
+
for (const line of options.text.split("\n")) lines.push(`│ ${line}`);
|
|
487
|
+
}
|
|
488
|
+
if (this.includeHtml && options.html) {
|
|
489
|
+
lines.push("│ HTML Body:");
|
|
490
|
+
for (const line of options.html.split("\n")) lines.push(`│ ${line}`);
|
|
491
|
+
}
|
|
492
|
+
lines.push(`└${separator}┘\n`);
|
|
493
|
+
this.log(lines.join("\n"));
|
|
494
|
+
return {
|
|
495
|
+
success: true,
|
|
496
|
+
messageId,
|
|
497
|
+
response: { logged: true }
|
|
498
|
+
};
|
|
499
|
+
}
|
|
574
500
|
};
|
|
575
501
|
|
|
576
|
-
|
|
577
|
-
|
|
502
|
+
//#endregion
|
|
503
|
+
//#region src/templates/factory.ts
|
|
504
|
+
const DEFAULT_SECURITY_MESSAGE = "If you did not request this, please ignore this email or contact support if you have concerns.";
|
|
578
505
|
function defaultFooter(brandName) {
|
|
579
|
-
|
|
506
|
+
return `© ${(/* @__PURE__ */ new Date()).getFullYear()} ${brandName}. All rights reserved.`;
|
|
580
507
|
}
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
otpCode: otp,
|
|
755
|
-
actionUrl: magicLink || void 0,
|
|
756
|
-
actionText: magicLink ? "Verify Login" : void 0,
|
|
757
|
-
extraMessage: this.defaultSecurityMessage,
|
|
758
|
-
footer: this.buildFooter(brand),
|
|
759
|
-
userName: username,
|
|
760
|
-
fromName: brand,
|
|
761
|
-
noHtmlMessage: htmlToPlainText(body) + `
|
|
762
|
-
|
|
763
|
-
Your verification code is: ${otp}` + plainLink
|
|
764
|
-
};
|
|
765
|
-
}
|
|
766
|
-
/**
|
|
767
|
-
* @deprecated Use `create2FAEmail({ username, otp, magicLink, brandName })` with the Parameter Object pattern instead.
|
|
768
|
-
* Will be removed in the next major release.
|
|
769
|
-
*/
|
|
770
|
-
create2FAEmail_Legacy(username, otp, magicLink = "", brandName) {
|
|
771
|
-
return this.create2FAEmail({ username, otp, magicLink, brandName });
|
|
772
|
-
}
|
|
773
|
-
/**
|
|
774
|
-
* Magic link sign-in email (no OTP, button only).
|
|
775
|
-
*/
|
|
776
|
-
createMagicLinkEmail(params) {
|
|
777
|
-
const { username, magicLink, brandName } = params;
|
|
778
|
-
const brand = this.resolveBrand(brandName);
|
|
779
|
-
const body = `Hi ${username},<br/>Click the button below to sign in. No password needed.`;
|
|
780
|
-
return {
|
|
781
|
-
subject: "Your Magic Link",
|
|
782
|
-
header: `Sign In to ${brand}`,
|
|
783
|
-
body,
|
|
784
|
-
logoUrl: this.defaultLogoUrl,
|
|
785
|
-
actionUrl: magicLink,
|
|
786
|
-
actionText: "Sign In",
|
|
787
|
-
extraMessage: this.defaultSecurityMessage,
|
|
788
|
-
footer: this.buildFooter(brand),
|
|
789
|
-
userName: username,
|
|
790
|
-
fromName: brand,
|
|
791
|
-
noHtmlMessage: htmlToPlainText(body) + `
|
|
792
|
-
|
|
793
|
-
Sign in here:
|
|
794
|
-
${magicLink}`
|
|
795
|
-
};
|
|
796
|
-
}
|
|
797
|
-
/**
|
|
798
|
-
* @deprecated Use `createMagicLinkEmail({ username, magicLink, brandName })` with the Parameter Object pattern instead.
|
|
799
|
-
* Will be removed in the next major release.
|
|
800
|
-
*/
|
|
801
|
-
createMagicLinkEmail_Legacy(username, magicLink, brandName) {
|
|
802
|
-
return this.createMagicLinkEmail({ username, magicLink, brandName });
|
|
803
|
-
}
|
|
804
|
-
// ─── Default Static Instance ──────────────────────────────────────────────
|
|
805
|
-
static _defaultInstance;
|
|
806
|
-
static get defaultInstance() {
|
|
807
|
-
if (!this._defaultInstance) {
|
|
808
|
-
this._defaultInstance = new _EmailTemplateFactory();
|
|
809
|
-
}
|
|
810
|
-
return this._defaultInstance;
|
|
811
|
-
}
|
|
812
|
-
static createEmailVerification(params) {
|
|
813
|
-
return this.defaultInstance.createEmailVerification(params);
|
|
814
|
-
}
|
|
815
|
-
/**
|
|
816
|
-
* @deprecated Use `createEmailVerification(params: EmailVerificationParams)` instead.
|
|
817
|
-
* Will be removed in the next major release.
|
|
818
|
-
*/
|
|
819
|
-
static createEmailVerification_Legacy(username, verificationUrl, otp, brandName) {
|
|
820
|
-
return this.defaultInstance.createEmailVerification({ username, verificationUrl, otp, brandName });
|
|
821
|
-
}
|
|
822
|
-
static createPasswordReset(params) {
|
|
823
|
-
return this.defaultInstance.createPasswordReset(params);
|
|
824
|
-
}
|
|
825
|
-
/**
|
|
826
|
-
* @deprecated Use `createPasswordReset(params: PasswordResetParams)` instead.
|
|
827
|
-
* Will be removed in the next major release.
|
|
828
|
-
*/
|
|
829
|
-
static createPasswordReset_Legacy(username, resetUrl, otp, brandName) {
|
|
830
|
-
return this.defaultInstance.createPasswordReset({ username, resetUrl, otp, brandName });
|
|
831
|
-
}
|
|
832
|
-
static createSimpleMessage(params) {
|
|
833
|
-
return this.defaultInstance.createSimpleMessage(params);
|
|
834
|
-
}
|
|
835
|
-
/**
|
|
836
|
-
* @deprecated Use `createSimpleMessage(params: SimpleMessageParams)` instead.
|
|
837
|
-
* Will be removed in the next major release.
|
|
838
|
-
*/
|
|
839
|
-
static createSimpleMessage_Legacy(subject, header, body, brandName) {
|
|
840
|
-
return this.defaultInstance.createSimpleMessage({ subject, header, body, brandName });
|
|
841
|
-
}
|
|
842
|
-
static createNewsLetter(params) {
|
|
843
|
-
return this.defaultInstance.createNewsLetter(params);
|
|
844
|
-
}
|
|
845
|
-
/**
|
|
846
|
-
* @deprecated Use `createNewsLetter(params: NewsletterParams)` instead.
|
|
847
|
-
* Will be removed in the next major release.
|
|
848
|
-
*/
|
|
849
|
-
static createNewsLetter_Legacy(subject, header, body, imageUrl, actionUrl, actionText, brandName, logoUrl) {
|
|
850
|
-
return this.defaultInstance.createNewsLetter({
|
|
851
|
-
subject,
|
|
852
|
-
header,
|
|
853
|
-
body,
|
|
854
|
-
imageUrl,
|
|
855
|
-
actionUrl,
|
|
856
|
-
actionText,
|
|
857
|
-
brandName,
|
|
858
|
-
logoUrl
|
|
859
|
-
});
|
|
860
|
-
}
|
|
861
|
-
static create2FAEmail(params) {
|
|
862
|
-
return this.defaultInstance.create2FAEmail(params);
|
|
863
|
-
}
|
|
864
|
-
/**
|
|
865
|
-
* @deprecated Use `create2FAEmail(params: TwoFactorParams)` instead.
|
|
866
|
-
* Will be removed in the next major release.
|
|
867
|
-
*/
|
|
868
|
-
static create2FAEmail_Legacy(username, otp, magicLink, brandName) {
|
|
869
|
-
return this.defaultInstance.create2FAEmail({ username, otp, magicLink, brandName });
|
|
870
|
-
}
|
|
871
|
-
static createMagicLinkEmail(params) {
|
|
872
|
-
return this.defaultInstance.createMagicLinkEmail(params);
|
|
873
|
-
}
|
|
874
|
-
/**
|
|
875
|
-
* @deprecated Use `createMagicLinkEmail(params: MagicLinkParams)` instead.
|
|
876
|
-
* Will be removed in the next major release.
|
|
877
|
-
*/
|
|
878
|
-
static createMagicLinkEmail_Legacy(username, magicLink, brandName) {
|
|
879
|
-
return this.defaultInstance.createMagicLinkEmail({ username, magicLink, brandName });
|
|
880
|
-
}
|
|
508
|
+
/**
|
|
509
|
+
* Factory for creating pre-populated, type-safe email template DTOs.
|
|
510
|
+
*/
|
|
511
|
+
var EmailTemplateFactory = class EmailTemplateFactory {
|
|
512
|
+
defaultBrandName;
|
|
513
|
+
defaultSecurityMessage;
|
|
514
|
+
footerResolver;
|
|
515
|
+
defaultLogoUrl;
|
|
516
|
+
constructor(config = {}) {
|
|
517
|
+
this.defaultBrandName = config.brandName || "Application";
|
|
518
|
+
this.defaultSecurityMessage = config.securityMessage || DEFAULT_SECURITY_MESSAGE;
|
|
519
|
+
this.defaultLogoUrl = config.logoUrl;
|
|
520
|
+
if (typeof config.footer === "function") this.footerResolver = config.footer;
|
|
521
|
+
else if (typeof config.footer === "string") {
|
|
522
|
+
const fixedFooter = config.footer;
|
|
523
|
+
this.footerResolver = () => fixedFooter;
|
|
524
|
+
} else this.footerResolver = defaultFooter;
|
|
525
|
+
}
|
|
526
|
+
resolveBrand(override) {
|
|
527
|
+
return override || this.defaultBrandName;
|
|
528
|
+
}
|
|
529
|
+
buildFooter(brandName) {
|
|
530
|
+
return this.footerResolver(brandName);
|
|
531
|
+
}
|
|
532
|
+
/**
|
|
533
|
+
* Welcome / email-verification email sent after registration.
|
|
534
|
+
*/
|
|
535
|
+
createEmailVerification(params) {
|
|
536
|
+
const { username, verificationUrl, otp, brandName } = params;
|
|
537
|
+
const brand = this.resolveBrand(brandName);
|
|
538
|
+
const body = `Hi ${username},<br/>Thank you for signing up. Use the code below or click the button to verify your email address.`;
|
|
539
|
+
return {
|
|
540
|
+
subject: "Verify Your Email Address",
|
|
541
|
+
header: `Welcome to ${brand}!`,
|
|
542
|
+
body,
|
|
543
|
+
logoUrl: this.defaultLogoUrl,
|
|
544
|
+
otpCode: otp,
|
|
545
|
+
actionUrl: verificationUrl,
|
|
546
|
+
actionText: "Verify Email",
|
|
547
|
+
extraMessage: this.defaultSecurityMessage,
|
|
548
|
+
footer: this.buildFooter(brand),
|
|
549
|
+
userName: username,
|
|
550
|
+
fromName: brand,
|
|
551
|
+
noHtmlMessage: htmlToPlainText(body) + `\n\nYour verification code is: ${otp}\n\nAlternatively, verify here:\n${verificationUrl}`
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
/**
|
|
555
|
+
* Password-reset email with an optional OTP code and action link.
|
|
556
|
+
*/
|
|
557
|
+
createPasswordReset(params) {
|
|
558
|
+
const { username, resetUrl, otp = "", brandName } = params;
|
|
559
|
+
const brand = this.resolveBrand(brandName);
|
|
560
|
+
const body = `Hi ${username},<br/>We received a request to reset your password. Use the OTP code below or click the button to proceed.`;
|
|
561
|
+
const plainTextOtp = otp ? `\n\nYour OTP code is: ${otp}` : "";
|
|
562
|
+
return {
|
|
563
|
+
subject: "Password Reset Request",
|
|
564
|
+
header: "Reset Your Password",
|
|
565
|
+
body,
|
|
566
|
+
logoUrl: this.defaultLogoUrl,
|
|
567
|
+
otpCode: otp || void 0,
|
|
568
|
+
actionUrl: resetUrl,
|
|
569
|
+
actionText: "Reset Password",
|
|
570
|
+
extraMessage: this.defaultSecurityMessage,
|
|
571
|
+
footer: this.buildFooter(brand),
|
|
572
|
+
userName: username,
|
|
573
|
+
fromName: brand,
|
|
574
|
+
noHtmlMessage: htmlToPlainText(body) + plainTextOtp + `\n\nAlternatively, you can use the following link to reset your password:\n${resetUrl}`
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
/**
|
|
578
|
+
* Generic plain message email (no action button, no OTP).
|
|
579
|
+
*/
|
|
580
|
+
createSimpleMessage(params) {
|
|
581
|
+
const { subject, header, body, brandName } = params;
|
|
582
|
+
const brand = this.resolveBrand(brandName);
|
|
583
|
+
return {
|
|
584
|
+
subject,
|
|
585
|
+
header,
|
|
586
|
+
body,
|
|
587
|
+
logoUrl: this.defaultLogoUrl,
|
|
588
|
+
footer: this.buildFooter(brand),
|
|
589
|
+
fromName: brand,
|
|
590
|
+
noHtmlMessage: htmlToPlainText(body)
|
|
591
|
+
};
|
|
592
|
+
}
|
|
593
|
+
/**
|
|
594
|
+
* Newsletter / announcement email with an optional hero image and CTA button.
|
|
595
|
+
*/
|
|
596
|
+
createNewsLetter(params) {
|
|
597
|
+
const { subject, header, body, imageUrl, actionUrl, actionText, brandName, logoUrl } = params;
|
|
598
|
+
const brand = this.resolveBrand(brandName);
|
|
599
|
+
let noHtmlMessage = htmlToPlainText(body);
|
|
600
|
+
if (actionUrl) noHtmlMessage += `\n\n${actionText || "Learn more"}: ${actionUrl}`;
|
|
601
|
+
return {
|
|
602
|
+
subject,
|
|
603
|
+
header,
|
|
604
|
+
body,
|
|
605
|
+
logoUrl: logoUrl || this.defaultLogoUrl,
|
|
606
|
+
imageUrl,
|
|
607
|
+
actionUrl,
|
|
608
|
+
actionText,
|
|
609
|
+
footer: this.buildFooter(brand),
|
|
610
|
+
fromName: brand,
|
|
611
|
+
noHtmlMessage
|
|
612
|
+
};
|
|
613
|
+
}
|
|
614
|
+
/**
|
|
615
|
+
* Two-factor authentication email with OTP code and optional magic link.
|
|
616
|
+
*/
|
|
617
|
+
create2FAEmail(params) {
|
|
618
|
+
const { username, otp, magicLink = "", brandName } = params;
|
|
619
|
+
const brand = this.resolveBrand(brandName);
|
|
620
|
+
const body = `Hi ${username},<br/>Use the code below or click the button to complete your login.`;
|
|
621
|
+
const plainLink = magicLink ? `\n\nAlternatively, verify here:\n${magicLink}` : "";
|
|
622
|
+
return {
|
|
623
|
+
subject: "Your Login Verification Code",
|
|
624
|
+
header: "Two-Factor Authentication",
|
|
625
|
+
body,
|
|
626
|
+
logoUrl: this.defaultLogoUrl,
|
|
627
|
+
otpCode: otp,
|
|
628
|
+
actionUrl: magicLink || void 0,
|
|
629
|
+
actionText: magicLink ? "Verify Login" : void 0,
|
|
630
|
+
extraMessage: this.defaultSecurityMessage,
|
|
631
|
+
footer: this.buildFooter(brand),
|
|
632
|
+
userName: username,
|
|
633
|
+
fromName: brand,
|
|
634
|
+
noHtmlMessage: htmlToPlainText(body) + `\n\nYour verification code is: ${otp}` + plainLink
|
|
635
|
+
};
|
|
636
|
+
}
|
|
637
|
+
/**
|
|
638
|
+
* Magic link sign-in email (no OTP, button only).
|
|
639
|
+
*/
|
|
640
|
+
createMagicLinkEmail(params) {
|
|
641
|
+
const { username, magicLink, brandName } = params;
|
|
642
|
+
const brand = this.resolveBrand(brandName);
|
|
643
|
+
const body = `Hi ${username},<br/>Click the button below to sign in. No password needed.`;
|
|
644
|
+
return {
|
|
645
|
+
subject: "Your Magic Link",
|
|
646
|
+
header: `Sign In to ${brand}`,
|
|
647
|
+
body,
|
|
648
|
+
logoUrl: this.defaultLogoUrl,
|
|
649
|
+
actionUrl: magicLink,
|
|
650
|
+
actionText: "Sign In",
|
|
651
|
+
extraMessage: this.defaultSecurityMessage,
|
|
652
|
+
footer: this.buildFooter(brand),
|
|
653
|
+
userName: username,
|
|
654
|
+
fromName: brand,
|
|
655
|
+
noHtmlMessage: htmlToPlainText(body) + `\n\nSign in here:\n${magicLink}`
|
|
656
|
+
};
|
|
657
|
+
}
|
|
658
|
+
static _defaultInstance;
|
|
659
|
+
static get defaultInstance() {
|
|
660
|
+
if (!this._defaultInstance) this._defaultInstance = new EmailTemplateFactory();
|
|
661
|
+
return this._defaultInstance;
|
|
662
|
+
}
|
|
663
|
+
static createEmailVerification(params) {
|
|
664
|
+
return this.defaultInstance.createEmailVerification(params);
|
|
665
|
+
}
|
|
666
|
+
static createPasswordReset(params) {
|
|
667
|
+
return this.defaultInstance.createPasswordReset(params);
|
|
668
|
+
}
|
|
669
|
+
static createSimpleMessage(params) {
|
|
670
|
+
return this.defaultInstance.createSimpleMessage(params);
|
|
671
|
+
}
|
|
672
|
+
static createNewsLetter(params) {
|
|
673
|
+
return this.defaultInstance.createNewsLetter(params);
|
|
674
|
+
}
|
|
675
|
+
static create2FAEmail(params) {
|
|
676
|
+
return this.defaultInstance.create2FAEmail(params);
|
|
677
|
+
}
|
|
678
|
+
static createMagicLinkEmail(params) {
|
|
679
|
+
return this.defaultInstance.createMagicLinkEmail(params);
|
|
680
|
+
}
|
|
881
681
|
};
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
});
|
|
682
|
+
|
|
683
|
+
//#endregion
|
|
684
|
+
exports.ConfigurationError = ConfigurationError;
|
|
685
|
+
exports.ConsoleEmailSender = ConsoleEmailSender;
|
|
686
|
+
exports.DefaultEmailRenderer = DefaultEmailRenderer;
|
|
687
|
+
exports.EmailTemplateFactory = EmailTemplateFactory;
|
|
688
|
+
exports.MailKit = MailKit;
|
|
689
|
+
exports.MailKitError = MailKitError;
|
|
690
|
+
exports.MemoryEmailSender = MemoryEmailSender;
|
|
691
|
+
exports.TemplateError = TemplateError;
|
|
692
|
+
exports.TransportError = TransportError;
|
|
693
|
+
exports.ZeptoMailHttpSender = ZeptoMailHttpSender;
|
|
694
|
+
exports.buildHtml = buildHtml;
|
|
695
|
+
exports.buildPlainText = buildPlainText;
|
|
696
|
+
exports.createMailKit = createMailKit;
|
|
697
|
+
exports.decodeHtmlEntities = decodeHtmlEntities;
|
|
698
|
+
exports.htmlToPlainText = htmlToPlainText;
|
|
699
|
+
exports.isValidHttpUrl = isValidHttpUrl;
|
|
700
|
+
exports.normalizeRecipient = normalizeRecipient;
|
|
701
|
+
exports.normalizeRecipients = normalizeRecipients;
|
|
702
|
+
exports.parseEmailAddress = parseEmailAddress;
|
|
904
703
|
//# sourceMappingURL=index.cjs.map
|