@ultimat3/mail 31.35.0 → 31.36.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ultimat3/mail",
3
- "version": "31.35.0",
3
+ "version": "31.36.0",
4
4
  "description": "Transactional email as data: one template renders HTML and text, sent through a job.",
5
5
  "keywords": [
6
6
  "ultimate",
@@ -44,11 +44,11 @@
44
44
  "test": "bun test"
45
45
  },
46
46
  "dependencies": {
47
- "@ultimat3/core": "31.35.0",
48
- "@ultimat3/i18n": "31.35.0",
49
- "@ultimat3/jobs": "31.35.0",
50
- "@ultimat3/schema": "31.35.0",
51
- "@ultimat3/storage": "31.35.0",
52
- "@ultimat3/time": "31.35.0"
47
+ "@ultimat3/core": "31.36.0",
48
+ "@ultimat3/i18n": "31.36.0",
49
+ "@ultimat3/jobs": "31.36.0",
50
+ "@ultimat3/schema": "31.36.0",
51
+ "@ultimat3/storage": "31.36.0",
52
+ "@ultimat3/time": "31.36.0"
53
53
  }
54
54
  }
package/src/driver-env.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  // is that something, and it is keyed on env rather than a config field so the same image deploys to
6
6
  // every environment.
7
7
 
8
- import { ConfigInvalidError, isLocal, resolveEnvironment } from '@ultimat3/core';
8
+ import { ConfigInvalidError, isLocal, renderFixShellArg, resolveEnvironment } from '@ultimat3/core';
9
9
  import { type MailDriver, memoryMailDriver, unconfiguredMailDriver } from './driver';
10
10
  import { resendMailDriver } from './driver-resend';
11
11
  import { sesMailDriver } from './driver-ses';
