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/rollup/email.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import fs from 'fs';
1
+ import { existsSync, open, read, closeSync, close } from 'fs';
2
2
  import { hostname } from 'os';
3
3
  import { Stream } from 'stream';
4
4
  import { TextEncoder, TextDecoder } from 'util';
@@ -252,6 +252,19 @@ function getRFC2822DateUTC(date = new Date()) {
252
252
  dates.push('+0000');
253
253
  return dates.join(' ');
254
254
  }
255
+ /**
256
+ * RFC 2822 regex
257
+ * @see https://tools.ietf.org/html/rfc2822#section-3.3
258
+ * @see https://github.com/moment/moment/blob/a831fc7e2694281ce31e4f090bbcf90a690f0277/src/lib/create/from-string.js#L101
259
+ */
260
+ const rfc2822re = /^(?:(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}))$/;
261
+ /**
262
+ * @param {string} [date] a string to check for conformance to the [rfc2822](https://tools.ietf.org/html/rfc2822#section-3.3) standard
263
+ * @returns {boolean} the result of the conformance check
264
+ */
265
+ function isRFC2822Date(date) {
266
+ return rfc2822re.test(date);
267
+ }
255
268
 
256
269
  // adapted from https://github.com/emailjs/emailjs-mime-codec/blob/6909c706b9f09bc0e5c3faf48f723cca53e5b352/src/mimecodec.js
257
270
  const encoder = new TextEncoder();
@@ -263,7 +276,7 @@ const RANGES = [
263
276
  [0x0a],
264
277
  [0x0d],
265
278
  [0x20, 0x3c],
266
- [0x3e, 0x7e],
279
+ [0x3e, 0x7e], // >?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}
267
280
  ];
268
281
  const LOOKUP = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split('');
269
282
  const MAX_CHUNK_LENGTH = 16383; // must be multiple of 3
@@ -440,7 +453,7 @@ function mimeWordEncode(data, mimeWordEncoding = 'Q', encoding = 'utf-8') {
440
453
  .trim();
441
454
  }
442
455
 
443
- const CRLF = '\r\n';
456
+ const CRLF$1 = '\r\n';
444
457
  /**
445
458
  * MIME standard wants 76 char chunks when sending out.
446
459
  */
@@ -454,7 +467,7 @@ const MIME64CHUNK = (MIMECHUNK * 6);
454
467
  */
455
468
  const BUFFERSIZE = (MIMECHUNK * 24 * 7);
456
469
  let counter = 0;
