emailjs 3.3.0 → 3.7.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/smtp/date.ts CHANGED
@@ -33,3 +33,19 @@ export function getRFC2822DateUTC(date = new Date()) {
33
33
  dates.push('+0000');
34
34
  return dates.join(' ');
35
35
  }
36
+
37
+ /**
38
+ * RFC 2822 regex
39
+ * @see https://tools.ietf.org/html/rfc2822#section-3.3
40
+ * @see https://github.com/moment/moment/blob/a831fc7e2694281ce31e4f090bbcf90a690f0277/src/lib/create/from-string.js#L101
41
+ */
42
+ const rfc2822re =
43
+ /^(?:(Mon|Tue|Wed|Thu|Fri|Sat|Sun),?\s)?(\d{1,2})\s(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s(\d{2,4})\s(\d\d):(\d\d)(?::(\d\d))?\s(?:(UT|GMT|[ECMP][SD]T)|([Zz])|([+-]\d{4}))$/;
44
+
45
+ /**
46
+ * @param {string} [date] a string to check for conformance to the [rfc2822](https://tools.ietf.org/html/rfc2822#section-3.3) standard
47
+ * @returns {boolean} the result of the conformance check
48
+ */
49
+ export function isRFC2822Date(date: string) {
50
+ return rfc2822re.test(date);
51
+ }
package/smtp/message.ts CHANGED
@@ -1,4 +1,11 @@
1
- import fs, { PathLike } from 'fs';
1
+ import {
2
+ PathLike,
3
+ existsSync,
4
+ open as openFile,
5
+ close as closeFile,
6
+ closeSync as closeFileSync,
7
+ read as readFile,
8
+ } from 'fs';
2
9
  import { hostname } from 'os';
3
10
  import { Stream } from 'stream';
4
11
  import type { Readable } from 'stream';
@@ -78,7 +85,7 @@ export interface MessageHeaders {
78
85
 
79
86
  let counter = 0;
80
87
 
81
- function generate_boundary() {
88
+ function generateBoundary() {
82
89
  let text = '';
83
90
  const possible =
84
91
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'()+_,-./:=?";
@@ -170,7 +177,7 @@ export class Message {
170
177
  * @param {MessageAttachment} options attachment options
171
178
  * @returns {Message} the current instance for chaining
172
179
  */
173
- public attach(options: MessageAttachment): Message {
180
+ public attach(options: MessageAttachment) {
174
181
  // sender can specify an attachment as an alternative
175
182
  if (options.alternative) {
176
183
  this.alternative = options;
@@ -186,16 +193,20 @@ export class Message {
186
193
 
187
194
  /**
188
195
  * @public
189
- * @param {function(isValid: boolean, invalidReason: string): void} callback .
190
- * @returns {void}
196
+ * @returns {{ isValid: boolean, validationError: (string | undefined) }} an object specifying whether this message is validly formatted, and the first validation error if it is not.
191
197
  */
192
- public valid(callback: (isValid: boolean, invalidReason?: string) => void) {
198
+ public checkValidity() {
193
199
  if (
194
200
  typeof this.header.from !== 'string' &&
195
201
  Array.isArray(this.header.from) === false
196
202
  ) {
197
- callback(false, 'Message must have a `from` header');
198
- } else if (
203
+ return {
204
+ isValid: false,
205
+ validationError: 'Message must have a `from` header',
206
+ };
207
+ }
208
+
209
+ if (
199
210
  typeof this.header.to !== 'string' &&
200
211
  Array.isArray(this.header.to) === false &&
201
212
  typeof this.header.cc !== 'string' &&
@@ -203,18 +214,19 @@ export class Message {
203
214
  typeof this.header.bcc !== 'string' &&
204
215
  Array.isArray(this.header.bcc) === false
205
216
  ) {
206
- callback(
207
- false,
208
- 'Message must have at least one `to`, `cc`, or `bcc` header'
209
- );
210
- } else if (this.attachments.length === 0) {
211
- callback(true, undefined);
212
- } else {
217
+ return {
218
+ isValid: false,
219
+ validationError:
220
+ 'Message must have at least one `to`, `cc`, or `bcc` header',
221
+ };
222
+ }
223
+
224
+ if (this.attachments.length > 0) {
213
225
  const failed: string[] = [];
214
226
 
215
227
  this.attachments.forEach((attachment) => {
216
228
  if (attachment.path) {
217
- if (fs.existsSync(attachment.path) == false) {
229
+ if (existsSync(attachment.path) === false) {
218
230
  failed.push(`${attachment.path} does not exist`);
219
231
  }
220
232
  } else if (attachment.stream) {
@@ -225,14 +237,29 @@ export class Message {
225
237
  failed.push('attachment has no data associated with it');
226
238
  }
227
239
  });
228
-
229
- callback(failed.length === 0, failed.join(', '));
240
+ return {
241
+ isValid: failed.length === 0,
242
+ validationError: failed.join(', '),
243
+ };
230
244
  }
245
+
246
+ return { isValid: true, validationError: undefined };
247
+ }
248
+
249
+ /**
250
+ * @public
251
+ * @deprecated does not conform to the `errback` style followed by the rest of the library, and will be removed in the next major version. use `checkValidity` instead.
252
+ * @param {function(isValid: boolean, invalidReason: (string | undefined)): void} callback .
253
+ * @returns {void}
254
+ */
255
+ public valid(callback: (isValid: boolean, invalidReason?: string) => void) {
256
+ const { isValid, validationError } = this.checkValidity();
257
+ callback(isValid, validationError);
231
258
  }
232
259
 
233
260
  /**
234
261
  * @public
235
- * @returns {*} a stream of the current message
262
+ * @returns {MessageStream} a stream of the current message
236
263
  */
237
264
  public stream() {
238
265
  return new MessageStream(this);
@@ -250,6 +277,18 @@ export class Message {
250
277
  str.on('end', (err) => callback(err, buffer));
251
278
  str.on('error', (err) => callback(err, buffer));
252
279
  }
280
+
281
+ public readAsync() {
282
+ return new Promise<string>((resolve, reject) => {
283
+ this.read((err, buffer) => {
284
+ if (err != null) {
285
+ reject(err);
286
+ } else {
287
+ resolve(buffer);
288
+ }
289
+ });
290
+ });
291
+ }
253
292
  }
254
293
 
255
294
  class MessageStream extends Stream {
@@ -259,7 +298,7 @@ class MessageStream extends Stream {
259
298
  bufferIndex = 0;
260
299
 
261
300
  /**
262
- * @param {*} message the message to stream
301
+ * @param {Message} message the message to stream
263
302
  */
264
303
  constructor(private message: Message) {
265
304
  super();
@@ -322,7 +361,7 @@ class MessageStream extends Stream {
322
361
  * @param {MessageAttachment} [attachment] the attachment whose headers you would like to output
323
362
  * @returns {void}
324
363
  */
325
- const output_attachment_headers = (attachment: MessageAttachment) => {
364
+ const outputAttachmentHeaders = (attachment: MessageAttachment) => {
326
365
  let data: string[] = [];
327
366
  const headers: Partial<MessageHeaders> = {
328
367
  'content-type':
@@ -361,7 +400,7 @@ class MessageStream extends Stream {
361
400
  * @param {function(): void} [callback] the function to call after output is finished
362
401
  * @returns {void}
363
402
  */
364
- const output_base64 = (data: string, callback?: () => void) => {
403
+ const outputBase64 = (data: string, callback?: () => void) => {
365
404
  const loops = Math.ceil(data.length / MIMECHUNK);
366
405
  let loop = 0;
367
406
  while (loop < loops) {
@@ -373,13 +412,21 @@ class MessageStream extends Stream {
373
412
  }
374
413
  };
375
414
 
376
- const output_file = (
415
+ const outputFile = (
377
416
  attachment: MessageAttachment,
378
417
  next: (err: NodeJS.ErrnoException | null) => void
379
418
  ) => {
380
419
  const chunk = MIME64CHUNK * 16;
381
420
  const buffer = Buffer.alloc(chunk);
382
- const closed = (fd: number) => fs.closeSync(fd);
421
+
422
+ const inputEncoding =
423
+ attachment?.headers?.['content-transfer-encoding'] || 'base64';
424
+ const encoding =
425
+ inputEncoding === '7bit'
426
+ ? 'ascii'
427
+ : inputEncoding === '8bit'
428
+ ? 'binary'
429
+ : inputEncoding;
383
430
 
384
431
  /**
385
432
  * @param {Error} err the error to emit
@@ -387,47 +434,38 @@ class MessageStream extends Stream {
387
434
  * @returns {void}
388
435
  */
389
436
  const opened = (err: NodeJS.ErrnoException | null, fd: number) => {
390
- if (!err) {
391
- const read = (err: NodeJS.ErrnoException | null, bytes: number) => {
392
- if (!err && this.readable) {
393
- let encoding =
394
- attachment && attachment.headers
395
- ? attachment.headers['content-transfer-encoding'] || 'base64'
396
- : 'base64';
397
- if (encoding === 'ascii' || encoding === '7bit') {
398
- encoding = 'ascii';
399
- } else if (encoding === 'binary' || encoding === '8bit') {
400
- encoding = 'binary';
401
- } else {
402
- encoding = 'base64';
403
- }
404
- // guaranteed to be encoded without padding unless it is our last read
405
- output_base64(buffer.toString(encoding, 0, bytes), () => {
406
- if (bytes == chunk) {
407
- // we read a full chunk, there might be more
408
- fs.read(fd, buffer, 0, chunk, null, read);
409
- } // that was the last chunk, we are done reading the file
410
- else {
411
- this.removeListener('error', closed);
412
- fs.close(fd, next);
413
- }
414
- });
415
- } else {
416
- this.emit(
417
- 'error',
418
- err || { message: 'message stream was interrupted somehow!' }
419
- );
420
- }
421
- };
422
-
423
- fs.read(fd, buffer, 0, chunk, null, read);
424
- this.once('error', closed);
425
- } else {
437
+ if (err) {
426
438
  this.emit('error', err);
439
+ return;
427
440
  }
441
+ const readBytes = (
442
+ err: NodeJS.ErrnoException | null,
443
+ bytes: number
444
+ ) => {
445
+ if (err || this.readable === false) {
446
+ this.emit(
447
+ 'error',
448
+ err || new Error('message stream was interrupted somehow!')
449
+ );
450
+ return;
451
+ }
452
+ // guaranteed to be encoded without padding unless it is our last read
453
+ outputBase64(buffer.toString(encoding, 0, bytes), () => {
454
+ if (bytes == chunk) {
455
+ // we read a full chunk, there might be more
456
+ readFile(fd, buffer, 0, chunk, null, readBytes);
457
+ } // that was the last chunk, we are done reading the file
458
+ else {
459
+ this.removeListener('error', closeFileSync);
460
+ closeFile(fd, next);
461
+ }
462
+ });
463
+ };
464
+ readFile(fd, buffer, 0, chunk, null, readBytes);
465
+ this.once('error', closeFileSync);
428
466
  };
429
467
 
430
- fs.open(attachment.path as PathLike, 'r', opened);
468
+ openFile(attachment.path as PathLike, 'r', opened);
431
469
  };
432
470
 
433
471
  /**
@@ -435,7 +473,7 @@ class MessageStream extends Stream {
435
473
  * @param {function(): void} callback the function to call after output is finished
436
474
  * @returns {void}
437
475
  */
438
- const output_stream = (
476
+ const outputStream = (
439
477
  attachment: MessageAttachment,
440
478
  callback: () => void
441
479
  ) => {
@@ -446,7 +484,7 @@ class MessageStream extends Stream {
446
484
  stream.resume();
447
485
 
448
486
  stream.on('end', () => {
449
- output_base64(previous.toString('base64'), callback);
487
+ outputBase64(previous.toString('base64'), callback);
450
488
  this.removeListener('pause', stream.pause);
451
489
  this.removeListener('resume', stream.resume);
452
490
  this.removeListener('error', stream.resume);
@@ -468,7 +506,7 @@ class MessageStream extends Stream {
468
506
  // copy dangling bytes into previous buffer
469
507
  buffer.copy(previous, 0, buffer.length - padded);
470
508
  }
471
- output_base64(buffer.toString('base64', 0, buffer.length - padded));
509
+ outputBase64(buffer.toString('base64', 0, buffer.length - padded));
472
510
  });
473
511
 
474
512
  this.on('pause', stream.pause);
@@ -479,16 +517,16 @@ class MessageStream extends Stream {
479
517
  }
480
518
  };
481
519
 
482
- const output_attachment = (
520
+ const outputAttachment = (
483
521
  attachment: MessageAttachment,
484
522
  callback: () => void
485
523
  ) => {
486
524
  const build = attachment.path
487
- ? output_file
525
+ ? outputFile
488
526
  : attachment.stream
489
- ? output_stream
490
- : output_data;
491
- output_attachment_headers(attachment);
527
+ ? outputStream
528
+ : outputData;
529
+ outputAttachmentHeaders(attachment);
492
530
  build(attachment, callback);
493
531
  };
494
532
 
@@ -499,7 +537,7 @@ class MessageStream extends Stream {
499
537
  * @param {function(): void} callback the function to call if index is greater than upper bound
500
538
  * @returns {void}
501
539
  */
502
- const output_message = (
540
+ const outputMessage = (
503
541
  boundary: string,
504
542
  list: MessageAttachment[],
505
543
  index: number,
@@ -508,12 +546,12 @@ class MessageStream extends Stream {
508
546
  if (index < list.length) {
509
547
  output(`--${boundary}${CRLF}`);
510
548
  if (list[index].related) {
511
- output_related(list[index], () =>
512
- output_message(boundary, list, index + 1, callback)
549
+ outputRelated(list[index], () =>
550
+ outputMessage(boundary, list, index + 1, callback)
513
551
  );
514
552
  } else {
515
- output_attachment(list[index], () =>
516
- output_message(boundary, list, index + 1, callback)
553
+ outputAttachment(list[index], () =>
554
+ outputMessage(boundary, list, index + 1, callback)
517
555
  );
518
556
  }
519
557
  } else {
@@ -522,20 +560,20 @@ class MessageStream extends Stream {
522
560
  }
523
561
  };
524
562
 
525
- const output_mixed = () => {
526
- const boundary = generate_boundary();
563
+ const outputMixed = () => {
564
+ const boundary = generateBoundary();
527
565
  output(
528
566
  `Content-Type: multipart/mixed; boundary="${boundary}"${CRLF}${CRLF}--${boundary}${CRLF}`
529
567
  );
530
568
 
531
569
  if (this.message.alternative == null) {
532
- output_text(this.message);
533
- output_message(boundary, this.message.attachments, 0, close);
570
+ outputText(this.message);
571
+ outputMessage(boundary, this.message.attachments, 0, close);
534
572
  } else {
535
- output_alternative(
573
+ outputAlternative(
536
574
  // typescript bug; should narrow to { alternative: MessageAttachment }
537
- this.message as Parameters<typeof output_alternative>[0],
538
- () => output_message(boundary, this.message.attachments, 0, close)
575
+ this.message as Parameters<typeof outputAlternative>[0],
576
+ () => outputMessage(boundary, this.message.attachments, 0, close)
539
577
  );
540
578
  }
541
579
  };
@@ -545,11 +583,11 @@ class MessageStream extends Stream {
545
583
  * @param {function(): void} callback the function to call after output is finished
546
584
  * @returns {void}
547
585
  */
548
- const output_data = (
586
+ const outputData = (
549
587
  attachment: MessageAttachment,
550
588
  callback: () => void
551
589
  ) => {
552
- output_base64(
590
+ outputBase64(
553
591
  attachment.encoded
554
592
  ? attachment.data ?? ''
555
593
  : Buffer.from(attachment.data ?? '').toString('base64'),
@@ -561,7 +599,7 @@ class MessageStream extends Stream {
561
599
  * @param {Message} message the message to output
562
600
  * @returns {void}
563
601
  */
564
- const output_text = (message: Message) => {
602
+ const outputText = (message: Message) => {
565
603
  let data: string[] = [];
566
604
 
567
605
  data = data.concat([
@@ -582,16 +620,16 @@ class MessageStream extends Stream {
582
620
  * @param {function(): void} callback the function to call after output is finished
583
621
  * @returns {void}
584
622
  */
585
- const output_related = (
623
+ const outputRelated = (
586
624
  message: MessageAttachment,
587
625
  callback: () => void
588
626
  ) => {
589
- const boundary = generate_boundary();
627
+ const boundary = generateBoundary();
590
628
  output(
591
629
  `Content-Type: multipart/related; boundary="${boundary}"${CRLF}${CRLF}--${boundary}${CRLF}`
592
630
  );
593
- output_attachment(message, () => {
594
- output_message(boundary, message.related ?? [], 0, () => {
631
+ outputAttachment(message, () => {
632
+ outputMessage(boundary, message.related ?? [], 0, () => {
595
633
  output(`${CRLF}--${boundary}--${CRLF}${CRLF}`);
596
634
  callback();
597
635
  });
@@ -603,15 +641,15 @@ class MessageStream extends Stream {
603
641
  * @param {function(): void} callback the function to call after output is finished
604
642
  * @returns {void}
605
643
  */
606
- const output_alternative = (
644
+ const outputAlternative = (
607
645
  message: Message & { alternative: MessageAttachment },
608
646
  callback: () => void
609
647
  ) => {
610
- const boundary = generate_boundary();
648
+ const boundary = generateBoundary();
611
649
  output(
612
650
  `Content-Type: multipart/alternative; boundary="${boundary}"${CRLF}${CRLF}--${boundary}${CRLF}`
613
651
  );
614
- output_text(message);
652
+ outputText(message);
615
653
  output(`--${boundary}${CRLF}`);
616
654
 
617
655
  /**
@@ -623,9 +661,9 @@ class MessageStream extends Stream {
623
661
  };
624
662
 
625
663
  if (message.alternative.related) {
626
- output_related(message.alternative, finish);
664
+ outputRelated(message.alternative, finish);
627
665
  } else {
628
- output_attachment(message.alternative, finish);
666
+ outputAttachment(message.alternative, finish);
629
667
  }
630
668
  };
631
669
 
@@ -652,13 +690,13 @@ class MessageStream extends Stream {
652
690
  /**
653
691
  * @returns {void}
654
692
  */
655
- const output_header_data = () => {
693
+ const outputHeaderData = () => {
656
694
  if (this.message.attachments.length || this.message.alternative) {
657
695
  output(`MIME-Version: 1.0${CRLF}`);
658
- output_mixed();
696
+ outputMixed();
659
697
  } // you only have a text message!
660
698
  else {
661
- output_text(this.message);
699
+ outputText(this.message);
662
700
  close();
663
701
  }
664
702
  };
@@ -666,7 +704,7 @@ class MessageStream extends Stream {
666
704
  /**
667
705
  * @returns {void}
668
706
  */
669
- const output_header = () => {
707
+ const outputHeader = () => {
670
708
  let data: string[] = [];
671
709
 
672
710
  for (const header in this.message.header) {
@@ -685,11 +723,11 @@ class MessageStream extends Stream {
685
723
  }
686
724
 
687
725
  output(data.join(''));
688
- output_header_data();
726
+ outputHeaderData();
689
727
  };
690
728
 
691
729
  this.once('destroy', close);
692
- process.nextTick(output_header);
730
+ process.nextTick(outputHeader);
693
731
  }
694
732
 
695
733
  /**
package/smtp/mime.ts CHANGED
@@ -13,9 +13,8 @@ const RANGES = [
13
13
  [0x20, 0x3c], // <SP>!"#$%&'()*+,-./0123456789:;
14
14
  [0x3e, 0x7e], // >?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}
15
15
  ];
16
- const LOOKUP = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split(
17
- ''
18
- );
16
+ const LOOKUP =
17
+ 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split('');
19
18
  const MAX_CHUNK_LENGTH = 16383; // must be multiple of 3
20
19
  const MAX_MIME_WORD_LENGTH = 52;
21
20
  const MAX_B64_MIME_WORD_BYTE_LENGTH = 39;