emailjs 4.0.3 → 5.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.
Files changed (84) hide show
  1. package/README.md +215 -247
  2. package/dist/address.d.ts +6 -0
  3. package/dist/address.js +183 -0
  4. package/dist/address.js.map +1 -0
  5. package/dist/address.test.d.ts +1 -0
  6. package/dist/address.test.js +120 -0
  7. package/dist/address.test.js.map +1 -0
  8. package/dist/auth.integration.test.d.ts +1 -0
  9. package/dist/auth.integration.test.js +137 -0
  10. package/dist/auth.integration.test.js.map +1 -0
  11. package/dist/client.d.ts +39 -0
  12. package/dist/client.integration.test.d.ts +1 -0
  13. package/dist/client.integration.test.js +198 -0
  14. package/dist/client.integration.test.js.map +1 -0
  15. package/dist/client.js +196 -0
  16. package/dist/client.js.map +1 -0
  17. package/dist/client.test.d.ts +1 -0
  18. package/dist/client.test.js +260 -0
  19. package/dist/client.test.js.map +1 -0
  20. package/dist/connection.commands.integration.test.d.ts +1 -0
  21. package/dist/connection.commands.integration.test.js +79 -0
  22. package/dist/connection.commands.integration.test.js.map +1 -0
  23. package/dist/connection.d.ts +83 -0
  24. package/dist/connection.errors.integration.test.d.ts +1 -0
  25. package/dist/connection.errors.integration.test.js +186 -0
  26. package/dist/connection.errors.integration.test.js.map +1 -0
  27. package/dist/connection.greylist.integration.test.d.ts +1 -0
  28. package/dist/connection.greylist.integration.test.js +53 -0
  29. package/dist/connection.greylist.integration.test.js.map +1 -0
  30. package/dist/connection.integration.test.d.ts +1 -0
  31. package/dist/connection.integration.test.js +223 -0
  32. package/dist/connection.integration.test.js.map +1 -0
  33. package/dist/connection.js +491 -0
  34. package/dist/connection.js.map +1 -0
  35. package/dist/connection.test.d.ts +1 -0
  36. package/dist/connection.test.js +50 -0
  37. package/dist/connection.test.js.map +1 -0
  38. package/dist/date.d.ts +3 -0
  39. package/dist/date.js +26 -0
  40. package/dist/date.js.map +1 -0
  41. package/dist/date.test.d.ts +1 -0
  42. package/dist/date.test.js +26 -0
  43. package/dist/date.test.js.map +1 -0
  44. package/dist/error.d.ts +20 -0
  45. package/dist/error.js +31 -0
  46. package/dist/error.js.map +1 -0
  47. package/dist/error.test.d.ts +1 -0
  48. package/dist/error.test.js +23 -0
  49. package/dist/error.test.js.map +1 -0
  50. package/dist/index.d.ts +8 -0
  51. package/dist/index.js +9 -0
  52. package/dist/index.js.map +1 -0
  53. package/dist/message.d.ts +71 -0
  54. package/dist/message.integration.test.d.ts +1 -0
  55. package/dist/message.integration.test.js +401 -0
  56. package/dist/message.integration.test.js.map +1 -0
  57. package/dist/message.js +456 -0
  58. package/dist/message.js.map +1 -0
  59. package/dist/message.test.d.ts +1 -0
  60. package/dist/message.test.js +258 -0
  61. package/dist/message.test.js.map +1 -0
  62. package/dist/mime.d.ts +2 -0
  63. package/dist/mime.js +145 -0
  64. package/dist/mime.js.map +1 -0
  65. package/dist/mime.test.d.ts +1 -0
  66. package/dist/mime.test.js +76 -0
  67. package/dist/mime.test.js.map +1 -0
  68. package/dist/response.d.ts +6 -0
  69. package/dist/response.js +93 -0
  70. package/dist/response.js.map +1 -0
  71. package/dist/response.test.d.ts +1 -0
  72. package/dist/response.test.js +177 -0
  73. package/dist/response.test.js.map +1 -0
  74. package/package.json +30 -28
  75. package/email.js +0 -2126
  76. package/email.ts +0 -8
  77. package/smtp/address.ts +0 -237
  78. package/smtp/client.ts +0 -365
  79. package/smtp/connection.ts +0 -937
  80. package/smtp/date.ts +0 -51
  81. package/smtp/error.ts +0 -57
  82. package/smtp/message.ts +0 -774
  83. package/smtp/mime.ts +0 -234
  84. package/smtp/response.ts +0 -112