457
- function generate_boundary() {
470
+ function generateBoundary() {
458
471
  let text = '';
459
472
  const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'()+_,-./:=?";
460
473
  for (let i = 0; i < 69; i++) {
@@ -552,30 +565,32 @@ class Message {
552
565
  }
553
566
  /**
554
567
  * @public
555
- * @param {function(isValid: boolean, invalidReason: string): void} callback .
556
- * @returns {void}
568
+ * @returns {{ isValid: boolean, validationError: (string | undefined) }} an object specifying whether this message is validly formatted, and the first validation error if it is not.
557
569
  */
558
- valid(callback) {
570
+ checkValidity() {
559
571
  if (typeof this.header.from !== 'string' &&
560
572
  Array.isArray(this.header.from) === false) {
561
- callback(false, 'Message must have a `from` header');
573
+ return {
574
+ isValid: false,
575
+ validationError: 'Message must have a `from` header',
576
+ };
562
577
  }
563
- else if (typeof this.header.to !== 'string' &&
578
+ if (typeof this.header.to !== 'string' &&
564
579
  Array.isArray(this.header.to) === false &&
565
580
  typeof this.header.cc !== 'string' &&
566
581
  Array.isArray(this.header.cc) === false &&
567
582
  typeof this.header.bcc !== 'string' &&
568
583
  Array.isArray(this.header.bcc) === false) {
569
- callback(false, 'Message must have at least one `to`, `cc`, or `bcc` header');
570
- }
571
- else if (this.attachments.length === 0) {
572
- callback(true, undefined);
584
+ return {
585
+ isValid: false,
586
+ validationError: 'Message must have at least one `to`, `cc`, or `bcc` header',
587
+ };
573
588
  }
574
- else {
589
+ if (this.attachments.length > 0) {
575
590
  const failed = [];
576
591
  this.attachments.forEach((attachment) => {
577
592
  if (attachment.path) {
578
- if (fs.existsSync(attachment.path) == false) {
593
+ if (existsSync(attachment.path) === false) {
579
594
  failed.push(`${attachment.path} does not exist`);
580
595
  }
581
596
  }
@@ -588,12 +603,26 @@ class Message {
588
603
  failed.push('attachment has no data associated with it');
589
604
  }
590
605
  });
591
- callback(failed.length === 0, failed.join(', '));
606
+ return {
607
+ isValid: failed.length === 0,
608
+ validationError: failed.join(', '),
609
+ };
592
610
  }
611
+ return { isValid: true, validationError: undefined };
593
612
  }
594
613
  /**
595
614
  * @public
596
- * @returns {*} a stream of the current message
615
+ * @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.
616
+ * @param {function(isValid: boolean, invalidReason: (string | undefined)): void} callback .
617
+ * @returns {void}
618
+ */
619
+ valid(callback) {
620
+ const { isValid, validationError } = this.checkValidity();
621
+ callback(isValid, validationError);
622
+ }
623
+ /**
624
+ * @public
625
+ * @returns {MessageStream} a stream of the current message
597
626
  */
598
627
  stream() {
599
628
  return new MessageStream(this);
@@ -610,10 +639,22 @@ class Message {
610
639
  str.on('end', (err) => callback(err, buffer));
611
640
  str.on('error', (err) => callback(err, buffer));
612
641
  }
642
+ readAsync() {
643
+ return new Promise((resolve, reject) => {
644
+ this.read((err, buffer) => {
645
+ if (err != null) {
646
+ reject(err);
647
+ }
648
+ else {
649
+ resolve(buffer);
650
+ }
651
+ });
652
+ });
653
+ }
613
654
  }
614
655
  class MessageStream extends Stream {
615
656
  /**
616
- * @param {*} message the message to stream
657
+ * @param {Message} message the message to stream
617
658
  */
618
659
  constructor(message) {
619
660
  super();
@@ -666,7 +707,7 @@ class MessageStream extends Stream {
666
707
  * @param {MessageAttachment} [attachment] the attachment whose headers you would like to output
667
708
  * @returns {void}
668
709
  */
669
- const output_attachment_headers = (attachment) => {
710
+ const outputAttachmentHeaders = (attachment) => {
670
711
  let data = [];
671
712
  const headers = {
672
713
  'content-type': attachment.type +
@@ -688,89 +729,81 @@ class MessageStream extends Stream {
688
729
  convertDashDelimitedTextToSnakeCase(header),
689
730
  ': ',
690
731
  headers[header],
691
- CRLF,
732
+ CRLF$1,
692
733
  ]);
693
734
  }
694
- output(data.concat([CRLF]).join(''));
735
+ output(data.concat([CRLF$1]).join(''));
695
736
  };
696
737
  /**
697
738
  * @param {string} data the data to output as base64
698
739
  * @param {function(): void} [callback] the function to call after output is finished
699
740
  * @returns {void}
700
741
  */
701
- const output_base64 = (data, callback) => {
742
+ const outputBase64 = (data, callback) => {
702
743
  const loops = Math.ceil(data.length / MIMECHUNK);
703
744
  let loop = 0;
704
745
  while (loop < loops) {
705
- output(data.substring(MIMECHUNK * loop, MIMECHUNK * (loop + 1)) + CRLF);
746
+ output(data.substring(MIMECHUNK * loop, MIMECHUNK * (loop + 1)) + CRLF$1);
706
747
  loop++;
707
748
  }
708
749
  if (callback) {
709
750
  callback();
710
751
  }
711
752
  };
712
- const output_file = (attachment, next) => {
753
+ const outputFile = (attachment, next) => {
754
+ var _a;
713
755
  const chunk = MIME64CHUNK * 16;
714
756
  const buffer = Buffer.alloc(chunk);
715
- const closed = (fd) => fs.closeSync(fd);
757
+ const inputEncoding = ((_a = attachment === null || attachment === void 0 ? void 0 : attachment.headers) === null || _a === void 0 ? void 0 : _a['content-transfer-encoding']) || 'base64';
758
+ const encoding = inputEncoding === '7bit'
759
+ ? 'ascii'
760
+ : inputEncoding === '8bit'
761
+ ? 'binary'
762
+ : inputEncoding;
716
763
  /**
717
764
  * @param {Error} err the error to emit
718
765
  * @param {number} fd the file descriptor
719
766
  * @returns {void}
720
767
  */
721
768
  const opened = (err, fd) => {
722
- if (!err) {
723
- const read = (err, bytes) => {
724
- if (!err && this.readable) {
725
- let encoding = attachment && attachment.headers
726
- ? attachment.headers['content-transfer-encoding'] || 'base64'
727
- : 'base64';
728
- if (encoding === 'ascii' || encoding === '7bit') {
729
- encoding = 'ascii';
730
- }
731
- else if (encoding === 'binary' || encoding === '8bit') {
732
- encoding = 'binary';
733
- }
734
- else {
735
- encoding = 'base64';
736
- }
737
- // guaranteed to be encoded without padding unless it is our last read
738
- output_base64(buffer.toString(encoding, 0, bytes), () => {
739
- if (bytes == chunk) {
740
- // we read a full chunk, there might be more
741
- fs.read(fd, buffer, 0, chunk, null, read);
742
- } // that was the last chunk, we are done reading the file
743
- else {
744
- this.removeListener('error', closed);
745
- fs.close(fd, next);
746
- }
747
- });
748
- }
749
- else {
750
- this.emit('error', err || { message: 'message stream was interrupted somehow!' });
751
- }
752
- };
753
- fs.read(fd, buffer, 0, chunk, null, read);
754
- this.once('error', closed);
755
- }
756
- else {
769
+ if (err) {
757
770
  this.emit('error', err);
771
+ return;
758
772
  }
773
+ const readBytes = (err, bytes) => {
774
+ if (err || this.readable === false) {
775
+ this.emit('error', err || new Error('message stream was interrupted somehow!'));
776
+ return;
777
+ }
778
+ // guaranteed to be encoded without padding unless it is our last read
779
+ outputBase64(buffer.toString(encoding, 0, bytes), () => {
780
+ if (bytes == chunk) {
781
+ // we read a full chunk, there might be more
782
+ read(fd, buffer, 0, chunk, null, readBytes);
783
+ } // that was the last chunk, we are done reading the file
784
+ else {
785
+ this.removeListener('error', closeSync);
786
+ close(fd, next);
787
+ }
788
+ });
789
+ };
790
+ read(fd, buffer, 0, chunk, null, readBytes);
791
+ this.once('error', closeSync);
759
792
  };
760
- fs.open(attachment.path, 'r', opened);
793
+ open(attachment.path, 'r', opened);
761
794
  };
762
795
  /**
763
796
  * @param {MessageAttachment} attachment the metadata to use as headers
764
797
  * @param {function(): void} callback the function to call after output is finished
765
798
  * @returns {void}
766
799
  */
767
- const output_stream = (attachment, callback) => {
800
+ const outputStream = (attachment, callback) => {
768
801
  const { stream } = attachment;
769
802
  if (stream === null || stream === void 0 ? void 0 : stream.readable) {
770
803
  let previous = Buffer.alloc(0);
771
804
  stream.resume();
772
805
  stream.on('end', () => {
773
- output_base64(previous.toString('base64'), callback);
806
+ outputBase64(previous.toString('base64'), callback);
774
807
  this.removeListener('pause', stream.pause);
775
808
  this.removeListener('resume', stream.resume);
776
809
  this.removeListener('error', stream.resume);
@@ -788,7 +821,7 @@ class MessageStream extends Stream {
788
821
  // copy dangling bytes into previous buffer
789
822
  buffer.copy(previous, 0, buffer.length - padded);
790
823
  }
791
- output_base64(buffer.toString('base64', 0, buffer.length - padded));
824
+ outputBase64(buffer.toString('base64', 0, buffer.length - padded));
792
825
  });
793
826
  this.on('pause', stream.pause);
794
827
  this.on('resume', stream.resume);
@@ -798,13 +831,13 @@ class MessageStream extends Stream {
798
831
  this.emit('error', { message: 'stream not readable' });
799
832
  }
800
833
  };
801
- const output_attachment = (attachment, callback) => {
834
+ const outputAttachment = (attachment, callback) => {
802
835
  const build = attachment.path
803
- ? output_file
836
+ ? outputFile
804
837
  : attachment.stream
805
- ? output_stream
806
- : output_data;
807
- output_attachment_headers(attachment);
838
+ ? outputStream
839
+ : outputData;
840
+ outputAttachmentHeaders(attachment);
808
841
  build(attachment, callback);
809
842
  };
810
843
  /**
@@ -814,32 +847,32 @@ class MessageStream extends Stream {
814
847
  * @param {function(): void} callback the function to call if index is greater than upper bound
815
848
  * @returns {void}
816
849
  */
817
- const output_message = (boundary, list, index, callback) => {
850
+ const outputMessage = (boundary, list, index, callback) => {
818
851
  if (index < list.length) {
819
- output(`--${boundary}${CRLF}`);
852
+ output(`--${boundary}${CRLF$1}`);
820
853
  if (list[index].related) {
821
- output_related(list[index], () => output_message(boundary, list, index + 1, callback));
854
+ outputRelated(list[index], () => outputMessage(boundary, list, index + 1, callback));
822
855
  }
823
856
  else {
824
- output_attachment(list[index], () => output_message(boundary, list, index + 1, callback));
857
+ outputAttachment(list[index], () => outputMessage(boundary, list, index + 1, callback));
825
858
  }
826
859
  }
827
860
  else {
828
- output(`${CRLF}--${boundary}--${CRLF}${CRLF}`);
861
+ output(`${CRLF$1}--${boundary}--${CRLF$1}${CRLF$1}`);
829
862
  callback();
830
863
  }
831
864
  };
832
- const output_mixed = () => {
833
- const boundary = generate_boundary();
834
- output(`Content-Type: multipart/mixed; boundary="${boundary}"${CRLF}${CRLF}--${boundary}${CRLF}`);
865
+ const outputMixed = () => {
866
+ const boundary = generateBoundary();
867
+ output(`Content-Type: multipart/mixed; boundary="${boundary}"${CRLF$1}${CRLF$1}--${boundary}${CRLF$1}`);
835
868
  if (this.message.alternative == null) {
836
- output_text(this.message);
837
- output_message(boundary, this.message.attachments, 0, close);
869
+ outputText(this.message);
870
+ outputMessage(boundary, this.message.attachments, 0, close$1);
838
871
  }
839
872
  else {
840
- output_alternative(
873
+ outputAlternative(
841
874
  // typescript bug; should narrow to { alternative: MessageAttachment }
842
- this.message, () => output_message(boundary, this.message.attachments, 0, close));
875
+ this.message, () => outputMessage(boundary, this.message.attachments, 0, close$1));
843
876
  }
844
877
  };
845
878
  /**
@@ -847,26 +880,27 @@ class MessageStream extends Stream {
847
880
  * @param {function(): void} callback the function to call after output is finished
848
881
  * @returns {void}
849
882
  */
850
- const output_data = (attachment, callback) => {
883
+ const outputData = (attachment, callback) => {
851
884
  var _a, _b;
852
- output_base64(attachment.encoded
853
- ? (_a = attachment.data) !== null && _a !== void 0 ? _a : '' : Buffer.from((_b = attachment.data) !== null && _b !== void 0 ? _b : '').toString('base64'), callback);
885
+ outputBase64(attachment.encoded
886
+ ? (_a = attachment.data) !== null && _a !== void 0 ? _a : ''
887
+ : Buffer.from((_b = attachment.data) !== null && _b !== void 0 ? _b : '').toString('base64'), callback);
854
888
  };
855
889
  /**
856
890
  * @param {Message} message the message to output
857
891
  * @returns {void}
858
892
  */
859
- const output_text = (message) => {
893
+ const outputText = (message) => {
860
894
  let data = [];
861
895
  data = data.concat([
862
896
  'Content-Type:',
863
897
  message.content,
864
- CRLF,
898
+ CRLF$1,
865
899
  'Content-Transfer-Encoding: 7bit',
866
- CRLF,
900
+ CRLF$1,
867
901
  ]);
868
- data = data.concat(['Content-Disposition: inline', CRLF, CRLF]);
869
- data = data.concat([message.text || '', CRLF, CRLF]);
902
+ data = data.concat(['Content-Disposition: inline', CRLF$1, CRLF$1]);
903
+ data = data.concat([message.text || '', CRLF$1, CRLF$1]);
870
904
  output(data.join(''));
871
905
  };
872
906
  /**
@@ -874,13 +908,13 @@ class MessageStream extends Stream {
874
908
  * @param {function(): void} callback the function to call after output is finished
875
909
  * @returns {void}
876
910
  */
877
- const output_related = (message, callback) => {
878
- const boundary = generate_boundary();
879
- output(`Content-Type: multipart/related; boundary="${boundary}"${CRLF}${CRLF}--${boundary}${CRLF}`);
880
- output_attachment(message, () => {
911
+ const outputRelated = (message, callback) => {
912
+ const boundary = generateBoundary();
913
+ output(`Content-Type: multipart/related; boundary="${boundary}"${CRLF$1}${CRLF$1}--${boundary}${CRLF$1}`);
914
+ outputAttachment(message, () => {
881
915
  var _a;
882
- output_message(boundary, (_a = message.related) !== null && _a !== void 0 ? _a : [], 0, () => {
883
- output(`${CRLF}--${boundary}--${CRLF}${CRLF}`);
916
+ outputMessage(boundary, (_a = message.related) !== null && _a !== void 0 ? _a : [], 0, () => {
917
+ output(`${CRLF$1}--${boundary}--${CRLF$1}${CRLF$1}`);
884
918
  callback();
885
919
  });
886
920
  });
@@ -890,26 +924,26 @@ class MessageStream extends Stream {
890
924
  * @param {function(): void} callback the function to call after output is finished
891
925
  * @returns {void}
892
926
  */
893
- const output_alternative = (message, callback) => {
894
- const boundary = generate_boundary();
895
- output(`Content-Type: multipart/alternative; boundary="${boundary}"${CRLF}${CRLF}--${boundary}${CRLF}`);
896
- output_text(message);
897
- output(`--${boundary}${CRLF}`);
927
+ const outputAlternative = (message, callback) => {
928
+ const boundary = generateBoundary();
929
+ output(`Content-Type: multipart/alternative; boundary="${boundary}"${CRLF$1}${CRLF$1}--${boundary}${CRLF$1}`);
930
+ outputText(message);
931
+ output(`--${boundary}${CRLF$1}`);
898
932
  /**
899
933
  * @returns {void}
900
934
  */
901
935
  const finish = () => {
902
- output([CRLF, '--', boundary, '--', CRLF, CRLF].join(''));
936
+ output([CRLF$1, '--', boundary, '--', CRLF$1, CRLF$1].join(''));
903
937
  callback();
904
938
  };
905
939
  if (message.alternative.related) {
906
- output_related(message.alternative, finish);
940
+ outputRelated(message.alternative, finish);
907
941
  }
908
942
  else {
909
- output_attachment(message.alternative, finish);
943
+ outputAttachment(message.alternative, finish);
910
944
  }
911
945
  };
912
- const close = (err) => {
946
+ const close$1 = (err) => {
913
947
  var _a, _b;
914
948
  if (err) {
915
949
  this.emit('error', err);
@@ -930,20 +964,20 @@ class MessageStream extends Stream {
930
964
  /**
931
965
  * @returns {void}
932
966
  */
933
- const output_header_data = () => {
967
+ const outputHeaderData = () => {
934
968
  if (this.message.attachments.length || this.message.alternative) {
935
- output(`MIME-Version: 1.0${CRLF}`);
936
- output_mixed();
969
+ output(`MIME-Version: 1.0${CRLF$1}`);
970
+ outputMixed();
937
971
  } // you only have a text message!
938
972
  else {
939
- output_text(this.message);
940
- close();
973
+ outputText(this.message);
974
+ close$1();
941
975
  }
942
976
  };
943
977
  /**
944
978
  * @returns {void}
945
979
  */
946
- const output_header = () => {
980
+ const outputHeader = () => {
947
981
  let data = [];
948
982
  for (const header in this.message.header) {
949
983
  // do not output BCC in the headers (regex) nor custom Object.prototype functions...
@@ -953,15 +987,15 @@ class MessageStream extends Stream {
953
987
  convertDashDelimitedTextToSnakeCase(header),
954
988
  ': ',
955
989
  this.message.header[header],
956
- CRLF,
990
+ CRLF$1,
957
991
  ]);
958
992
  }
959
993
  }
960
994
  output(data.join(''));
961
- output_header_data();
995
+ outputHeaderData();
962
996
  };
963
- this.once('destroy', close);
964
- process.nextTick(output_header);
997
+ this.once('destroy', close$1);
998
+ process.nextTick(outputHeader);
965
999
  }
966
1000
  /**
967
1001
  * @public
@@ -1128,11 +1162,11 @@ const DEFAULT_TIMEOUT = 5000;
1128
1162
  const SMTP_PORT = 25;
1129
1163
  const SMTP_SSL_PORT = 465;
1130
1164
  const SMTP_TLS_PORT = 587;
1131
- const CRLF$1 = '\r\n';
1165
+ const CRLF = '\r\n';
1132
1166
  const GREYLIST_DELAY = 300;
1133
1167
  let DEBUG = 0;
1134
1168
  /**
1135
- * @param {...any} args the message(s) to log
1169
+ * @param {...any[]} args the message(s) to log
1136
1170
  * @returns {void}
1137
1171
  */
1138
1172
  const log = (...args) => {
@@ -1145,8 +1179,8 @@ const log = (...args) => {
1145
1179
  }
1146
1180
  };
1147
1181
  /**
1148
- * @param {function(...*): void} callback the function to call
1149
- * @param {...*} args the arguments to apply to the function
1182
+ * @param {function(...any[]): void} callback the function to call
1183
+ * @param {...any[]} args the arguments to apply to the function
1150
1184
  * @returns {void}
1151
1185
  */
1152
1186
  const caller = (callback, ...args) => {
@@ -1183,7 +1217,7 @@ class SMTPConnection extends EventEmitter {
1183
1217
  this.host = 'localhost';
1184
1218
  this.ssl = false;
1185
1219
  this.tls = false;
1186
- this.greylistResponseTracker = new WeakMap();
1220
+ this.greylistResponseTracker = new WeakSet();
1187
1221
  if (Array.isArray(authentication)) {
1188
1222
  this.authentication = authentication;
1189
1223
  }
@@ -1246,7 +1280,7 @@ class SMTPConnection extends EventEmitter {
1246
1280
  * NOTE: `host` is trimmed before being used to establish a connection; however, the original untrimmed value will still be visible in configuration.
1247
1281
  *
1248
1282
  * @public
1249
- * @param {function(...*): void} callback function to call after response
1283
+ * @param {function(...any[]): void} callback function to call after response
1250
1284
  * @param {number} [port] the port to use for the connection
1251
1285
  * @param {string} [host] the hostname to use for the connection
1252
1286
  * @param {ConnectOptions} [options={}] the options
@@ -1328,7 +1362,7 @@ class SMTPConnection extends EventEmitter {
1328
1362
  /**
1329
1363
  * @public
1330
1364
  * @param {string} str the string to send
1331
- * @param {*} callback function to call after response
1365
+ * @param {function(...any[]): void} callback function to call after response
1332
1366
  * @returns {void}
1333
1367
  */
1334
1368
  send(str, callback) {
@@ -1355,7 +1389,7 @@ class SMTPConnection extends EventEmitter {
1355
1389
  /**
1356
1390
  * @public
1357
1391
  * @param {string} cmd command to issue
1358
- * @param {function(...*): void} callback function to call after response
1392
+ * @param {function(...any[]): void} callback function to call after response
1359
1393
  * @param {(number[] | number)} [codes=[250]] array codes
1360
1394
  * @returns {void}
1361
1395
  */
@@ -1376,10 +1410,10 @@ class SMTPConnection extends EventEmitter {
1376
1410
  }
1377
1411
  else if ((code === 450 || code === 451) &&
1378
1412
  msg.message.toLowerCase().includes('greylist') &&
1379
- this.greylistResponseTracker.get(response) === false) {
1380
- this.greylistResponseTracker.set(response, true);
1413
+ this.greylistResponseTracker.has(response) === false) {
1414
+ this.greylistResponseTracker.add(response);
1381
1415
  setTimeout(() => {
1382
- this.send(cmd + CRLF$1, response);
1416
+ this.send(cmd + CRLF, response);
1383
1417
  }, GREYLIST_DELAY);
1384
1418
  }
1385
1419
  else {
@@ -1389,8 +1423,8 @@ class SMTPConnection extends EventEmitter {
1389
1423
  }
1390
1424
  }
1391
1425
  };
1392
- this.greylistResponseTracker.set(response, false);
1393
- this.send(cmd + CRLF$1, response);
1426
+ this.greylistResponseTracker.delete(response);
1427
+ this.send(cmd + CRLF, response);
1394
1428
  }
1395
1429
  /**
1396
1430
  * @public
@@ -1402,7 +1436,7 @@ class SMTPConnection extends EventEmitter {
1402
1436
  * As this command was deprecated by rfc2821, it should only be used for compatibility with non-compliant servers.
1403
1437
  * @see https://tools.ietf.org/html/rfc2821#appendix-F.3
1404
1438
  *
1405
- * @param {function(...*): void} callback function to call after response
1439
+ * @param {function(...any[]): void} callback function to call after response
1406
1440
  * @param {string} domain the domain to associate with the 'helo' request
1407
1441
  * @returns {void}
1408
1442
  */
@@ -1419,7 +1453,7 @@ class SMTPConnection extends EventEmitter {
1419
1453
  }
1420
1454
  /**
1421
1455
  * @public
1422
- * @param {function(...*): void} callback function to call after response
1456
+ * @param {function(...any[]): void} callback function to call after response
1423
1457
  * @returns {void}
1424
1458
  */
1425
1459
  starttls(callback) {
@@ -1474,7 +1508,7 @@ class SMTPConnection extends EventEmitter {
1474
1508
  }
1475
1509
  /**
1476
1510
  * @public
1477
- * @param {function(...*): void} callback function to call after response
1511
+ * @param {function(...any[]): void} callback function to call after response
1478
1512
  * @param {string} domain the domain to associate with the 'ehlo' request
1479
1513
  * @returns {void}
1480
1514
  */
@@ -1507,7 +1541,7 @@ class SMTPConnection extends EventEmitter {
1507
1541
  /**
1508
1542
  * @public
1509
1543
  * @description SMTP 'help' command, returns text from the server
1510
- * @param {function(...*): void} callback function to call after response
1544
+ * @param {function(...any[]): void} callback function to call after response
1511
1545
  * @param {string} domain the domain to associate with the 'help' request
1512
1546
  * @returns {void}
1513
1547
  */
@@ -1516,7 +1550,7 @@ class SMTPConnection extends EventEmitter {
1516
1550
  }
1517
1551
  /**
1518
1552
  * @public
1519
- * @param {function(...*): void} callback function to call after response
1553
+ * @param {function(...any[]): void} callback function to call after response
1520
1554
  * @returns {void}
1521
1555
  */
1522
1556
  rset(callback) {
@@ -1524,7 +1558,7 @@ class SMTPConnection extends EventEmitter {
1524
1558
  }
1525
1559
  /**
1526
1560
  * @public
1527
- * @param {function(...*): void} callback function to call after response
1561
+ * @param {function(...any[]): void} callback function to call after response
1528
1562
  * @returns {void}
1529
1563
  */
1530
1564
  noop(callback) {
@@ -1532,7 +1566,7 @@ class SMTPConnection extends EventEmitter {
1532
1566
  }
1533
1567
  /**
1534
1568
  * @public
1535
- * @param {function(...*): void} callback function to call after response
1569
+ * @param {function(...any[]): void} callback function to call after response
1536
1570
  * @param {string} from the sender
1537
1571
  * @returns {void}
1538
1572
  */
@@ -1541,7 +1575,7 @@ class SMTPConnection extends EventEmitter {
1541
1575
  }
1542
1576
  /**
1543
1577
  * @public
1544
- * @param {function(...*): void} callback function to call after response
1578
+ * @param {function(...any[]): void} callback function to call after response
1545
1579
  * @param {string} to the receiver
1546
1580
  * @returns {void}
1547
1581
  */
@@ -1550,7 +1584,7 @@ class SMTPConnection extends EventEmitter {
1550
1584
  }
1551
1585
  /**
1552
1586
  * @public
1553
- * @param {function(...*): void} callback function to call after response
1587
+ * @param {function(...any[]): void} callback function to call after response
1554
1588
  * @returns {void}
1555
1589
  */
1556
1590
  data(callback) {
@@ -1558,11 +1592,11 @@ class SMTPConnection extends EventEmitter {
1558
1592
  }
1559
1593
  /**
1560
1594
  * @public
1561
- * @param {function(...*): void} callback function to call after response
1595
+ * @param {function(...any[]): void} callback function to call after response
1562
1596
  * @returns {void}
1563
1597
  */
1564
1598
  data_end(callback) {
1565
- this.command(`${CRLF$1}.`, callback);
1599
+ this.command(`${CRLF}.`, callback);
1566
1600
  }
1567
1601
  /**
1568
1602
  * @public
@@ -1578,7 +1612,7 @@ class SMTPConnection extends EventEmitter {
1578
1612
  * @public
1579
1613
  * @description SMTP 'verify' command -- checks for address validity.
1580
1614
  * @param {string} address the address to validate
1581
- * @param {function(...*): void} callback function to call after response
1615
+ * @param {function(...any[]): void} callback function to call after response
1582
1616
  * @returns {void}
1583
1617
  */
1584
1618
  verify(address, callback) {
@@ -1588,7 +1622,7 @@ class SMTPConnection extends EventEmitter {
1588
1622
  * @public
1589
1623
  * @description SMTP 'expn' command -- expands a mailing list.
1590
1624
  * @param {string} address the mailing list to expand
1591
- * @param {function(...*): void} callback function to call after response
1625
+ * @param {function(...any[]): void} callback function to call after response
1592
1626
  * @returns {void}
1593
1627
  */
1594
1628
  expn(address, callback) {
@@ -1601,7 +1635,7 @@ class SMTPConnection extends EventEmitter {
1601
1635
  * If there has been no previous EHLO or HELO command self session, self
1602
1636
  * method tries ESMTP EHLO first.
1603
1637
  *
1604
- * @param {function(...*): void} callback function to call after response
1638
+ * @param {function(...any[]): void} callback function to call after response
1605
1639
  * @param {string} [domain] the domain to associate with the command
1606
1640
  * @returns {void}
1607
1641
  */
@@ -1629,7 +1663,7 @@ class SMTPConnection extends EventEmitter {
1629
1663
  *
1630
1664
  * This method will return normally if the authentication was successful.
1631
1665
  *
1632
- * @param {function(...*): void} callback function to call after response
1666
+ * @param {function(...any[]): void} callback function to call after response
1633
1667
  * @param {string} [user] the username to authenticate with
1634
1668
  * @param {string} [password] the password for the authentication
1635
1669
  * @param {{ method: string, domain: string }} [options] login options
@@ -1654,7 +1688,7 @@ class SMTPConnection extends EventEmitter {
1654
1688
  * @param {string} challenge challenge
1655
1689
  * @returns {string} base64 cram hash
1656
1690
  */
1657
- const encode_cram_md5 = (challenge) => {
1691
+ const encodeCramMd5 = (challenge) => {
1658
1692
  const hmac = createHmac('md5', login.password());
1659
1693
  hmac.update(Buffer.from(challenge, 'base64').toString('ascii'));
1660
1694
  return Buffer.from(`${login.user()} ${hmac.digest('hex')}`).toString('base64');
@@ -1662,19 +1696,19 @@ class SMTPConnection extends EventEmitter {
1662
1696
  /**
1663
1697
  * @returns {string} base64 login/password
1664
1698
  */
1665
- const encode_plain = () => Buffer.from(`\u0000${login.user()}\u0000${login.password()}`).toString('base64');
1699
+ const encodePlain = () => Buffer.from(`\u0000${login.user()}\u0000${login.password()}`).toString('base64');
1666
1700
  /**
1667
1701
  * @see https://developers.google.com/gmail/xoauth2_protocol
1668
1702
  * @returns {string} base64 xoauth2 auth token
1669
1703
  */
1670
- const encode_xoauth2 = () => Buffer.from(`user=${login.user()}\u0001auth=Bearer ${login.password()}\u0001\u0001`).toString('base64');
1704
+ const encodeXoauth2 = () => Buffer.from(`user=${login.user()}\u0001auth=Bearer ${login.password()}\u0001\u0001`).toString('base64');
1671
1705
  // List of authentication methods we support: from preferred to
1672
1706
  // less preferred methods.
1673
1707
  if (!method) {
1674
1708
  const preferred = this.authentication;
1675
1709
  let auth = '';
1676
- if (typeof ((_a = this.features) === null || _a === void 0 ? void 0 : _a.auth) === 'string') {
1677
- auth = this.features.auth;
1710
+ if (typeof ((_a = this.features) === null || _a === void 0 ? void 0 : _a['auth']) === 'string') {
1711
+ auth = this.features['auth'];
1678
1712
  }
1679
1713
  for (let i = 0; i < preferred.length; i++) {
1680
1714
  if (auth.includes(preferred[i])) {
@@ -1686,7 +1720,7 @@ class SMTPConnection extends EventEmitter {
1686
1720
  /**
1687
1721
  * handle bad responses from command differently
1688
1722
  * @param {Error} err err
1689
- * @param {*} data data
1723
+ * @param {unknown} data data
1690
1724
  * @returns {void}
1691
1725
  */
1692
1726
  const failed = (err, data) => {
@@ -1696,7 +1730,7 @@ class SMTPConnection extends EventEmitter {
1696
1730
  };
1697
1731
  /**
1698
1732
  * @param {Error} err err
1699
- * @param {*} data data
1733
+ * @param {unknown} data data
1700
1734
  * @returns {void}
1701
1735
  */
1702
1736
  const response = (err, data) => {
@@ -1710,7 +1744,7 @@ class SMTPConnection extends EventEmitter {
1710
1744
  };
1711
1745
  /**
1712
1746
  * @param {Error} err err
1713
- * @param {*} data data
1747
+ * @param {unknown} data data
1714
1748
  * @param {string} msg msg
1715
1749
  * @returns {void}
1716
1750
  */
@@ -1720,7 +1754,7 @@ class SMTPConnection extends EventEmitter {
1720
1754
  }
1721
1755
  else {
1722
1756
  if (method === AUTH_METHODS['CRAM-MD5']) {
1723
- this.command(encode_cram_md5(msg), response, [235, 503]);
1757
+ this.command(encodeCramMd5(msg), response, [235, 503]);
1724
1758
  }
1725
1759
  else if (method === AUTH_METHODS.LOGIN) {
1726
1760
  this.command(Buffer.from(login.password()).toString('base64'), response, [235, 503]);
@@ -1729,11 +1763,11 @@ class SMTPConnection extends EventEmitter {
1729
1763
  };
1730
1764
  /**
1731
1765
  * @param {Error} err err
1732
- * @param {*} data data
1766
+ * @param {unknown} data data
1733
1767
  * @param {string} msg msg
1734
1768
  * @returns {void}
1735
1769
  */
1736
- const attempt_user = (err, data) => {
1770
+ const attemptUser = (err, data) => {
1737
1771
  if (err) {
1738
1772
  failed(err, data);
1739
1773
  }
@@ -1748,18 +1782,16 @@ class SMTPConnection extends EventEmitter {
1748
1782
  this.command(`AUTH ${AUTH_METHODS['CRAM-MD5']}`, attempt, [334]);
1749
1783
  break;
1750
1784
  case AUTH_METHODS.LOGIN:
1751
- this.command(`AUTH ${AUTH_METHODS.LOGIN}`, attempt_user, [334]);
1785
+ this.command(`AUTH ${AUTH_METHODS.LOGIN}`, attemptUser, [334]);
1752
1786
  break;
1753
1787
  case AUTH_METHODS.PLAIN:
1754
- this.command(`AUTH ${AUTH_METHODS.PLAIN} ${encode_plain()}`, response, [235, 503]);
1788
+ this.command(`AUTH ${AUTH_METHODS.PLAIN} ${encodePlain()}`, response, [235, 503]);
1755
1789
  break;
1756
1790
  case AUTH_METHODS.XOAUTH2:
1757
- this.command(`AUTH ${AUTH_METHODS.XOAUTH2} ${encode_xoauth2()}`, response, [235, 503]);
1791
+ this.command(`AUTH ${AUTH_METHODS.XOAUTH2} ${encodeXoauth2()}`, response, [235, 503]);
1758
1792
  break;
1759
1793
  default:
1760
- const msg = 'no form of authorization supported';
1761
- const err = SMTPError.create(msg, SMTPErrorStates.AUTHNOTSUPPORTED, null, data);
1762
- caller(callback, err);
1794
+ caller(callback, SMTPError.create('no form of authorization supported', SMTPErrorStates.AUTHNOTSUPPORTED, null, data));
1763
1795
  break;
1764
1796
  }
1765
1797
  };
@@ -1793,7 +1825,7 @@ class SMTPConnection extends EventEmitter {
1793
1825
  }
1794
1826
  /**
1795
1827
  * @public
1796
- * @param {function(...*): void} [callback] function to call after response
1828
+ * @param {function(...any[]): void} [callback] function to call after response
1797
1829
  * @returns {void}
1798
1830
  */
1799
1831
  quit(callback) {
@@ -1835,18 +1867,34 @@ class SMTPClient {
1835
1867
  callback(new Error('message is not a valid Message instance'), msg);
1836
1868
  return;
1837
1869
  }
1838
- message.valid((valid, why) => {
1839
- if (valid) {
1840
- const stack = this.createMessageStack(message, callback);
1841
- if (stack.to.length === 0) {
1842
- return callback(new Error('No recipients found in message'), msg);
1870
+ const { isValid, validationError } = message.checkValidity();
1871
+ if (isValid) {
1872
+ const stack = this.createMessageStack(message, callback);
1873
+ if (stack.to.length === 0) {
1874
+ return callback(new Error('No recipients found in message'), msg);
1875
+ }
1876
+ this.queue.push(stack);
1877
+ this._poll();
1878
+ }
1879
+ else {
1880
+ callback(new Error(validationError), msg);
1881
+ }
1882
+ }
1883
+ /**
1884
+ * @public
1885
+ * @param {Message} msg the message to send
1886
+ * @returns {Promise<Message>} a promise that resolves to the fully processed message
1887
+ */
1888
+ sendAsync(msg) {
1889
+ return new Promise((resolve, reject) => {
1890
+ this.send(msg, (err, msg) => {
1891
+ if (err != null) {
1892
+ reject(err);
1843
1893
  }
1844
- this.queue.push(stack);
1845
- this._poll();
1846
- }
1847
- else {
1848
- callback(new Error(why), msg);
1849
- }
1894
+ else {
1895
+ resolve(msg);
1896
+ }
1897
+ });
1850
1898
  });
1851
1899
  }
1852
1900
  /**
@@ -1962,8 +2010,8 @@ class SMTPClient {
1962
2010
  }
1963
2011
  /**
1964
2012
  * @protected
1965
- * @param {*} attachment attachment
1966
- * @returns {*} whether the attachment contains inlined html
2013
+ * @param {MessageAttachment | MessageAttachment[]} attachment attachment
2014
+ * @returns {boolean} whether the attachment contains inlined html
1967
2015
  */
1968
2016
  _containsInlinedHtml(attachment) {
1969
2017
  if (Array.isArray(attachment)) {
@@ -2069,5 +2117,5 @@ class SMTPClient {
2069
2117
  }
2070
2118
  }
2071
2119
 
2072
- export { AUTH_METHODS, BUFFERSIZE, DEFAULT_TIMEOUT, MIME64CHUNK, MIMECHUNK, Message, SMTPClient, SMTPConnection, SMTPError, SMTPErrorStates, SMTPResponseMonitor, SMTPState, addressparser, getRFC2822Date, getRFC2822DateUTC, mimeEncode, mimeWordEncode };
2120
+ export { AUTH_METHODS, BUFFERSIZE, DEFAULT_TIMEOUT, MIME64CHUNK, MIMECHUNK, Message, SMTPClient, SMTPConnection, SMTPError, SMTPErrorStates, SMTPResponseMonitor, SMTPState, addressparser, getRFC2822Date, getRFC2822DateUTC, isRFC2822Date, mimeEncode, mimeWordEncode };
2073
2121
  //# sourceMappingURL=email.mjs.map