@@ -122,7 +122,7 @@ export function selectMailDriver(
122
122
  if (selected.length > 1) {
123
123
  throw new ConfigInvalidError({
124
124
  cause: `${selected.join(' and ')} are ${selected.length === 2 ? 'both' : 'all'} set — ${selected.length} transports claim the same mail`,
125
- fix: 'unset one of them in .env.production: a process delivers through exactly one transport',
125
+ fix: `sed -i.bak '/^${renderFixShellArg(selected[selected.length - 1], 'SMTP_URL')}=/d' .env.production # or drop another: keep one of ${selected.join(', ')} — a process delivers through exactly one transport`,
126
126
  meta: { selected },
127
127
  });
128
128
  }
@@ -46,7 +46,7 @@ function requireApiKey(apiKey: string): string {
46
46
  if (apiKey.trim() !== '') return apiKey;
47
47
  throw new EnvMissingError({
48
48
  cause: 'RESEND_API_KEY is not set, so the resend driver cannot authenticate',
49
- fix: "set RESEND_API_KEY in .env (or the container's secret store), then re-run",
49
+ fix: "RESEND_API_KEY=re_... # in .env (or the container's secret store), then re-run",
50
50
  meta: { missing: 'RESEND_API_KEY' },
51
51
  });
52
52
  }
@@ -55,7 +55,7 @@ function requireFrom(from: string): string {
55
55
  if (from.trim() !== '') return from;
56
56
  throw new ConfigInvalidError({
57
57
  cause: 'the resend driver was configured without a from address',
58
- fix: 'set mail.from in app.config.ts to a domain verified with Resend',
58
+ fix: "resendMailDriver({ apiKey, from: 'App <no-reply@yourdomain.test>' }) // a domain verified with Resend; selectMailDriver(env) reads MAIL_FROM",
59
59
  });
60
60
  }
61
61
 
@@ -67,19 +67,19 @@ function parseSmtpUrl(raw: string): SmtpUrl {
67
67
  } catch {
68
68
  throw new ConfigInvalidError({
69
69
  cause: 'SMTP_URL is not a URL',
70
- fix: 'set SMTP_URL to smtps://user:pass@host:465 (implicit TLS) or smtp://host:587',
70
+ fix: 'SMTP_URL="smtps://user:pass@host:465" # implicit TLS — or smtp://host:587 for STARTTLS',
71
71
  });
72
72
  }
73
73
  if (url.protocol !== 'smtp:' && url.protocol !== 'smtps:') {
74
74
  throw new ConfigInvalidError({
75
75
  cause: `SMTP_URL uses the "${url.protocol}" scheme, which is not an SMTP one`,
76
- fix: 'set SMTP_URL to smtps://user:pass@host:465 (implicit TLS) or smtp://host:587',
76
+ fix: 'SMTP_URL="smtps://user:pass@host:465" # implicit TLS — or smtp://host:587 for STARTTLS',
77
77
  });
78
78
  }
79
79
  if (url.hostname === '') {
80
80
  throw new ConfigInvalidError({
81
81
  cause: 'SMTP_URL names no host',
82
- fix: 'set SMTP_URL to smtps://user:pass@host:465 (implicit TLS) or smtp://host:587',
82
+ fix: 'SMTP_URL="smtps://user:pass@host:465" # implicit TLS — or smtp://host:587 for STARTTLS',
83
83
  });
84
84
  }
85
85
  const tls = url.protocol === 'smtps:';
@@ -105,7 +105,7 @@ function decodedCredential(part: 'user' | 'password', raw: string): string {
105
105
  throw new ConfigInvalidError({
106
106
  cause: `the SMTP_URL ${part} holds a "%" that starts no percent-escape, so it cannot be decoded`,
107
107
  // `prompt`, so the credential reaches neither shell history nor this message.
108
- fix: `percent-encode the ${part} and put the output in SMTP_URL: bun -e 'console.log(encodeURIComponent(prompt("${renderFixShellArg(part, 'value')}:") ?? ""))'`,
108
+ fix: `bun -e 'console.log(encodeURIComponent(prompt("${renderFixShellArg(part, 'value')}:") ?? ""))' # percent-encodes the ${part}: put the output in SMTP_URL`,
109
109
  meta: { part },
110
110
  });
111
111
  }
@@ -122,7 +122,7 @@ function resolvePoolSize(poolSize: number | undefined): number {
122
122
  if (!Number.isInteger(poolSize) || poolSize < 1) {
123
123
  throw new ConfigInvalidError({
124
124
  cause: `the smtp driver was configured with poolSize: ${poolSize}, which opens no connection`,
125
- fix: `set poolSize to a whole number >= 1 in app.config.ts, or drop it for ${DEFAULT_POOL_SIZE}`,
125
+ fix: `smtpMailDriver({ url, from, poolSize: 1 }) // a whole number >= 1, or drop it for ${DEFAULT_POOL_SIZE}; selectMailDriver(env) reads MAIL_POOL_SIZE`,
126
126
  meta: { poolSize },
127
127
  });
128
128
  }
@@ -157,7 +157,7 @@ export function smtpMailDriver(options: SmtpDriverOptions): MailDriver {
157
157
  if (options.from.trim() === '') {
158
158
  throw new ConfigInvalidError({
159
159
  cause: 'the smtp driver was configured without a from address',
160
- fix: 'set mail.from in app.config.ts to an address the SMTP server will relay for',
160
+ fix: "smtpMailDriver({ url, from: 'App <no-reply@yourdomain.test>' }) // an address the SMTP server will relay for; selectMailDriver(env) reads MAIL_FROM",
161
161
  });
162
162
  }
163
163
  const connect = options.connect ?? bunSmtpStream;
package/src/errors.ts CHANGED
@@ -122,7 +122,7 @@ export const localeMissing = (mailId: string): MailError =>
122
122
  new MailError({
123
123
  code: 'X_MAIL_LOCALE_MISSING',
124
124
  cause: `send(${mailId}, data, options) got no locale — the recipient's language is unknown`,
125
- fix: `pass it: send(${mailId}, data, { to, locale: ctx.locale })`,
125
+ fix: `send(${mailId}, data, { to, locale: ctx.locale }) // pass the recipient's locale`,
126
126
  meta: { mailId },
127
127
  });
128
128
 
@@ -150,7 +150,7 @@ export const mailDuplicate = (mailId: string): MailError =>
150
150
  new MailError({
151
151
  code: 'X_MAIL_DUPLICATE',
152
152
  cause: `mail id "${mailId}" is already registered by another defineMail() call`,
153
- fix: `rename one of the two defineMail({ id: '${mailId}' }) declarations — an id is the key both surfaces address a template by`,
153
+ fix: `defineMail({ id: '${mailId}-2', ... }) // rename one of the two declarations — an id is the key both surfaces address a template by`,
154
154
  meta: { mailId },
155
155
  });
156
156
 
@@ -166,7 +166,7 @@ export const mailLayoutDuplicate = (name: string): MailError =>
166
166
  new MailError({
167
167
  code: 'X_MAIL_DUPLICATE',
168
168
  cause: `mail layout "${name}" is already registered, so a second registerLayout() would replace it`,
169
- fix: `pick a name of your own: registerLayout('${name}-custom', myLayout) — a layout name is the key defineMail({ layout }) addresses a shell by`,
169
+ fix: `registerLayout('${name}-custom', myLayout) // a name of your own — a layout name is the key defineMail({ layout }) addresses a shell by`,
170
170
  meta: { layout: name },
171
171
  });
172
172
 
@@ -174,7 +174,7 @@ export const textMissing = (mailId: string): MailError =>
174
174
  new MailError({
175
175
  code: 'X_MAIL_TEXT_MISSING',
176
176
  cause: `mail "${mailId}" rendered an empty text part (HTML-only mail scores as spam)`,
177
- fix: `add a text block to the "${mailId}" template: blocks.paragraph('mail.${mailId}.body')`,
177
+ fix: `blocks.paragraph('mail.${mailId}.body') // a text block in the "${mailId}" template`,
178
178
  meta: { mailId },
179
179
  });
180
180
 
@@ -231,10 +231,7 @@ export const mailCredentialMissing = (environment: Environment): MailError =>
231
231
  cause:
232
232
  `none of SMTP_URL, RESEND_API_KEY or SES_REGION is set in ${environment}, so this process ` +
233
233
  'has no transport — the message was not delivered and was not queued anywhere',
234
- fix:
235
- 'set SMTP_URL="smtps://user:pass@host:465" (or RESEND_API_KEY=re_..., or SES_REGION with ' +
236
- 'SES_ACCESS_KEY_ID and SES_SECRET_ACCESS_KEY) and ' +
237
- 'MAIL_FROM="App <no-reply@yourdomain.test>" in the deployment environment, then restart',
234
+ fix: 'SMTP_URL="smtps://user:pass@host:465" MAIL_FROM="App <no-reply@yourdomain.test>" # in the deployment environment, then restart — or RESEND_API_KEY=re_..., or SES_REGION with SES_ACCESS_KEY_ID and SES_SECRET_ACCESS_KEY, in place of SMTP_URL',
238
235
  meta: { environment, missing: ['SMTP_URL', 'RESEND_API_KEY', 'SES_REGION'] },
239
236
  });
240
237
 
@@ -259,7 +256,7 @@ const ADDRESS_OPTION: ReadonlyMap<string, string> = new Map([
259
256
  const nonAsciiFix = (name: string): string => {
260
257
  const option = ADDRESS_OPTION.get(name);
261
258
  return option === undefined
262
- ? 'set mail.from in app.config.ts to an ASCII mailbox, e.g. no-reply@example.test'
259
+ ? 'MAIL_FROM=no-reply@example.test # an ASCII mailbox, in .env.production'
263
260
  : `pass the mailbox's ASCII form, the domain as punycode: send(mail, data, { ${option}: 'jose@xn--exmple-cua.test', locale })`;
264
261
  };
265
262
 
@@ -281,8 +278,7 @@ export const headerInvalid = (
281
278
  : `the "${name}" header of mail "${mailId}" holds a non-ASCII byte in its mailbox, and this package negotiates no SMTPUTF8, so the address cannot be written as a header`,
282
279
  fix:
283
280
  reason === 'line-break'
284
- ? `strip line breaks from the value before it reaches the header: ` +
285
- `t('mail.${mailId}.subject', { ...data, x: String(x).replace(/[\\r\\n]+/g, ' ') })`
281
+ ? `t.string.pattern(/^[^\\r\\n]*$/) // on the input field that feeds the "${name}" header of mail "${mailId}": reject the value at its own boundary, then re-send — never strip the break, a silently rewritten header hides the injection attempt`
286
282
  : nonAsciiFix(name),
287
283
  meta: { header: name, mailId, reason },
288
284
  });
@@ -296,8 +292,8 @@ export const headerInvalid = (
296
292
  export type EnvelopeAddressField = 'sender' | 'recipient';
297
293
 
298
294
  const ADDRESS_FIXES: Readonly<Record<EnvelopeAddressField, string>> = {
299
- sender: 'set mail.from in app.config.ts to a bare address, e.g. no-reply@example.test',
300
- recipient: "pass bare addresses: send(mail, data, { to: ['ada@example.test'], locale })",
295
+ sender: 'MAIL_FROM=no-reply@example.test # a bare address, in .env.production',
296
+ recipient: "send(mail, data, { to: ['ada@example.test'], locale }) // bare addresses",
301
297
  };
302
298
 
303
299
  /**
@@ -401,6 +397,6 @@ export const transformFailed = (mailId: string, reason: string): MailError =>
401
397
  new MailError({
402
398
  code: 'X_MAIL_TRANSFORM_FAILED',
403
399
  cause: `the mail transform failed for "${mailId}": ${reason} — the mail was not sent`,
404
- fix: `make the setMailTransform() hook return { subject, html, text } strings for "${mailId}" (return the rendered argument unchanged for a mail it does not track), then retry the send`,
400
+ fix: `setMailTransform((rendered, meta) => (meta.mailName === '${mailId}' ? track(rendered) : rendered)) // the hook returns { subject, html, text } strings — the rendered argument unchanged for a mail it does not track — then retry the send`,
405
401
  meta: { mailId },
406
402
  });
@@ -69,14 +69,17 @@ const QUIT_TIMEOUT_MS = 5_000;
69
69
  // falls back rather than a silent typo. `Partial` because `connect` and `request` never reach a
70
70
  // server reply: one belongs to the socket and the other to the HTTPS transport.
71
71
  const FIXES: Readonly<Partial<Record<SendStage, string>>> = {
72
- greeting: 'correct the host and port in SMTP_URL — the server did not open with 220',
73
- ehlo: 'point SMTP_URL at an ESMTP server (submission on 587, implicit TLS on 465)',
74
- reply: 'point SMTP_URL at the SMTP port itself — a proxy or an HTTP port answers like this',
75
- tls: 'fix the certificate on the implicit-TLS port, or use smtp://host:587 and STARTTLS',
76
- starttls: 'set SMTP_URL in .env to smtps://host:465',
77
- auth: 'set SMTP_URL in .env to smtps://user:password@host:465 with a valid user and password',
78
- from: 'set mail.from in app.config.ts to an address this server is willing to relay for',
79
- recipient: 'the reply above names the address the server refused — correct or drop it',
72
+ greeting:
73
+ 'SMTP_URL="smtps://user:pass@host:465" # correct the host and port — the server did not open with 220',
74
+ ehlo: 'SMTP_URL="smtp://host:587" # an ESMTP server: submission on 587, implicit TLS on 465',
75
+ reply:
76
+ 'SMTP_URL="smtp://host:587" # the SMTP port itself — a proxy or an HTTP port answers like this',
77
+ tls: 'SMTP_URL="smtp://host:587" # STARTTLS — or fix the certificate on the implicit-TLS port',
78
+ starttls: 'SMTP_URL="smtps://host:465" # in .env',
79
+ auth: 'SMTP_URL="smtps://user:password@host:465" # in .env, with a valid user and password',
80
+ from: 'MAIL_FROM="App <no-reply@yourdomain.test>" # in .env.production — an address this server is willing to relay for',
81
+ recipient:
82
+ "send(mail, data, { to: ['ada@example.test'], locale }) // the reply above names the address the server refused — correct or drop it",
80
83
  data: 'the reply above says why the body was refused (size, content or policy)',
81
84
  quit: 'nothing to fix: the message was already accepted before the goodbye failed',
82
85
  };
@@ -88,7 +91,7 @@ const refused = (stage: SendStage, reply: SmtpReply): MailError =>
88
91
  detail: replySummary(reply),
89
92
  status: reply.code,
90
93
  retryable: isTransient(reply.code),
91
- fix: FIXES[stage] ?? 'run x doctor --json and check the mail section',
94
+ fix: FIXES[stage] ?? 'x doctor --json # and check the mail section',
92
95
  });
93
96
 
94
97
  /** Reads whole replies off a chunked stream, with a deadline on every one of them. */
@@ -115,7 +118,7 @@ class Conversation {
115
118
  'the server sent bytes after its STARTTLS 220 and before the TLS handshake — plaintext a ' +
116
119
  'man-in-the-middle can append (RFC 3207 §4.2), so the session is refused before any credential',
117
120
  retryable: false,
118
- fix: FIXES['starttls'] ?? 'set SMTP_URL in .env to smtps://host:465',
121
+ fix: FIXES['starttls'] ?? 'SMTP_URL="smtps://host:465" # in .env',
119
122
  });
120
123
  }
121
124
  this.parser = createReplyParser();
@@ -161,7 +164,7 @@ class Conversation {
161
164
  stage,
162
165
  detail: `the server sent nothing for ${timeoutMs}ms, so the read deadline expired`,
163
166
  retryable: true,
164
- fix: 'pass timeoutMs: 60_000 to smtpMailDriver() in app.config.ts',
167
+ fix: 'smtpMailDriver({ url, from, timeoutMs: 60_000 }) // where the driver is built',
165
168
  }),