package/email.ts DELETED
@@ -1,8 +0,0 @@
1
- export * from './smtp/address.js';
2
- export * from './smtp/client.js';
3
- export * from './smtp/connection.js';
4
- export * from './smtp/date.js';
5
- export * from './smtp/error.js';
6
- export * from './smtp/message.js';
7
- export * from './smtp/mime.js';
8
- export * from './smtp/response.js';
package/smtp/address.ts DELETED
@@ -1,237 +0,0 @@
1
- interface AddressToken {
2
- type: 'operator' | 'text';
3
- value: string;
4
- }
5
-
6
- export interface AddressObject {
7
- address?: string;
8
- name?: string;
9
- group?: AddressObject[];
10
- }
11
-
12
- /*
13
- * Operator tokens and which tokens are expected to end the sequence
14
- */
15
- const OPERATORS = new Map([
16
- ['"', '"'],
17
- ['(', ')'],
18
- ['<', '>'],
19
- [',', ''],
20
- // Groups are ended by semicolons
21
- [':', ';'],
22
- // Semicolons are not a legal delimiter per the RFC2822 grammar other
23
- // than for terminating a group, but they are also not valid for any
24
- // other use in this context. Given that some mail clients have
25
- // historically allowed the semicolon as a delimiter equivalent to the
26
- // comma in their UI, it makes sense to treat them the same as a comma
27
- // when used outside of a group.
28
- [';', ''],
29
- ]);
30
-
31
- /**
32
- * Tokenizes the original input string
33
- *
34
- * @param {string | string[] | undefined} address string(s) to tokenize
35
- * @return {AddressToken[]} An array of operator|text tokens
36
- */
37
- function tokenizeAddress(address: string | string[] = '') {
38
- const tokens: AddressToken[] = [];
39
- let token: AddressToken | undefined = undefined;
40
- let operator: string | undefined = undefined;
41
-
42
- for (const character of address.toString()) {
43
- if ((operator?.length ?? 0) > 0 && character === operator) {
44
- tokens.push({ type: 'operator', value: character });
45
- token = undefined;
46
- operator = undefined;
47
- } else if ((operator?.length ?? 0) === 0 && OPERATORS.has(character)) {
48
- tokens.push({ type: 'operator', value: character });
49
- token = undefined;
50
- operator = OPERATORS.get(character);
51
- } else {
52
- if (token == null) {
53
- token = { type: 'text', value: character };
54
- tokens.push(token);
55
- } else {
56
- token.value += character;
57
- }
58
- }
59
- }
60
-
61
- return tokens
62
- .map((x) => {
63
- x.value = x.value.trim();
64
- return x;
65
- })
66
- .filter((x) => x.value.length > 0);
67
- }
68
-
69
- /**
70
- * Converts tokens for a single address into an address object
71
- *
72
- * @param {AddressToken[]} tokens Tokens object
73
- * @return {AddressObject[]} addresses object array
74
- */
75
- function convertAddressTokens(tokens: AddressToken[]) {
76
- const addressObjects: AddressObject[] = [];
77
- const groups: string[] = [];
78
- let addresses: string[] = [];
79
- let comments: string[] = [];
80
- let texts: string[] = [];
81
-
82
- let state = 'text';
83
- let isGroup = false;
84
- function handleToken(token: AddressToken) {
85
- if (token.type === 'operator') {
86
- switch (token.value) {
87
- case '<':
88
- state = 'address';
89
- break;
90
- case '(':
91
- state = 'comment';
92
- break;
93
- case ':':
94
- state = 'group';
95
- isGroup = true;
96
- break;
97
- default:
98
- state = 'text';
99
- break;
100
- }
101
- } else if (token.value.length > 0) {
102
- switch (state) {
103
- case 'address':
104
- addresses.push(token.value);
105
- break;
106
- case 'comment':
107
- comments.push(token.value);
108
- break;
109
- case 'group':
110
- groups.push(token.value);
111
- break;
112
- default:
113
- texts.push(token.value);
114
- break;
115
- }
116
- }
117
- }
118
-
119
- // Filter out <addresses>, (comments) and regular text
120
- for (const token of tokens) {
121
- handleToken(token);
122
- }
123
-
124
- // If there is no text but a comment, replace the two
125
- if (texts.length === 0 && comments.length > 0) {
126
- texts = [...comments];
127
- comments = [];
128
- }
129
-
130
- // http://tools.ietf.org/html/rfc2822#appendix-A.1.3
131
- if (isGroup) {
132
- addressObjects.push({
133
- name: texts.length === 0 ? undefined : texts.join(' '),
134
- group: groups.length > 0 ? addressparser(groups.join(',')) : [],
135
- });
136
- } else {
137
- // If no address was found, try to detect one from regular text
138
- if (addresses.length === 0 && texts.length > 0) {
139
- for (let i = texts.length - 1; i >= 0; i--) {
140
- if (texts[i].match(/^[^@\s]+@[^@\s]+$/)) {
141
- addresses = texts.splice(i, 1);
142
- break;
143
- }
144
- }
145
-
146
- // still no address
147
- if (addresses.length === 0) {
148
- for (let i = texts.length - 1; i >= 0; i--) {
149
- texts[i] = texts[i]
150
- .replace(/\s*\b[^@\s]+@[^@\s]+\b\s*/, (address: string) => {
151
- if (addresses.length === 0) {
152
- addresses = [address.trim()];
153
- return ' ';
154
- } else {
155
- return address;
156
- }
157
- })
158
- .trim();
159
-
160
- if (addresses.length > 0) {
161
- break;
162
- }
163
- }
164
- }
165
- }
166
-
167
- // If there's still is no text but a comment exixts, replace the two
168
- if (texts.length === 0 && comments.length > 0) {
169
- texts = [...comments];
170
- comments = [];
171
- }
172
-
173
- // Keep only the first address occurence, push others to regular text
174
- if (addresses.length > 1) {
175
- texts = [...texts, ...addresses.splice(1)];
176
- }
177
-
178
- if (addresses.length === 0 && isGroup) {
179
- return [];
180
- } else {
181
- // Join values with spaces
182
- let address = addresses.join(' ');
183
- let name = texts.length === 0 ? address : texts.join(' ');
184
-
185
- if (address === name) {
186
- if (address.match(/@/)) {
187
- name = '';
188
- } else {
189
- address = '';
190
- }
191
- }
192
-
193
- addressObjects.push({ address, name });
194
- }
195
- }
196
-
197
- return addressObjects;
198
- }
199
-
200
- /**
201
- * Parses structured e-mail addresses from an address field
202
- *
203
- * Example:
204
- *
205
- * "Name <address@domain>"
206
- *
207
- * will be converted to
208
- *
209
- * [{name: "Name", address: "address@domain"}]
210
- *
211
- * @param {string | string[] | undefined} address Address field
212
- * @return {AddressObject[]} An array of address objects
213
- */
214
- export function addressparser(address?: string | string[]) {
215
- const addresses: AddressObject[] = [];
216
- let tokens: AddressToken[] = [];
217
-
218
- for (const token of tokenizeAddress(address)) {
219
- if (
220
- token.type === 'operator' &&
221
- (token.value === ',' || token.value === ';')
222
- ) {
223
- if (tokens.length > 0) {
224
- addresses.push(...convertAddressTokens(tokens));
225
- }
226
- tokens = [];
227
- } else {
228
- tokens.push(token);
229
- }
230
- }
231
-
232
- if (tokens.length > 0) {
233
- addresses.push(...convertAddressTokens(tokens));
234
- }
235
-
236
- return addresses;
237
- }
package/smtp/client.ts DELETED
@@ -1,365 +0,0 @@
1
- import { addressparser } from './address.js';
2
- import type { MessageAttachment, MessageHeaders } from './message.js';
3
- import { Message } from './message.js';
4
- import type { SMTPConnectionOptions } from './connection.js';
5
- import { SMTPConnection, SMTPState } from './connection.js';
6
-
7
- export type MessageCallback<T = Message | MessageHeaders> = <
8
- U extends Error | null,
9
- V extends U extends Error ? T : Message
10
- >(
11
- err: U,
12
- msg: V
13
- ) => void;
14
-
15
- export interface MessageStack {
16
- callback: MessageCallback;
17
- message: Message;
18
- attachment: MessageAttachment;
19
- text: string;
20
- returnPath: string;
21
- from: string;
22
- to: ReturnType<typeof addressparser>;
23
- cc: string[];
24
- bcc: string[];
25
- }
26
-
27
- export class SMTPClient {
28
- public readonly smtp: SMTPConnection;
29
- public readonly queue: MessageStack[] = [];
30
-
31
- protected sending = false;
32
- protected ready = false;
33
- protected timer: NodeJS.Timeout | null = null;
34
-
35
- /**
36
- * Create a standard SMTP client backed by a self-managed SMTP connection.
37
- *
38
- * NOTE: `host` is trimmed before being used to establish a connection; however, the original untrimmed value will still be visible in configuration.
39
- *
40
- * @param {SMTPConnectionOptions} server smtp options
41
- */
42
- constructor(server: Partial<SMTPConnectionOptions>) {
43
- this.smtp = new SMTPConnection(server);
44
- }
45
-
46
- /**
47
- * @public
48
- * @template {Message | MessageHeaders} T
49
- * @param {T} msg the message to send
50
- * @param {MessageCallback<T>} callback receiver for the error (if any) as well as the passed-in message / headers
51
- * @returns {void}
52
- */
53
- public send<T extends Message | MessageHeaders>(
54
- msg: T,
55
- callback: MessageCallback<T>
56
- ): void {
57
- const message =
58
- msg instanceof Message
59
- ? msg
60
- : this._canMakeMessage(msg)
61
- ? new Message(msg)
62
- : null;
63
-
64
- if (message == null) {
65
- callback(new Error('message is not a valid Message instance'), msg);
66
- return;
67
- }
68
-
69
- const { isValid, validationError } = message.checkValidity();
70
-
71
- if (isValid) {
72
- const stack = this.createMessageStack(message, callback);
73
- if (stack.to.length === 0) {
74
- return callback(new Error('No recipients found in message'), msg);
75
- }
76
- this.queue.push(stack);
77
- this._poll();
78
- } else {
79
- callback(new Error(validationError), msg);
80
- }
81
- }
82
-
83
- /**
84
- * @public
85
- * @template {Message | MessageHeaders} T
86
- * @param {T} msg the message to send
87
- * @returns {Promise<T>} a promise that resolves to the passed-in message / headers
88
- */
89
- public sendAsync<T extends Message | MessageHeaders>(msg: T) {
90
- return new Promise<Message>((resolve, reject) => {
91
- this.send(msg, (err, message) => {
92
- if (err != null) {
93
- reject(err);
94
- } else {
95
- // unfortunately, the conditional type doesn't reach here
96
- // fortunately, we only return a `Message` when err is null, so this is safe
97
- resolve(message as Message);
98
- }
99
- });
100
- });
101
- }
102
-
103
- /**
104
- * @public
105
- * @description Converts a message to the raw object used by the internal stack.
106
- * @param {Message} message message to convert
107
- * @param {MessageCallback} callback errback
108
- * @returns {MessageStack} raw message object
109
- */
110
- public createMessageStack(
111
- message: Message,
112
- callback: MessageCallback = function () {
113
- /* ø */
114
- }
115
- ) {
116
- const [{ address: from }] = addressparser(message.header.from);
117
- const stack = {
118
- message,
119
- to: [] as ReturnType<typeof addressparser>,
120
- from,
121
- callback: callback.bind(this),
122
- } as MessageStack;
123
-
124
- const {
125
- header: { to, cc, bcc, 'return-path': returnPath },
126
- } = message;
127
-
128
- if ((typeof to === 'string' || Array.isArray(to)) && to.length > 0) {
129
- stack.to = addressparser(to);
130
- }
131
-
132
- if ((typeof cc === 'string' || Array.isArray(cc)) && cc.length > 0) {
133
- stack.to = stack.to.concat(
134
- addressparser(cc).filter(
135
- (x) => stack.to.some((y) => y.address === x.address) === false
136
- )
137
- );
138
- }
139
-
140
- if ((typeof bcc === 'string' || Array.isArray(bcc)) && bcc.length > 0) {
141
- stack.to = stack.to.concat(
142
- addressparser(bcc).filter(
143
- (x) => stack.to.some((y) => y.address === x.address) === false
144
- )
145
- );
146
- }
147
-
148
- if (typeof returnPath === 'string' && returnPath.length > 0) {
149
- const parsedReturnPath = addressparser(returnPath);
150
- if (parsedReturnPath.length > 0) {
151
- const [{ address: returnPathAddress }] = parsedReturnPath;
152
- stack.returnPath = returnPathAddress as string;
153
- }
154
- }
155
-
156
- return stack;
157
- }
158
-
159
- /**
160
- * @protected
161
- * @returns {void}
162
- */
163
- protected _poll() {
164
- if (this.timer != null) {
165
- clearTimeout(this.timer);
166
- }
167
-
168
- if (this.queue.length) {
169
- if (this.smtp.state() == SMTPState.NOTCONNECTED) {
170
- this._connect(this.queue[0]);
171
- } else if (
172
- this.smtp.state() == SMTPState.CONNECTED &&
173
- !this.sending &&
174
- this.ready
175
- ) {
176
- this._sendmail(this.queue.shift() as MessageStack);
177
- }
178
- }
179
- // wait around 1 seconds in case something does come in,
180
- // otherwise close out SMTP connection if still open
181
- else if (this.smtp.state() == SMTPState.CONNECTED) {
182
- this.timer = setTimeout(() => this.smtp.quit(), 1000);
183
- }
184
- }
185
-
186
- /**
187
- * @protected
188
- * @param {MessageStack} stack stack
189
- * @returns {void}
190
- */
191
- protected _connect(stack: MessageStack) {
192
- /**
193
- * @param {Error} err callback error
194
- * @returns {void}
195
- */
196
- const connect = (err: Error) => {
197
- if (!err) {
198
- const begin = (err: Error) => {
199
- if (!err) {
200
- this.ready = true;
201
- this._poll();
202
- } else {
203
- stack.callback(err, stack.message);
204
-
205
- // clear out the queue so all callbacks can be called with the same error message
206
- this.queue.shift();
207
- this._poll();
208
- }
209
- };
210
-
211
- if (!this.smtp.authorized()) {
212
- this.smtp.login(begin);
213
- } else {
214
- this.smtp.ehlo_or_helo_if_needed(begin);
215
- }
216
- } else {
217
- stack.callback(err, stack.message);
218
-
219
- // clear out the queue so all callbacks can be called with the same error message
220
- this.queue.shift();
221
- this._poll();
222
- }
223
- };
224
-
225
- this.ready = false;
226
- this.smtp.connect(connect);
227
- }
228
-
229
- /**
230
- * @protected
231
- * @param {MessageStack} msg message stack
232
- * @returns {boolean} can make message
233
- */
234
- protected _canMakeMessage(msg: MessageHeaders) {
235
- return (
236
- msg.from &&
237
- (msg.to || msg.cc || msg.bcc) &&
238
- (msg.text !== undefined || this._containsInlinedHtml(msg.attachment))
239
- );
240
- }
241
-
242
- /**
243
- * @protected
244
- * @param {MessageAttachment | MessageAttachment[]} attachment attachment
245
- * @returns {boolean} whether the attachment contains inlined html
246
- */
247
- protected _containsInlinedHtml(
248
- attachment?: MessageAttachment | MessageAttachment[]
249
- ) {
250
- if (Array.isArray(attachment)) {
251
- return attachment.some((att) => {
252
- return this._isAttachmentInlinedHtml(att);
253
- });
254
- } else {
255
- return this._isAttachmentInlinedHtml(attachment);
256
- }
257
- }
258
-
259
- /**
260
- * @protected
261
- * @param {MessageAttachment} attachment attachment
262
- * @returns {boolean} whether the attachment is inlined html
263
- */
264
- protected _isAttachmentInlinedHtml(attachment?: MessageAttachment) {
265
- return (
266
- attachment &&
267
- (attachment.data || attachment.path) &&
268
- attachment.alternative === true
269
- );
270
- }
271
-
272
- /**
273
- * @protected
274
- * @param {MessageStack} stack stack
275
- * @param {function(MessageStack): void} next next
276
- * @returns {function(Error): void} callback
277
- */
278
- protected _sendsmtp(stack: MessageStack, next: (msg: MessageStack) => void) {
279
- /**
280
- * @param {Error} [err] error
281
- * @returns {void}
282
- */
283
- return (err: Error) => {
284
- if (!err && next) {
285
- next.apply(this, [stack]);
286
- } else {
287
- // if we snag on SMTP commands, call done, passing the error
288
- // but first reset SMTP state so queue can continue polling
289
- this.smtp.rset(() => this._senddone(err, stack));
290
- }
291
- };
292
- }
293
-
294
- /**
295
- * @protected
296
- * @param {MessageStack} stack stack
297
- * @returns {void}
298
- */
299
- protected _sendmail(stack: MessageStack) {
300
- const from = stack.returnPath || stack.from;
301
- this.sending = true;
302
- this.smtp.mail(this._sendsmtp(stack, this._sendrcpt), '<' + from + '>');
303
- }
304
-
305
- /**
306
- * @protected
307
- * @param {MessageStack} stack stack
308
- * @returns {void}
309
- */
310
- protected _sendrcpt(stack: MessageStack) {
311
- if (stack.to == null || typeof stack.to === 'string') {
312
- throw new TypeError('stack.to must be array');
313
- }
314
-
315
- const to = stack.to.shift()?.address;
316
- this.smtp.rcpt(
317
- this._sendsmtp(stack, stack.to.length ? this._sendrcpt : this._senddata),
318
- `<${to}>`
319
- );
320
- }
321
-
322
- /**
323
- * @protected
324
- * @param {MessageStack} stack stack
325
- * @returns {void}
326
- */
327
- protected _senddata(stack: MessageStack) {
328
- this.smtp.data(this._sendsmtp(stack, this._sendmessage));
329
- }
330
-
331
- /**
332
- * @protected
333
- * @param {MessageStack} stack stack
334
- * @returns {void}
335
- */
336
- protected _sendmessage(stack: MessageStack) {
337
- const stream = stack.message.stream();
338
-
339
- stream.on('data', (data) => this.smtp.message(data));
340
- stream.on('end', () => {
341
- this.smtp.data_end(
342
- this._sendsmtp(stack, () => this._senddone(null, stack))
343
- );
344
- });
345
-
346
- // there is no way to cancel a message while in the DATA portion,
347
- // so we have to close the socket to prevent a bad email from going out
348
- stream.on('error', (err) => {
349
- this.smtp.close();
350
- this._senddone(err, stack);
351
- });
352
- }
353
-
354
- /**
355
- * @protected
356
- * @param {Error} err err
357
- * @param {MessageStack} stack stack
358
- * @returns {void}
359
- */
360
- protected _senddone(err: Error | null, stack: MessageStack) {
361
- this.sending = false;
362
- stack.callback(err, stack.message);
363
- this._poll();
364
- }
365
- }