@stamhoofd/email 2.118.1 → 2.120.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/dist/classes/Email.d.ts +4 -4
- package/dist/classes/Email.d.ts.map +1 -1
- package/dist/classes/Email.js +36 -39
- package/dist/classes/Email.js.map +1 -1
- package/dist/classes/EmailMocker.d.ts +1 -1
- package/dist/classes/EmailMocker.d.ts.map +1 -1
- package/dist/classes/EmailMocker.js +12 -49
- package/dist/classes/EmailMocker.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -6
- package/dist/index.js.map +1 -1
- package/dist/models/EmailAddress.d.ts +1 -1
- package/dist/models/EmailAddress.d.ts.map +1 -1
- package/dist/models/EmailAddress.js +34 -38
- package/dist/models/EmailAddress.js.map +1 -1
- package/package.json +9 -6
- package/dist/classes/Email.test.d.ts +0 -2
- package/dist/classes/Email.test.d.ts.map +0 -1
- package/dist/classes/Email.test.js +0 -27
- package/dist/classes/Email.test.js.map +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/src/classes/Email.test.ts +0 -28
- package/src/classes/Email.ts +0 -546
- package/src/classes/EmailMocker.ts +0 -145
- package/src/index.ts +0 -3
- package/src/migrations/1603433451-email-addresses.sql +0 -14
- package/src/migrations/1720080973-convert-charset.sql +0 -5
- package/src/models/EmailAddress.ts +0 -160
- /package/{dist/migrations → migrations}/1603433451-email-addresses.sql +0 -0
- /package/{dist/migrations → migrations}/1720080973-convert-charset.sql +0 -0
package/src/classes/Email.ts
DELETED
|
@@ -1,546 +0,0 @@
|
|
|
1
|
-
import { SimpleError } from '@simonbackx/simple-errors';
|
|
2
|
-
import { I18n } from '@stamhoofd/backend-i18n';
|
|
3
|
-
import { Country, Language } from '@stamhoofd/structures';
|
|
4
|
-
import { DataValidator, Formatter, sleep } from '@stamhoofd/utility';
|
|
5
|
-
import htmlToText from 'html-to-text';
|
|
6
|
-
import nodemailer from 'nodemailer';
|
|
7
|
-
import Mail from 'nodemailer/lib/mailer';
|
|
8
|
-
import { EmailAddress } from '../models/EmailAddress';
|
|
9
|
-
|
|
10
|
-
export type EmailInterfaceRecipient = {
|
|
11
|
-
name?: string | null;
|
|
12
|
-
email: string;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export type EmailInterfaceBase = {
|
|
16
|
-
to: EmailInterfaceRecipient[];
|
|
17
|
-
bcc?: EmailInterfaceRecipient[];
|
|
18
|
-
replyTo?: EmailInterfaceRecipient;
|
|
19
|
-
subject: string;
|
|
20
|
-
text?: string;
|
|
21
|
-
html?: string;
|
|
22
|
-
attachments?: { filename: string; path?: string; href?: string; content?: string | Buffer; contentType?: string; encoding?: string }[];
|
|
23
|
-
retryCount?: number;
|
|
24
|
-
type?: 'transactional' | 'broadcast';
|
|
25
|
-
headers?: Record<string, string> | null;
|
|
26
|
-
callback?: (error: Error | null) => void;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export type EmailInterface = EmailInterfaceBase & {
|
|
30
|
-
from: EmailInterfaceRecipient;
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
/// An email builder is called until it returns undefined. This allows to reduce memory usage for an e-mail with multiple recipients
|
|
34
|
-
export type EmailBuilder = () => EmailInterface | undefined;
|
|
35
|
-
|
|
36
|
-
export type InternalEmailData = {
|
|
37
|
-
from: string;
|
|
38
|
-
bcc: string | undefined;
|
|
39
|
-
replyTo: string | undefined;
|
|
40
|
-
to: string;
|
|
41
|
-
subject: string;
|
|
42
|
-
text?: string;
|
|
43
|
-
html?: string;
|
|
44
|
-
attachments?: { filename: string; path?: string; href?: string; content?: string | Buffer; contentType?: string }[];
|
|
45
|
-
headers?: Record<string, string>;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
function emailObjectToString(email: null | undefined): undefined;
|
|
49
|
-
function emailObjectToString(email: EmailInterfaceRecipient): string;
|
|
50
|
-
function emailObjectToString(email: EmailInterfaceRecipient | null | undefined): string | undefined {
|
|
51
|
-
if (!email) {
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
if (email.name) {
|
|
55
|
-
const cleaned = Formatter.emailSenderName(email.name);
|
|
56
|
-
if (cleaned.length < 2) {
|
|
57
|
-
return email.email;
|
|
58
|
-
}
|
|
59
|
-
return '"' + email.name.replaceAll('"', '') + '" <' + email.email + '>';
|
|
60
|
-
}
|
|
61
|
-
return email.email;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function emailObjectsToString(emails: EmailInterfaceRecipient[]): string | undefined {
|
|
65
|
-
if (emails.length === 0) {
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
return emails.map(emailObjectToString).join(', ');
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
class EmailStatic {
|
|
72
|
-
transporter: Mail;
|
|
73
|
-
transactionalTransporter: Mail;
|
|
74
|
-
rps = 14;
|
|
75
|
-
|
|
76
|
-
currentQueue: EmailBuilder[] = [];
|
|
77
|
-
sending = false;
|
|
78
|
-
|
|
79
|
-
setupIfNeeded() {
|
|
80
|
-
if (this.transporter) {
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
if (STAMHOOFD.environment === 'test') {
|
|
85
|
-
throw new Error('When using Email in tests, make sure to use EmailMocker.infect() in jest.setup.ts');
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
if (!STAMHOOFD.SMTP_HOST || !STAMHOOFD.SMTP_PORT) {
|
|
89
|
-
throw new Error('Missing environment variables to send emails');
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// create reusable transporter object using the default SMTP transport
|
|
93
|
-
this.transporter = nodemailer.createTransport({
|
|
94
|
-
pool: true,
|
|
95
|
-
host: STAMHOOFD.SMTP_HOST,
|
|
96
|
-
port: STAMHOOFD.SMTP_PORT,
|
|
97
|
-
auth: {
|
|
98
|
-
user: STAMHOOFD.SMTP_USERNAME, // generated ethereal user
|
|
99
|
-
pass: STAMHOOFD.SMTP_PASSWORD, // generated ethereal password
|
|
100
|
-
},
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
// create reusable transporter object using the default SMTP transport
|
|
104
|
-
this.transactionalTransporter = nodemailer.createTransport({
|
|
105
|
-
pool: true,
|
|
106
|
-
host: STAMHOOFD.TRANSACTIONAL_SMTP_HOST,
|
|
107
|
-
port: STAMHOOFD.TRANSACTIONAL_SMTP_PORT,
|
|
108
|
-
auth: {
|
|
109
|
-
user: STAMHOOFD.TRANSACTIONAL_SMTP_USERNAME, // generated ethereal user
|
|
110
|
-
pass: STAMHOOFD.TRANSACTIONAL_SMTP_PASSWORD, // generated ethereal password
|
|
111
|
-
},
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
// verify connection configuration
|
|
115
|
-
this.transporter.verify((error) => {
|
|
116
|
-
if (error) {
|
|
117
|
-
console.error('SMTP server not working', error);
|
|
118
|
-
}
|
|
119
|
-
else {
|
|
120
|
-
console.log('SMTP server is ready to take our messages');
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
// verify connection configuration
|
|
125
|
-
this.transactionalTransporter.verify((error) => {
|
|
126
|
-
if (error) {
|
|
127
|
-
console.error('Transactinoal SMTP server not working', error);
|
|
128
|
-
}
|
|
129
|
-
else {
|
|
130
|
-
console.log('Transactinoal SMTP server is ready to take our messages');
|
|
131
|
-
}
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
private sendNextIfNeeded() {
|
|
136
|
-
if (!this.sending) {
|
|
137
|
-
if (this.currentQueue.length == 0) {
|
|
138
|
-
console.log('mail queue is empty');
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
let next = this.currentQueue[0]();
|
|
142
|
-
|
|
143
|
-
while (next === undefined) {
|
|
144
|
-
this.currentQueue.shift();
|
|
145
|
-
if (this.currentQueue.length == 0) {
|
|
146
|
-
console.log('mail queue is empty');
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
next = this.currentQueue[0]();
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
this.sending = true;
|
|
153
|
-
this.doSend(next).catch((e) => {
|
|
154
|
-
console.error(e);
|
|
155
|
-
if (next.callback) {
|
|
156
|
-
next.callback(e);
|
|
157
|
-
}
|
|
158
|
-
}).finally(() => {
|
|
159
|
-
this.sending = false;
|
|
160
|
-
this.sendNextIfNeeded();
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
parseTo(to: string | EmailInterfaceRecipient[]): EmailInterfaceRecipient[] {
|
|
166
|
-
if (typeof to === 'string') {
|
|
167
|
-
return this.parseEmailStr(to).map(email => ({ email }));
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
// Filter invalid email addresses
|
|
171
|
-
return to.filter(r => DataValidator.isEmailValid(r.email));
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Get the raw email
|
|
176
|
-
*/
|
|
177
|
-
parseEmailStr(emailStr: string): string[] {
|
|
178
|
-
let insideQuote = false;
|
|
179
|
-
let escaped = false;
|
|
180
|
-
let inAddr = false;
|
|
181
|
-
let email = '';
|
|
182
|
-
let didFindAddr = false;
|
|
183
|
-
let cleanedStr = '';
|
|
184
|
-
|
|
185
|
-
const addresses: string[] = [];
|
|
186
|
-
|
|
187
|
-
function endAddress() {
|
|
188
|
-
let m: string;
|
|
189
|
-
if (didFindAddr) {
|
|
190
|
-
m = email.trim();
|
|
191
|
-
}
|
|
192
|
-
else {
|
|
193
|
-
m = cleanedStr.trim();
|
|
194
|
-
}
|
|
195
|
-
if (DataValidator.isEmailValid(m)) {
|
|
196
|
-
addresses.push(m);
|
|
197
|
-
}
|
|
198
|
-
didFindAddr = false;
|
|
199
|
-
email = '';
|
|
200
|
-
inAddr = false;
|
|
201
|
-
insideQuote = false;
|
|
202
|
-
escaped = false;
|
|
203
|
-
cleanedStr = '';
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
// eslint-disable-next-line @typescript-eslint/prefer-for-of
|
|
207
|
-
for (let index = 0; index < emailStr.length; index++) {
|
|
208
|
-
const shouldEscape = escaped;
|
|
209
|
-
if (escaped) {
|
|
210
|
-
escaped = false;
|
|
211
|
-
}
|
|
212
|
-
const character = emailStr[index];
|
|
213
|
-
if (insideQuote) {
|
|
214
|
-
if (character === '\\') {
|
|
215
|
-
escaped = true;
|
|
216
|
-
continue;
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
if (!shouldEscape) {
|
|
221
|
-
if (character === '"') {
|
|
222
|
-
if (insideQuote) {
|
|
223
|
-
insideQuote = false;
|
|
224
|
-
continue;
|
|
225
|
-
}
|
|
226
|
-
insideQuote = true;
|
|
227
|
-
continue;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
if (!insideQuote) {
|
|
231
|
-
if (character === '<') {
|
|
232
|
-
inAddr = true;
|
|
233
|
-
continue;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
if (character === '>') {
|
|
237
|
-
inAddr = false;
|
|
238
|
-
didFindAddr = true;
|
|
239
|
-
continue;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
if (character === ',') {
|
|
243
|
-
// End previous address
|
|
244
|
-
endAddress();
|
|
245
|
-
continue;
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
if (inAddr) {
|
|
251
|
-
email += character;
|
|
252
|
-
}
|
|
253
|
-
cleanedStr += character;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
endAddress();
|
|
257
|
-
return addresses;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
private matchWhitelist(email: string, whitelist: string[]) {
|
|
261
|
-
if (!whitelist.includes('*') && !whitelist.includes('*@*')) {
|
|
262
|
-
const l = email.toLowerCase();
|
|
263
|
-
if (whitelist.includes(l)) {
|
|
264
|
-
return true;
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
const domainIndex = l.indexOf('@');
|
|
268
|
-
const domain = l.substring(domainIndex);
|
|
269
|
-
|
|
270
|
-
if (whitelist.includes('*' + domain)) {
|
|
271
|
-
return true;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
if (STAMHOOFD.environment === 'production') {
|
|
275
|
-
console.warn('Filtered email ' + l + ': not whitelisted');
|
|
276
|
-
}
|
|
277
|
-
return false;
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
return true;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
private filterWhitelist(recipients: EmailInterfaceRecipient[], whitelist: string[]) {
|
|
284
|
-
if (!whitelist.includes('*') && !whitelist.includes('*@*')) {
|
|
285
|
-
return recipients.filter((mail) => {
|
|
286
|
-
return this.matchWhitelist(mail.email, whitelist);
|
|
287
|
-
});
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
return recipients;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
private async doSend(data: EmailInterface) {
|
|
294
|
-
let recipients = data.to.filter(({ email }) => DataValidator.isEmailValid(email));
|
|
295
|
-
|
|
296
|
-
// Check if this email is not marked as spam
|
|
297
|
-
// Filter recipients if bounced or spam
|
|
298
|
-
if (recipients.length === 0) {
|
|
299
|
-
// Invalid string
|
|
300
|
-
console.warn('No recipients for email');
|
|
301
|
-
|
|
302
|
-
try {
|
|
303
|
-
data.callback?.(
|
|
304
|
-
new SimpleError({
|
|
305
|
-
code: 'invalid_email_address',
|
|
306
|
-
message: 'Invalid email address',
|
|
307
|
-
human: $t(`cbbff442-758c-4f76-b8c2-26bb176fefcc`),
|
|
308
|
-
}),
|
|
309
|
-
);
|
|
310
|
-
}
|
|
311
|
-
catch (e) {
|
|
312
|
-
console.error('Error in email callback', e);
|
|
313
|
-
}
|
|
314
|
-
return;
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
// Check spam and bounces
|
|
318
|
-
recipients = await EmailAddress.filterSendTo(recipients);
|
|
319
|
-
|
|
320
|
-
if (recipients.length === 0) {
|
|
321
|
-
// Invalid string
|
|
322
|
-
console.warn("Filtered all emails due hard bounce or spam '" + data.to + "'. E-mail skipped");
|
|
323
|
-
|
|
324
|
-
try {
|
|
325
|
-
data.callback?.(
|
|
326
|
-
new SimpleError({
|
|
327
|
-
code: 'all_filtered',
|
|
328
|
-
message: 'All recipients are filtered due to hard bounce or spam',
|
|
329
|
-
human: data.to.length > 1 ? $t(`e3c3f519-562e-4ef4-b670-599ce4cb74ac`) : $t('212d39e4-8da5-4096-84bb-bd7fadc192fc'),
|
|
330
|
-
}),
|
|
331
|
-
);
|
|
332
|
-
}
|
|
333
|
-
catch (e) {
|
|
334
|
-
console.error('Error in email callback', e);
|
|
335
|
-
}
|
|
336
|
-
return;
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
// Filter by environment
|
|
340
|
-
if (STAMHOOFD.environment !== 'production' || (STAMHOOFD.WHITELISTED_EMAIL_DESTINATIONS && STAMHOOFD.WHITELISTED_EMAIL_DESTINATIONS.length > 0)) {
|
|
341
|
-
const whitelist = STAMHOOFD.WHITELISTED_EMAIL_DESTINATIONS ?? [];
|
|
342
|
-
recipients = this.filterWhitelist(recipients, whitelist);
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
const to = emailObjectsToString(recipients);
|
|
346
|
-
|
|
347
|
-
if (!to || recipients.length === 0) {
|
|
348
|
-
// Invalid string
|
|
349
|
-
try {
|
|
350
|
-
data.callback?.(
|
|
351
|
-
new SimpleError({
|
|
352
|
-
code: 'email_skipped_whitelist',
|
|
353
|
-
message: 'All recipients are filtered due to environment',
|
|
354
|
-
human: data.to.length > 1 ? $t(`462d5e22-af11-40de-9e16-eda1b93ac0c7`) : $t('e2eeb4a3-2c32-4ba2-b991-3e139402225f'),
|
|
355
|
-
}),
|
|
356
|
-
);
|
|
357
|
-
}
|
|
358
|
-
catch (e) {
|
|
359
|
-
console.error('Error in email callback', e);
|
|
360
|
-
}
|
|
361
|
-
return;
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
// Clean bcc
|
|
365
|
-
let bccRecipients: EmailInterfaceRecipient[] = [];
|
|
366
|
-
if (data.bcc) {
|
|
367
|
-
// Filter
|
|
368
|
-
bccRecipients.push(...(await EmailAddress.filterSendTo(data.bcc.filter(({ email }) => DataValidator.isEmailValid(email)))));
|
|
369
|
-
|
|
370
|
-
// Filter by environment
|
|
371
|
-
if (STAMHOOFD.environment !== 'production' || (STAMHOOFD.WHITELISTED_EMAIL_DESTINATIONS && STAMHOOFD.WHITELISTED_EMAIL_DESTINATIONS.length > 0)) {
|
|
372
|
-
const whitelist = STAMHOOFD.WHITELISTED_EMAIL_DESTINATIONS ?? [];
|
|
373
|
-
bccRecipients = this.filterWhitelist(bccRecipients, whitelist);
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
this.setupIfNeeded();
|
|
377
|
-
|
|
378
|
-
// send mail with defined transport object
|
|
379
|
-
const mail: InternalEmailData = {
|
|
380
|
-
from: emailObjectToString(data.from), // sender address
|
|
381
|
-
bcc: emailObjectsToString(bccRecipients),
|
|
382
|
-
replyTo: data.replyTo ? emailObjectToString(data.replyTo) : undefined,
|
|
383
|
-
to,
|
|
384
|
-
subject: data.subject.substring(0, 1000), // Subject line
|
|
385
|
-
text: data.text, // plain text body
|
|
386
|
-
};
|
|
387
|
-
|
|
388
|
-
if (data.attachments) {
|
|
389
|
-
mail.attachments = data.attachments;
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
if (data.headers) {
|
|
393
|
-
mail.headers = data.headers;
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
if (data.html) {
|
|
397
|
-
mail.html = data.html;
|
|
398
|
-
|
|
399
|
-
if (!data.text) {
|
|
400
|
-
mail.text = htmlToText.fromString(data.html, {
|
|
401
|
-
wordwrap: null,
|
|
402
|
-
unorderedListItemPrefix: ' - ',
|
|
403
|
-
});
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
if (!DataValidator.isEmailValid(data.from.email)) {
|
|
408
|
-
throw new Error('Invalid from email ' + data.from.email);
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
try {
|
|
412
|
-
// Can we send from the transactional email server?
|
|
413
|
-
if (STAMHOOFD.TRANSACTIONAL_WHITELIST !== undefined && data.type === 'transactional') {
|
|
414
|
-
if (!this.matchWhitelist(data.from.email, STAMHOOFD.TRANSACTIONAL_WHITELIST)) {
|
|
415
|
-
// Not supported
|
|
416
|
-
data.type = 'broadcast';
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
const transporter = (data.type === 'transactional') ? this.transactionalTransporter : this.transporter;
|
|
421
|
-
|
|
422
|
-
if (data.type === 'transactional') {
|
|
423
|
-
mail.headers = {
|
|
424
|
-
...data.headers,
|
|
425
|
-
...STAMHOOFD.TRANSACTIONAL_SMTP_HEADERS,
|
|
426
|
-
};
|
|
427
|
-
}
|
|
428
|
-
else {
|
|
429
|
-
mail.headers = {
|
|
430
|
-
...data.headers,
|
|
431
|
-
...STAMHOOFD.SMTP_HEADERS,
|
|
432
|
-
};
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
console.log('Sending email', to, data.subject, data.type);
|
|
436
|
-
const info = await transporter.sendMail(mail);
|
|
437
|
-
console.log('Message sent:', to, data.subject, info.messageId, data.type);
|
|
438
|
-
|
|
439
|
-
if (STAMHOOFD.environment === 'development') {
|
|
440
|
-
await sleep(100);
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
try {
|
|
444
|
-
data.callback?.(null);
|
|
445
|
-
}
|
|
446
|
-
catch (e) {
|
|
447
|
-
console.error('Error in email callback', e);
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
catch (e) {
|
|
451
|
-
if (STAMHOOFD.environment !== 'test') {
|
|
452
|
-
console.error('Failed to send e-mail:');
|
|
453
|
-
console.error(e);
|
|
454
|
-
console.error(mail);
|
|
455
|
-
|
|
456
|
-
// Sleep 1 second to give servers some time to fix possible rate limits
|
|
457
|
-
await sleep(1000);
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
// Reschedule twice (at maximum) to fix temporary connection issues
|
|
461
|
-
data.retryCount = (data.retryCount ?? 0) + 1;
|
|
462
|
-
|
|
463
|
-
if (data.retryCount <= 2) {
|
|
464
|
-
if (data.type === 'transactional' && data.retryCount === 2) {
|
|
465
|
-
data.type = 'broadcast';
|
|
466
|
-
}
|
|
467
|
-
this.send(data);
|
|
468
|
-
}
|
|
469
|
-
else {
|
|
470
|
-
try {
|
|
471
|
-
if (data.callback) {
|
|
472
|
-
data.callback(e);
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
catch (e2) {
|
|
476
|
-
console.error('Error in email failure callback', e2, 'for original error', e);
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
// Email address is not verified.
|
|
480
|
-
if (STAMHOOFD.environment === 'production') {
|
|
481
|
-
if (data.from.email !== this.getWebmasterFromEmail().email) {
|
|
482
|
-
this.sendWebmaster({
|
|
483
|
-
subject: $t(`2206e5e4-2fc4-4ffd-aefb-60ba0d20aa23`),
|
|
484
|
-
text: $t(`d1b217e5-c82e-42fb-93e5-dd6f2d137692`, { email: data.from.email, to: mail.to }) + ': \n\n' + e + '\n\n' + (mail.text ?? ''),
|
|
485
|
-
type: (data.type === 'transactional') ? 'broadcast' : 'transactional',
|
|
486
|
-
});
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
getWebmasterFromEmail() {
|
|
494
|
-
return {
|
|
495
|
-
name: Formatter.capitalizeFirstLetter(STAMHOOFD.platformName ?? 'Stamhoofd'),
|
|
496
|
-
email: 'webmaster@' + (new I18n(Language.Dutch, Country.Belgium).localizedDomains.defaultTransactionalEmail()),
|
|
497
|
-
};
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
getWebmasterToEmail() {
|
|
501
|
-
return {
|
|
502
|
-
name: 'Stamhoofd',
|
|
503
|
-
email: 'hallo@stamhoofd.be',
|
|
504
|
-
};
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
/**
|
|
508
|
-
* Send an email to the webmaster
|
|
509
|
-
*/
|
|
510
|
-
sendWebmaster(data: Omit<EmailInterfaceBase, 'to'>) {
|
|
511
|
-
const mail = Object.assign(data, {
|
|
512
|
-
from: this.getWebmasterFromEmail(),
|
|
513
|
-
to: [this.getWebmasterToEmail()],
|
|
514
|
-
type: data.type ?? 'transactional',
|
|
515
|
-
});
|
|
516
|
-
this.send(mail);
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
send(data: EmailInterface) {
|
|
520
|
-
let didSend = false;
|
|
521
|
-
|
|
522
|
-
this.schedule(() => {
|
|
523
|
-
if (didSend) {
|
|
524
|
-
return undefined;
|
|
525
|
-
}
|
|
526
|
-
didSend = true;
|
|
527
|
-
return data;
|
|
528
|
-
});
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
schedule(builder: EmailBuilder) {
|
|
532
|
-
this.currentQueue.push(builder);
|
|
533
|
-
this.sendNextIfNeeded();
|
|
534
|
-
}
|
|
535
|
-
|
|
536
|
-
wait() {
|
|
537
|
-
return new Promise<void>((resolve) => {
|
|
538
|
-
this.schedule(() => {
|
|
539
|
-
resolve();
|
|
540
|
-
return undefined;
|
|
541
|
-
});
|
|
542
|
-
});
|
|
543
|
-
}
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
export const Email = new EmailStatic();
|
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
import { TestUtils } from '@stamhoofd/test-utils';
|
|
2
|
-
import { Email, InternalEmailData } from './Email.js';
|
|
3
|
-
|
|
4
|
-
type MockedCallback = (data: InternalEmailData) => Promise<void> | void;
|
|
5
|
-
type MockedResponse = { error?: Error | null; callback?: MockedCallback };
|
|
6
|
-
|
|
7
|
-
export class EmailMocker {
|
|
8
|
-
sentEmails: InternalEmailData[] = [];
|
|
9
|
-
failedEmails: InternalEmailData[] = [];
|
|
10
|
-
|
|
11
|
-
responseQueue: MockedResponse[] = [];
|
|
12
|
-
mode: 'transactional' | 'broadcast' = 'transactional';
|
|
13
|
-
|
|
14
|
-
constructor(mode: 'transactional' | 'broadcast' = 'transactional') {
|
|
15
|
-
this.mode = mode;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
static transactional = new EmailMocker('transactional');
|
|
19
|
-
static broadcast = new EmailMocker('broadcast');
|
|
20
|
-
|
|
21
|
-
static infect() {
|
|
22
|
-
if (STAMHOOFD.environment !== 'test') {
|
|
23
|
-
throw new Error('EmailMocker can only be used in test environment');
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const handler = async (mocker: EmailMocker, data: InternalEmailData) => {
|
|
27
|
-
const nextHandler = mocker.responseQueue.shift();
|
|
28
|
-
|
|
29
|
-
try {
|
|
30
|
-
if (nextHandler) {
|
|
31
|
-
if (nextHandler.callback) {
|
|
32
|
-
await nextHandler.callback(data);
|
|
33
|
-
}
|
|
34
|
-
if (nextHandler.error) {
|
|
35
|
-
throw nextHandler.error;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
catch (e) {
|
|
40
|
-
mocker.failedEmails.push(data);
|
|
41
|
-
throw e;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
mocker.sentEmails.push(data);
|
|
45
|
-
|
|
46
|
-
return {
|
|
47
|
-
messageId: 'mocked-' + mocker.sentEmails.length,
|
|
48
|
-
};
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
TestUtils.addBeforeAll(async () => {
|
|
52
|
-
// We load async here because this is a dev dependency (otherwise Node will reach this and complain, breaking the boot)
|
|
53
|
-
const sinon = await import('sinon');
|
|
54
|
-
|
|
55
|
-
sinon.stub(Email, 'setupIfNeeded').callsFake(function (this: typeof Email) {
|
|
56
|
-
this.transporter = {
|
|
57
|
-
sendMail: async (data: InternalEmailData) => {
|
|
58
|
-
return handler(EmailMocker.broadcast, data);
|
|
59
|
-
},
|
|
60
|
-
} as any;
|
|
61
|
-
|
|
62
|
-
this.transactionalTransporter = {
|
|
63
|
-
sendMail: async (data: InternalEmailData) => {
|
|
64
|
-
return handler(EmailMocker.transactional, data);
|
|
65
|
-
},
|
|
66
|
-
} as any;
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
TestUtils.addAfterEach(() => {
|
|
71
|
-
EmailMocker.afterAll();
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
static afterAll() {
|
|
76
|
-
// Clear
|
|
77
|
-
EmailMocker.transactional.reset();
|
|
78
|
-
EmailMocker.broadcast.reset();
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
reset() {
|
|
82
|
-
this.sentEmails = [];
|
|
83
|
-
this.failedEmails = [];
|
|
84
|
-
this.responseQueue = [];
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// Defining failure / success
|
|
88
|
-
onNext(callback: MockedCallback) {
|
|
89
|
-
this.responseQueue.push({ callback });
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
succeedNext() {
|
|
93
|
-
this.responseQueue.push({});
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
failNext(error: Error) {
|
|
97
|
-
this.responseQueue.push({ error });
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// Helpers
|
|
101
|
-
async getSucceededCount() {
|
|
102
|
-
await Email.wait();
|
|
103
|
-
return this.sentEmails.length;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
async getFailedCount() {
|
|
107
|
-
await Email.wait();
|
|
108
|
-
return this.failedEmails.length;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
static async getSucceededCount() {
|
|
112
|
-
return await EmailMocker.transactional.getSucceededCount() + await EmailMocker.broadcast.getSucceededCount();
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
static async getFailedCount() {
|
|
116
|
-
return await EmailMocker.transactional.getFailedCount() + await EmailMocker.broadcast.getFailedCount();
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
getSucceededEmail(index: number) {
|
|
120
|
-
return this.sentEmails[index];
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
static getSucceededEmail(index: number) {
|
|
124
|
-
const transactionalCount = EmailMocker.transactional.sentEmails.length;
|
|
125
|
-
if (index < transactionalCount) {
|
|
126
|
-
return EmailMocker.transactional.getSucceededEmail(index);
|
|
127
|
-
}
|
|
128
|
-
return EmailMocker.broadcast.getSucceededEmail(index - transactionalCount);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
async getSucceededEmails() {
|
|
132
|
-
await Email.wait();
|
|
133
|
-
return this.sentEmails;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
static async getSucceededEmails() {
|
|
137
|
-
await Email.wait();
|
|
138
|
-
return [...EmailMocker.transactional.sentEmails, ...EmailMocker.broadcast.sentEmails];
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
async getFailedEmails() {
|
|
142
|
-
await Email.wait();
|
|
143
|
-
return this.failedEmails;
|
|
144
|
-
}
|
|
145
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
CREATE TABLE `email_addresses` (
|
|
2
|
-
`id` varchar(36) CHARACTER SET ascii COLLATE ascii_general_ci NOT NULL,
|
|
3
|
-
`email` varchar(255) DEFAULT NULL,
|
|
4
|
-
`organizationId` varchar(36) CHARACTER SET ascii COLLATE ascii_general_ci DEFAULT NULL,
|
|
5
|
-
`markedAsSpam` tinyint(1) DEFAULT NULL,
|
|
6
|
-
`hardBounce` tinyint(1) DEFAULT NULL,
|
|
7
|
-
`unsubscribedMarketing` tinyint(1) DEFAULT NULL,
|
|
8
|
-
`unsubscribedAll` tinyint(1) DEFAULT NULL,
|
|
9
|
-
`token` varchar(255) CHARACTER SET ascii COLLATE ascii_general_ci DEFAULT NULL,
|
|
10
|
-
`createdAt` datetime NOT NULL,
|
|
11
|
-
`updatedAt` datetime NOT NULL,
|
|
12
|
-
PRIMARY KEY (`id`),
|
|
13
|
-
UNIQUE KEY `email` (`email`,`organizationId`) USING BTREE
|
|
14
|
-
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|