166
169
  );
167
170
  }, timeoutMs);
@@ -176,7 +179,7 @@ class Conversation {
176
179
  'the server closed the connection mid-conversation, which is usually it ' +
177
180
  'rate-limiting the sessions it keeps open at once',
178
181
  retryable: true,
179
- fix: 'pass poolSize: 1 to smtpMailDriver() in app.config.ts',
182
+ fix: 'MAIL_POOL_SIZE=1 # in .env.production — or smtpMailDriver({ url, from, poolSize: 1 })',
180
183
  });
181
184
  }
182
185
  return chunk;
@@ -232,7 +235,7 @@ export async function smtpDeliver(
232
235
  'the server does not advertise STARTTLS and the connection is not already TLS; ' +
233
236
  'allowInsecure: true on smtpMailDriver() would send this in the clear instead',
234
237
  retryable: false,
235
- fix: FIXES['starttls'] ?? 'set SMTP_URL in .env to smtps://host:465',
238
+ fix: FIXES['starttls'] ?? 'SMTP_URL="smtps://host:465" # in .env',
236
239
  });
237
240
  }
238
241
 
@@ -278,6 +281,6 @@ async function authenticate(
278
281
  mechanisms.join(', ') || 'none'
279
282
  })`,
280
283
  retryable: false,
281
- fix: 'drop user:password from SMTP_URL if the server wants none, or enable AUTH PLAIN on it',
284
+ fix: 'SMTP_URL="smtps://host:465" # drop user:password if the server wants none — or enable AUTH PLAIN on it',
282
285
  });
283
286
  }
@@ -61,7 +61,7 @@ export function createReplyParser(): ReplyParser {
61
61
  stage: 'reply',
62
62
  detail: `the server sent more than ${MAX_REPLY_BYTES} bytes without completing one reply`,
63
63
  retryable: true,
64
- fix: 'point SMTP_URL at the SMTP port itself — a proxy or an HTTP port answers like this',
64
+ fix: 'SMTP_URL="smtp://host:587" # the SMTP port itself — a proxy or an HTTP port answers like this',
65
65
  });
66
66
  };
67
67
 
@@ -2,6 +2,7 @@
2
2
  // handlers while the conversation pulls replies, so a queue sits between them; `startTls` swaps in
3
3
  // the upgraded socket for STARTTLS. Nothing here knows an SMTP verb — that is `smtp-client.ts`.
4
4
 
5
+ import { renderFixShellArg } from '@ultimat3/core';
5
6
  import { type MailError, sendFailed } from './errors';
6
7
  import type { SmtpStream, SmtpTarget } from './smtp-client';
7
8
 
@@ -120,6 +121,8 @@ export function smtpStreamOver(runtime: BunConnect, target: SmtpTarget): Promise
120
121
  // `smtps://` hands the socket to TLS before a single SMTP byte is exchanged, so the window is
