@web-ts-toolkit/email-service 0.73.0 → 0.74.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.js CHANGED
@@ -382,7 +382,7 @@ function encodeEvidence(input, code, capacity) {
382
382
 
383
383
  // src/internal/smtp-capacity.ts
384
384
  var MIN_RECIPIENTS = MESSAGE_LIMITS.recipients + 1;
385
- var MAX_RECIPIENTS = 1e3;
385
+ var MAX_SMTP_RECIPIENTS = 1e3;
386
386
  var KIB = 1024;
387
387
  var limits = Object.freeze({
388
388
  snapshotBytes: 2 * KIB * KIB,
@@ -410,7 +410,7 @@ function readBinding(input, code) {
410
410
  return Object.freeze({
411
411
  format,
412
412
  namespace: uuidV4(namespace, code),
413
- maxRecipients: integer(maxRecipients, MIN_RECIPIENTS, MAX_RECIPIENTS, code)
413
+ maxRecipients: integer(maxRecipients, MIN_RECIPIENTS, MAX_SMTP_RECIPIENTS, code)
414
414
  });
415
415
  }
416
416
  function readCapacityOption(raw) {
@@ -487,6 +487,57 @@ function recipientCounts(envelope) {
487
487
  function smtpMessageId(attemptId) {
488
488
  return `<${attemptId}@email.invalid>`;
489
489
  }
490
+ function readRecipientResults(input, code, { limit, allowAccepted, ordered }) {
491
+ const seen = /* @__PURE__ */ new Set();
492
+ let previous = -1;
493
+ return readArray(input, limit, code, (value) => {
494
+ const { group, index: rawIndex, status } = readRecord(value, ["group", "index", "status"], [], code);
495
+ if (group !== "to" && group !== "cc" && group !== "bcc") invalid(code);
496
+ if (status !== "rejected" && (status !== "accepted" || !allowAccepted)) invalid(code);
497
+ const index = integer(rawIndex, 0, limit - 1, code);
498
+ if (ordered) {
499
+ const position = recipientGroups.indexOf(group) * limit + index;
500
+ if (position <= previous) invalid(code);
501
+ previous = position;
502
+ } else {
503
+ const key = `${group}:${index}`;
504
+ if (seen.has(key)) invalid(code);
505
+ seen.add(key);
506
+ }
507
+ return Object.freeze({ group, index, status });
508
+ });
509
+ }
510
+ function allAccepted(recipients) {
511
+ return recipients.length !== 0 && recipients.every(({ status }) => status === "accepted");
512
+ }
513
+ function createSmtpAcceptanceResult(recipients, messageId) {
514
+ const code = "invalid_command";
515
+ if (messageId !== void 0 && (typeof messageId !== "string" || messageId.length !== 52 || messageId !== smtpMessageId(uuidV4(messageId.slice(1, 37), code)))) {
516
+ invalid(code);
517
+ }
518
+ const checked = readRecipientResults(recipients, code, {
519
+ limit: MAX_SMTP_RECIPIENTS,
520
+ allowAccepted: true,
521
+ ordered: false
522
+ });
523
+ const data = { retry: "never", stage: "data", ...messageId === void 0 ? {} : { messageId } };
524
+ if (allAccepted(checked)) {
525
+ return Object.freeze({ ...data, reason: "smtp_accepted", outcome: "accepted", recipients: checked });
526
+ }
527
+ if (!checked.some(({ status }) => status === "accepted")) invalid(code);
528
+ return Object.freeze({
529
+ ...data,
530
+ reason: "smtp_partial",
531
+ outcome: "partially_accepted",
532
+ recipients: checked
533
+ });
534
+ }
535
+ function uncertainProviderResult(stage, reason) {
536
+ const base = { outcome: "unknown", retry: "never", stage };
537
+ if (reason === "provider_timeout") return { ...base, reason };
538
+ if (reason === "provider_aborted") return { ...base, reason };
539
+ return { ...base, reason };
540
+ }
490
541
  function readHint(input, reason, code) {
491
542
  if (reason !== "rate_deferred" && reason !== "sendgrid_throttled") invalid(code);
492
543
  const raw = readRecord(input, [], ["delayMs", "notBefore"], code);
@@ -520,24 +571,10 @@ function readAttemptResult(input, code = "invalid_command", projected = false, c
520
571
  let recipients;
521
572
  if (hasRecipients) {
522
573
  if (origin !== "smtp" && origin !== "provider" || stage !== "envelope" && stage !== "data") invalid(code);
523
- const seen = /* @__PURE__ */ new Set();
524
- const limit = recipientLimit(capacity);
525
- let previous = -1;
526
- recipients = readArray(raw.recipients, limit, code, (value) => {
527
- const { group, index: rawIndex, status } = readRecord(value, ["group", "index", "status"], [], code);
528
- if (group !== "to" && group !== "cc" && group !== "bcc") invalid(code);
529
- if (status !== "rejected" && (status !== "accepted" || !smtpSuccess)) invalid(code);
530
- const index = integer(rawIndex, 0, limit - 1, code);
531
- if (capacity) {
532
- const position = recipientGroups.indexOf(group) * limit + index;
533
- if (position <= previous) invalid(code);
534
- previous = position;
535
- } else {
536
- const key = `${group}:${index}`;
537
- if (seen.has(key)) invalid(code);
538
- seen.add(key);
539
- }
540
- return Object.freeze({ group, index, status });
574
+ recipients = readRecipientResults(raw.recipients, code, {
575
+ limit: recipientLimit(capacity),
576
+ allowAccepted: smtpSuccess,
577
+ ordered: Boolean(capacity)
541
578
  });
542
579
  const accepted = recipients.filter(({ status }) => status === "accepted").length;
543
580
  if (reason === "smtp_accepted" && (!accepted || accepted !== recipients.length) || reason === "smtp_partial" && (!accepted || accepted === recipients.length)) {
@@ -947,6 +984,13 @@ function readProviderCall(input) {
947
984
  // src/providers/smtp/mime.ts
948
985
  var import_mail_composer = __toESM(require("nodemailer/lib/mail-composer"));
949
986
  var import_mime_funcs = require("nodemailer/lib/mime-funcs");
987
+
988
+ // src/internal/smtp-attachments.ts
989
+ function assertSmtpAttachmentTypes(refs) {
990
+ if (refs.some(({ contentType }) => /^(?:message|multipart)\//.test(contentType))) invalid("invalid_snapshot");
991
+ }
992
+
993
+ // src/providers/smtp/mime.ts
950
994
  function body(content) {
951
995
  if (content !== void 0) return { content: Buffer.from(content, "utf8"), contentTransferEncoding: "base64" };
952
996
  }
@@ -958,6 +1002,7 @@ function prepareSmtpMessage({
958
1002
  attemptId,
959
1003
  attachments
960
1004
  }) {
1005
+ assertSmtpAttachmentTypes(attachments.map(({ ref }) => ref));
961
1006
  const { from, to, cc, replyTo } = snapshot.envelope;
962
1007
  const destinations = recipientGroups.flatMap(
963
1008
  (group) => snapshot.envelope[group].map(({ address }, index) => Object.freeze({ address, group, index }))
@@ -975,16 +1020,13 @@ function prepareSmtpMessage({
975
1020
  html: body(snapshot.html),
976
1021
  date: new Date(snapshot.preparedAt),
977
1022
  messageId,
978
- attachments: attachments.map(({ ref, bytes }) => {
979
- if (/^(?:multipart|message)\//.test(ref.contentType)) invalid("invalid_snapshot");
980
- return {
981
- filename: ref.filename,
982
- contentType: ref.contentType,
983
- content: Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength),
984
- contentDisposition: "attachment",
985
- contentTransferEncoding: "base64"
986
- };
987
- }),
1023
+ attachments: attachments.map(({ ref, bytes }) => ({
1024
+ filename: ref.filename,
1025
+ contentType: ref.contentType,
1026
+ content: Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength),
1027
+ contentDisposition: "attachment",
1028
+ contentTransferEncoding: "base64"
1029
+ })),
988
1030
  disableFileAccess: true,
989
1031
  disableUrlAccess: true,
990
1032
  newline: "win"
@@ -1391,18 +1433,12 @@ var SmtpConnection = class {
1391
1433
  function failed(stage, reason) {
1392
1434
  if (reason === "not_transmitted") return { outcome: "unsubmitted", retry: "safe_transient", stage, reason };
1393
1435
  if (reason === "provider_configuration") return { outcome: "unsubmitted", retry: "never", stage, reason };
1394
- return { outcome: "unknown", retry: "never", stage, reason };
1436
+ return uncertainProviderResult(stage, reason);
1395
1437
  }
1396
- function negative(stage, code, permanentRcptRefusal = false, recipients = []) {
1438
+ function negative(at, code, permanentRcptRefusal = false) {
1397
1439
  if (code < 400 || code > 599) throw new SmtpFailure("transport_uncertain");
1398
- const permanent = code >= 500 || permanentRcptRefusal;
1399
- return {
1400
- outcome: "rejected",
1401
- retry: permanent ? "never" : "safe_transient",
1402
- stage,
1403
- reason: permanent ? "smtp_permanent_reply" : "smtp_transient_reply",
1404
- ...recipients.length ? { recipients } : {}
1405
- };
1440
+ const details = at.recipients?.length ? at : { stage: at.stage };
1441
+ return code >= 500 || permanentRcptRefusal ? { ...details, outcome: "rejected", retry: "never", reason: "smtp_permanent_reply" } : { ...details, outcome: "rejected", retry: "safe_transient", reason: "smtp_transient_reply" };
1406
1442
  }
1407
1443
  function extensions({ lines }) {
1408
1444
  let starttls = false;
@@ -1423,34 +1459,34 @@ async function authenticate(connection, { user, pass }, mechanisms) {
1423
1459
  if (command.length <= 510) response = await connection.command(command);
1424
1460
  else {
1425
1461
  response = await connection.command("AUTH PLAIN");
1426
- if (response.code !== 334) return negative("auth", response.code);
1462
+ if (response.code !== 334) return negative({ stage: "auth" }, response.code);
1427
1463
  response = await connection.command(encoded);
1428
1464
  }
1429
1465
  } else if (mechanisms.has("LOGIN")) {
1430
1466
  response = await connection.command("AUTH LOGIN");
1431
- if (response.code !== 334) return negative("auth", response.code);
1467
+ if (response.code !== 334) return negative({ stage: "auth" }, response.code);
1432
1468
  response = await connection.command(Buffer.from(user, "utf8").toString("base64"));
1433
- if (response.code !== 334) return negative("auth", response.code);
1469
+ if (response.code !== 334) return negative({ stage: "auth" }, response.code);
1434
1470
  response = await connection.command(Buffer.from(pass, "utf8").toString("base64"));
1435
1471
  } else return failed("auth", "provider_configuration");
1436
- if (response.code !== 235) return negative("auth", response.code);
1472
+ if (response.code !== 235) return negative({ stage: "auth" }, response.code);
1437
1473
  }
1438
1474
  async function exchange(connection, config, message) {
1439
1475
  await connection.connect();
1440
1476
  if (config.tls === "implicit") await connection.startTls();
1441
1477
  connection.stage = "connect";
1442
1478
  const greeting = await connection.greeting();
1443
- if (greeting.code !== 220) return negative("connect", greeting.code);
1479
+ if (greeting.code !== 220) return negative({ stage: "connect" }, greeting.code);
1444
1480
  let ehlo = await connection.command("EHLO email.invalid");
1445
- if (ehlo.code !== 250) return negative("connect", ehlo.code);
1481
+ if (ehlo.code !== 250) return negative({ stage: "connect" }, ehlo.code);
1446
1482
  if (config.tls === "starttls") {
1447
1483
  connection.stage = "tls";
1448
1484
  if (!extensions(ehlo).starttls) return failed("tls", "provider_configuration");
1449
1485
  const upgrade = await connection.command("STARTTLS");
1450
- if (upgrade.code !== 220) return negative("tls", upgrade.code);
1486
+ if (upgrade.code !== 220) return negative({ stage: "tls" }, upgrade.code);
1451
1487
  await connection.startTls();
1452
1488
  ehlo = await connection.command("EHLO email.invalid");
1453
- if (ehlo.code !== 250) return negative("tls", ehlo.code);
1489
+ if (ehlo.code !== 250) return negative({ stage: "tls" }, ehlo.code);
1454
1490
  }
1455
1491
  if (config.auth) {
1456
1492
  const failure = await authenticate(connection, config.auth, extensions(ehlo).mechanisms);
@@ -1458,7 +1494,7 @@ async function exchange(connection, config, message) {
1458
1494
  }
1459
1495
  connection.stage = "envelope";
1460
1496
  let { code } = await connection.command(`MAIL FROM:<${message.from}>`);
1461
- if (code !== 250) return negative("envelope", code);
1497
+ if (code !== 250) return negative({ stage: "envelope" }, code);
1462
1498
  const recipients = [];
1463
1499
  let accepted = 0;
1464
1500
  for (const { address, group, index } of message.destinations) {
@@ -1472,26 +1508,20 @@ async function exchange(connection, config, message) {
1472
1508
  if (code === 421) break;
1473
1509
  } else throw new SmtpFailure("transport_uncertain");
1474
1510
  }
1475
- const rejected = recipients.filter(({ status }) => status === "rejected");
1511
+ const rejected = recipients.filter((recipient) => recipient.status === "rejected");
1476
1512
  if (accepted === 0 && recipients.length === message.destinations.length) {
1477
- return negative("envelope", code, connection.permanentRcptRefusal, rejected);
1513
+ return negative({ stage: "envelope", recipients: rejected }, code, connection.permanentRcptRefusal);
1478
1514
  }
1479
1515
  if (code === 421) {
1480
1516
  return failed("envelope", connection.permanentRcptRefusal ? "transport_uncertain" : "not_transmitted");
1481
1517
  }
1482
1518
  connection.stage = "data";
1519
+ const refusal = { stage: "data", recipients: rejected };
1483
1520
  ({ code } = await connection.command("DATA"));
1484
- if (code !== 354) return negative("data", code, connection.permanentRcptRefusal, rejected);
1521
+ if (code !== 354) return negative(refusal, code, connection.permanentRcptRefusal);
1485
1522
  ({ code } = await connection.data(message.openMimeStream));
1486
- if (code !== 250) return negative("data", code, connection.permanentRcptRefusal, rejected);
1487
- return {
1488
- outcome: rejected.length ? "partially_accepted" : "accepted",
1489
- retry: "never",
1490
- stage: "data",
1491
- reason: rejected.length ? "smtp_partial" : "smtp_accepted",
1492
- messageId: message.messageId,
1493
- recipients
1494
- };
1523
+ if (code !== 250) return negative(refusal, code, connection.permanentRcptRefusal);
1524
+ return createSmtpAcceptanceResult(recipients, message.messageId);
1495
1525
  }
1496
1526
  async function submitSmtp(config, message, call, resolverFactory) {
1497
1527
  const connection = new SmtpConnection(config, call, resolverFactory);
package/smtp.mjs CHANGED
@@ -346,7 +346,7 @@ function encodeEvidence(input, code, capacity) {
346
346
 
347
347
  // src/internal/smtp-capacity.ts
348
348
  var MIN_RECIPIENTS = MESSAGE_LIMITS.recipients + 1;
349
- var MAX_RECIPIENTS = 1e3;
349
+ var MAX_SMTP_RECIPIENTS = 1e3;
350
350
  var KIB = 1024;
351
351
  var limits = Object.freeze({
352
352
  snapshotBytes: 2 * KIB * KIB,
@@ -374,7 +374,7 @@ function readBinding(input, code) {
374
374
  return Object.freeze({
375
375
  format,
376
376
  namespace: uuidV4(namespace, code),
377
- maxRecipients: integer(maxRecipients, MIN_RECIPIENTS, MAX_RECIPIENTS, code)
377
+ maxRecipients: integer(maxRecipients, MIN_RECIPIENTS, MAX_SMTP_RECIPIENTS, code)
378
378
  });
379
379
  }
380
380
  function readCapacityOption(raw) {
@@ -451,6 +451,57 @@ function recipientCounts(envelope) {
451
451
  function smtpMessageId(attemptId) {
452
452
  return `<${attemptId}@email.invalid>`;
453
453
  }
454
+ function readRecipientResults(input, code, { limit, allowAccepted, ordered }) {
455
+ const seen = /* @__PURE__ */ new Set();
456
+ let previous = -1;
457
+ return readArray(input, limit, code, (value) => {
458
+ const { group, index: rawIndex, status } = readRecord(value, ["group", "index", "status"], [], code);
459
+ if (group !== "to" && group !== "cc" && group !== "bcc") invalid(code);
460
+ if (status !== "rejected" && (status !== "accepted" || !allowAccepted)) invalid(code);
461
+ const index = integer(rawIndex, 0, limit - 1, code);
462
+ if (ordered) {
463
+ const position = recipientGroups.indexOf(group) * limit + index;
464
+ if (position <= previous) invalid(code);
465
+ previous = position;
466
+ } else {
467
+ const key = `${group}:${index}`;
468
+ if (seen.has(key)) invalid(code);
469
+ seen.add(key);
470
+ }
471
+ return Object.freeze({ group, index, status });
472
+ });
473
+ }
474
+ function allAccepted(recipients) {
475
+ return recipients.length !== 0 && recipients.every(({ status }) => status === "accepted");
476
+ }
477
+ function createSmtpAcceptanceResult(recipients, messageId) {
478
+ const code = "invalid_command";
479
+ if (messageId !== void 0 && (typeof messageId !== "string" || messageId.length !== 52 || messageId !== smtpMessageId(uuidV4(messageId.slice(1, 37), code)))) {
480
+ invalid(code);
481
+ }
482
+ const checked = readRecipientResults(recipients, code, {
483
+ limit: MAX_SMTP_RECIPIENTS,
484
+ allowAccepted: true,
485
+ ordered: false
486
+ });
487
+ const data = { retry: "never", stage: "data", ...messageId === void 0 ? {} : { messageId } };
488
+ if (allAccepted(checked)) {
489
+ return Object.freeze({ ...data, reason: "smtp_accepted", outcome: "accepted", recipients: checked });
490
+ }
491
+ if (!checked.some(({ status }) => status === "accepted")) invalid(code);
492
+ return Object.freeze({
493
+ ...data,
494
+ reason: "smtp_partial",
495
+ outcome: "partially_accepted",
496
+ recipients: checked
497
+ });
498
+ }
499
+ function uncertainProviderResult(stage, reason) {
500
+ const base = { outcome: "unknown", retry: "never", stage };
501
+ if (reason === "provider_timeout") return { ...base, reason };
502
+ if (reason === "provider_aborted") return { ...base, reason };
503
+ return { ...base, reason };
504
+ }
454
505
  function readHint(input, reason, code) {
455
506
  if (reason !== "rate_deferred" && reason !== "sendgrid_throttled") invalid(code);
456
507
  const raw = readRecord(input, [], ["delayMs", "notBefore"], code);
@@ -484,24 +535,10 @@ function readAttemptResult(input, code = "invalid_command", projected = false, c
484
535
  let recipients;
485
536
  if (hasRecipients) {
486
537
  if (origin !== "smtp" && origin !== "provider" || stage !== "envelope" && stage !== "data") invalid(code);
487
- const seen = /* @__PURE__ */ new Set();
488
- const limit = recipientLimit(capacity);
489
- let previous = -1;
490
- recipients = readArray(raw.recipients, limit, code, (value) => {
491
- const { group, index: rawIndex, status } = readRecord(value, ["group", "index", "status"], [], code);
492
- if (group !== "to" && group !== "cc" && group !== "bcc") invalid(code);
493
- if (status !== "rejected" && (status !== "accepted" || !smtpSuccess)) invalid(code);
494
- const index = integer(rawIndex, 0, limit - 1, code);
495
- if (capacity) {
496
- const position = recipientGroups.indexOf(group) * limit + index;
497
- if (position <= previous) invalid(code);
498
- previous = position;
499
- } else {
500
- const key = `${group}:${index}`;
501
- if (seen.has(key)) invalid(code);
502
- seen.add(key);
503
- }
504
- return Object.freeze({ group, index, status });
538
+ recipients = readRecipientResults(raw.recipients, code, {
539
+ limit: recipientLimit(capacity),
540
+ allowAccepted: smtpSuccess,
541
+ ordered: Boolean(capacity)
505
542
  });
506
543
  const accepted = recipients.filter(({ status }) => status === "accepted").length;
507
544
  if (reason === "smtp_accepted" && (!accepted || accepted !== recipients.length) || reason === "smtp_partial" && (!accepted || accepted === recipients.length)) {
@@ -911,6 +948,13 @@ function readProviderCall(input) {
911
948
  // src/providers/smtp/mime.ts
912
949
  import MailComposer from "nodemailer/lib/mail-composer";
913
950
  import { encodeWord } from "nodemailer/lib/mime-funcs";
951
+
952
+ // src/internal/smtp-attachments.ts
953
+ function assertSmtpAttachmentTypes(refs) {
954
+ if (refs.some(({ contentType }) => /^(?:message|multipart)\//.test(contentType))) invalid("invalid_snapshot");
955
+ }
956
+
957
+ // src/providers/smtp/mime.ts
914
958
  function body(content) {
915
959
  if (content !== void 0) return { content: Buffer.from(content, "utf8"), contentTransferEncoding: "base64" };
916
960
  }
@@ -922,6 +966,7 @@ function prepareSmtpMessage({
922
966
  attemptId,
923
967
  attachments
924
968
  }) {
969
+ assertSmtpAttachmentTypes(attachments.map(({ ref }) => ref));
925
970
  const { from, to, cc, replyTo } = snapshot.envelope;
926
971
  const destinations = recipientGroups.flatMap(
927
972
  (group) => snapshot.envelope[group].map(({ address }, index) => Object.freeze({ address, group, index }))
@@ -939,16 +984,13 @@ function prepareSmtpMessage({
939
984
  html: body(snapshot.html),
940
985
  date: new Date(snapshot.preparedAt),
941
986
  messageId,
942
- attachments: attachments.map(({ ref, bytes }) => {
943
- if (/^(?:multipart|message)\//.test(ref.contentType)) invalid("invalid_snapshot");
944
- return {
945
- filename: ref.filename,
946
- contentType: ref.contentType,
947
- content: Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength),
948
- contentDisposition: "attachment",
949
- contentTransferEncoding: "base64"
950
- };
951
- }),
987
+ attachments: attachments.map(({ ref, bytes }) => ({
988
+ filename: ref.filename,
989
+ contentType: ref.contentType,
990
+ content: Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength),
991
+ contentDisposition: "attachment",
992
+ contentTransferEncoding: "base64"
993
+ })),
952
994
  disableFileAccess: true,
953
995
  disableUrlAccess: true,
954
996
  newline: "win"
@@ -1355,18 +1397,12 @@ var SmtpConnection = class {
1355
1397
  function failed(stage, reason) {
1356
1398
  if (reason === "not_transmitted") return { outcome: "unsubmitted", retry: "safe_transient", stage, reason };
1357
1399
  if (reason === "provider_configuration") return { outcome: "unsubmitted", retry: "never", stage, reason };
1358
- return { outcome: "unknown", retry: "never", stage, reason };
1400
+ return uncertainProviderResult(stage, reason);
1359
1401
  }
1360
- function negative(stage, code, permanentRcptRefusal = false, recipients = []) {
1402
+ function negative(at, code, permanentRcptRefusal = false) {
1361
1403
  if (code < 400 || code > 599) throw new SmtpFailure("transport_uncertain");
1362
- const permanent = code >= 500 || permanentRcptRefusal;
1363
- return {
1364
- outcome: "rejected",
1365
- retry: permanent ? "never" : "safe_transient",
1366
- stage,
1367
- reason: permanent ? "smtp_permanent_reply" : "smtp_transient_reply",
1368
- ...recipients.length ? { recipients } : {}
1369
- };
1404
+ const details = at.recipients?.length ? at : { stage: at.stage };
1405
+ return code >= 500 || permanentRcptRefusal ? { ...details, outcome: "rejected", retry: "never", reason: "smtp_permanent_reply" } : { ...details, outcome: "rejected", retry: "safe_transient", reason: "smtp_transient_reply" };
1370
1406
  }
1371
1407
  function extensions({ lines }) {
1372
1408
  let starttls = false;
@@ -1387,34 +1423,34 @@ async function authenticate(connection, { user, pass }, mechanisms) {
1387
1423
  if (command.length <= 510) response = await connection.command(command);
1388
1424
  else {
1389
1425
  response = await connection.command("AUTH PLAIN");
1390
- if (response.code !== 334) return negative("auth", response.code);
1426
+ if (response.code !== 334) return negative({ stage: "auth" }, response.code);
1391
1427
  response = await connection.command(encoded);
1392
1428
  }
1393
1429
  } else if (mechanisms.has("LOGIN")) {
1394
1430
  response = await connection.command("AUTH LOGIN");
1395
- if (response.code !== 334) return negative("auth", response.code);
1431
+ if (response.code !== 334) return negative({ stage: "auth" }, response.code);
1396
1432
  response = await connection.command(Buffer.from(user, "utf8").toString("base64"));
1397
- if (response.code !== 334) return negative("auth", response.code);
1433
+ if (response.code !== 334) return negative({ stage: "auth" }, response.code);
1398
1434
  response = await connection.command(Buffer.from(pass, "utf8").toString("base64"));
1399
1435
  } else return failed("auth", "provider_configuration");
1400
- if (response.code !== 235) return negative("auth", response.code);
1436
+ if (response.code !== 235) return negative({ stage: "auth" }, response.code);
1401
1437
  }
1402
1438
  async function exchange(connection, config, message) {
1403
1439
  await connection.connect();
1404
1440
  if (config.tls === "implicit") await connection.startTls();
1405
1441
  connection.stage = "connect";
1406
1442
  const greeting = await connection.greeting();
1407
- if (greeting.code !== 220) return negative("connect", greeting.code);
1443
+ if (greeting.code !== 220) return negative({ stage: "connect" }, greeting.code);
1408
1444
  let ehlo = await connection.command("EHLO email.invalid");
1409
- if (ehlo.code !== 250) return negative("connect", ehlo.code);
1445
+ if (ehlo.code !== 250) return negative({ stage: "connect" }, ehlo.code);
1410
1446
  if (config.tls === "starttls") {
1411
1447
  connection.stage = "tls";
1412
1448
  if (!extensions(ehlo).starttls) return failed("tls", "provider_configuration");
1413
1449
  const upgrade = await connection.command("STARTTLS");
1414
- if (upgrade.code !== 220) return negative("tls", upgrade.code);
1450
+ if (upgrade.code !== 220) return negative({ stage: "tls" }, upgrade.code);
1415
1451
  await connection.startTls();
1416
1452
  ehlo = await connection.command("EHLO email.invalid");
1417
- if (ehlo.code !== 250) return negative("tls", ehlo.code);
1453
+ if (ehlo.code !== 250) return negative({ stage: "tls" }, ehlo.code);
1418
1454
  }
1419
1455
  if (config.auth) {
1420
1456
  const failure = await authenticate(connection, config.auth, extensions(ehlo).mechanisms);
@@ -1422,7 +1458,7 @@ async function exchange(connection, config, message) {
1422
1458
  }
1423
1459
  connection.stage = "envelope";
1424
1460
  let { code } = await connection.command(`MAIL FROM:<${message.from}>`);
1425
- if (code !== 250) return negative("envelope", code);
1461
+ if (code !== 250) return negative({ stage: "envelope" }, code);
1426
1462
  const recipients = [];
1427
1463
  let accepted = 0;
1428
1464
  for (const { address, group, index } of message.destinations) {
@@ -1436,26 +1472,20 @@ async function exchange(connection, config, message) {
1436
1472
  if (code === 421) break;
1437
1473
  } else throw new SmtpFailure("transport_uncertain");
1438
1474
  }
1439
- const rejected = recipients.filter(({ status }) => status === "rejected");
1475
+ const rejected = recipients.filter((recipient) => recipient.status === "rejected");
1440
1476
  if (accepted === 0 && recipients.length === message.destinations.length) {
1441
- return negative("envelope", code, connection.permanentRcptRefusal, rejected);
1477
+ return negative({ stage: "envelope", recipients: rejected }, code, connection.permanentRcptRefusal);
1442
1478
  }
1443
1479
  if (code === 421) {
1444
1480
  return failed("envelope", connection.permanentRcptRefusal ? "transport_uncertain" : "not_transmitted");
1445
1481
  }
1446
1482
  connection.stage = "data";
1483
+ const refusal = { stage: "data", recipients: rejected };
1447
1484
  ({ code } = await connection.command("DATA"));
1448
- if (code !== 354) return negative("data", code, connection.permanentRcptRefusal, rejected);
1485
+ if (code !== 354) return negative(refusal, code, connection.permanentRcptRefusal);
1449
1486
  ({ code } = await connection.data(message.openMimeStream));
1450
- if (code !== 250) return negative("data", code, connection.permanentRcptRefusal, rejected);
1451
- return {
1452
- outcome: rejected.length ? "partially_accepted" : "accepted",
1453
- retry: "never",
1454
- stage: "data",
1455
- reason: rejected.length ? "smtp_partial" : "smtp_accepted",
1456
- messageId: message.messageId,
1457
- recipients
1458
- };
1487
+ if (code !== 250) return negative(refusal, code, connection.permanentRcptRefusal);
1488
+ return createSmtpAcceptanceResult(recipients, message.messageId);
1459
1489
  }
1460
1490
  async function submitSmtp(config, message, call, resolverFactory) {
1461
1491
  const connection = new SmtpConnection(config, call, resolverFactory);