121
122
  // already open when the connection is made; a plaintext one opens it at `startTls()` or never.
122
123
  let handshake: Handshake = target.tls ? 'tls' : undefined;
124
+ // Screened once: both handshake fixes splice it, and a fix is pasted into a shell.
125
+ const endpoint = renderFixShellArg(`${target.host}:${String(target.port)}`, '<SMTP host>:<port>');
123
126
 
124
127
  /**
125
128
  * A failed handshake is a refused certificate or a protocol mismatch, and the same attempt fails
@@ -133,8 +136,9 @@ export function smtpStreamOver(runtime: BunConnect, target: SmtpTarget): Promise
133
136
  detail: `the TLS handshake with ${target.host} failed: ${detail}`,
134
137
  retryable: false,
135
138
  fix:
136
- `check the certificate and protocol the host offers: openssl s_client ` +
137
- `${kind === 'starttls' ? '-starttls smtp ' : ''}-connect ${target.host}:${target.port}`,
139
+ kind === 'starttls'
140
+ ? `openssl s_client -starttls smtp -connect ${endpoint} # the certificate and protocol the host offers`
141
+ : `openssl s_client -connect ${endpoint} # the certificate and protocol the host offers`,
138
142
  });
139
143
 
140
144
  /** Outside a handshake, a dead socket really is transient — a reset, a rate limit, a restart. */
@@ -229,7 +233,7 @@ export function smtpStreamOver(runtime: BunConnect, target: SmtpTarget): Promise
229
233
  stage: 'data',
230
234
  detail: `the socket stopped accepting bytes for ${target.timeoutMs}ms`,
231
235
  retryable: true,
232
- fix: 'pass timeoutMs: 60_000 to smtpMailDriver() in app.config.ts',
236
+ fix: 'smtpMailDriver({ url, from, timeoutMs: 60_000 }) // where the driver is built',
233
237
  }),
234
238
  );
235
239
  }, target.timeoutMs);
@@ -281,7 +285,7 @@ export function smtpStreamOver(runtime: BunConnect, target: SmtpTarget): Promise
281
285
  stage: 'starttls',
282
286
  detail: 'the runtime returned no TLS socket for the STARTTLS upgrade',
283
287
  retryable: false,
284
- fix: 'upgrade the runtime: bun upgrade # STARTTLS needs bun >= 1.3',
288
+ fix: 'bun upgrade # STARTTLS needs bun >= 1.3',
285
289
  }),
286
290
  );
287
291
  }