@infaris/calendar-cli 0.9.0 → 0.10.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/README.md CHANGED
@@ -294,27 +294,155 @@ infaris-calendar attach task --id 427 ./screenshot.png ./logs.pdf
294
294
 
295
295
  10 MB per file, same limit as the web app's attachment upload.
296
296
 
297
- ## Stage a mail draft
297
+ ## Mail drafts
298
298
 
299
- Prepares an editable concept in your own webmail; it never sends anything.
300
- Open the printed URL to review and send it from `/dashboard/mail`.
299
+ Saves a real draft in Concepten of one of your own mailboxes. `mail draft`
300
+ and `mail batch` never send; only [`mail send --yes`](#sending-drafts) does.
301
+ Your default signature is added below the body (do not put
302
+ a closing or signature in the HTML yourself). Without `--from` the draft goes
303
+ to your primary mailbox; a `--from` you have no access to is refused. The
304
+ printed URL opens the saved draft in `/dashboard/mail/compose`.
301
305
 
302
306
  ```bash
303
- infaris-calendar mail draft --to client@example.com --subject "Offerte" \
304
- --body "<p>Hoi,</p><p>Zie bijlage.</p>" --attach ./offerte.pdf
307
+ # New mail with several recipients, a PDF and a screenshot inline
308
+ infaris-calendar mail draft --to kees@example.com,anna@example.com \
309
+ --cc sales@example.com --subject "Offerte" \
310
+ --body-file ./mail.html --image ./screenshot.png --attach ./offerte.pdf
311
+
312
+ # Images referenced in the body are embedded as well (paths relative to the file)
313
+ echo '<p>Hoi Kees,</p><p>Zo ziet het eruit:</p><img src="shot.png">' > ./mail.html
314
+
315
+ # Markdown (.md) or plain text (.txt) bodies work too
316
+ printf 'Hoi Kees,\n\nZo ziet het eruit:\n\n![scherm](shot.png)\n\n- **LCP** 4,1 s\n' > ./mail.md
317
+ infaris-calendar mail draft --to kees@example.com --subject "Meting" --body-file ./mail.md
318
+
319
+ # Which --from addresses can I use, and which signature goes below?
320
+ infaris-calendar mail mailboxes
321
+
322
+ # Reply to a message: find its UID, read it, stage the reply
323
+ infaris-calendar mail messages --search "offerte"
324
+ infaris-calendar mail read 4812
325
+ infaris-calendar mail draft --reply 4812 --body "<p>Dank je, klopt helemaal.</p>"
326
+
327
+ # Change a draft: save the new version and remove the old one in one go
328
+ infaris-calendar mail drafts
329
+ infaris-calendar mail draft --replace 977 --to kees@example.com \
330
+ --subject "Offerte v2" --body-file ./mail.html
331
+
332
+ # Check images, attachments and sizes without saving anything, or look at it
333
+ infaris-calendar mail draft --to kees@example.com --body-file ./mail.html --dry-run
334
+ infaris-calendar mail draft --to kees@example.com --body-file ./mail.md --preview /tmp/mail.html
335
+
336
+ # Read the saved draft back to check it
337
+ infaris-calendar mail read 978 --drafts
338
+
339
+ # Many drafts at once (outreach): every draft is checked before any is saved
340
+ cat > drafts.json <<'JSON'
341
+ [
342
+ { "to": "kees@example.com", "subject": "Meting", "bodyFile": "kees.md", "images": ["kees.png"] },
343
+ { "to": ["anna@example.com"], "cc": "sales@example.com", "subject": "Offerte",
344
+ "bodyFile": "anna.md", "attach": ["offerte.pdf"] }
345
+ ]
346
+ JSON
347
+ infaris-calendar mail batch drafts.json --dry-run
348
+ infaris-calendar mail batch drafts.json --write-map map.json
349
+
350
+ # Edited the texts? Redo the batch; each draft replaces its predecessor
351
+ # (matched on the first recipient, or the subject when there is none)
352
+ infaris-calendar mail batch drafts.json --replace-map map.json --write-map map.json
353
+ ```
354
+
355
+ `mail draft` flags:
356
+
357
+ | Flag | Description |
358
+ | --- | --- |
359
+ | `--to/--cc/--bcc <emails...>` | Recipients; repeat or comma-separate. Optional with `--reply` |
360
+ | `--subject <text>` | Subject (default `Re:`/`Fwd:` of the original) |
361
+ | `--body <html>` / `--body-file <path>` | Body above the signature (file `-` = stdin); `.md` is Markdown, `.txt` plain text |
362
+ | `--format <html\|markdown\|text>` | Body format when the extension does not say it |
363
+ | `--image <paths...>` | Images shown inline below the body, in order |
364
+ | `--attach <paths...>` | Files to attach (PDF, images, docx, xlsx, ...) |
365
+ | `--from <email>` | Sender: a mailbox address or alias you have access to |
366
+ | `--no-signature` | Leave out the default signature |
367
+ | `--reply <uid>` / `--reply-all <uid>` | Draft as reply: thread headers, `Re:` and the quoted original |
368
+ | `--forward <uid>` | Draft as forward, with the original attachments (`--no-forward-attachments`) |
369
+ | `--folder <path>` | Folder of the original message (default `INBOX`) |
370
+ | `--source-mailbox <id>` | Mailbox of the original (default: the draft mailbox) |
371
+ | `--replace <uid>` | Replace an existing draft in Concepten |
372
+ | `--dry-run` | Print recipients, images and attachments without saving |
373
+ | `--preview <file.html>` | Write the body with inline images to an HTML file; saves nothing |
374
+ | `--no-inline-remote` | Keep `https://` images as links instead of embedding them |
375
+ | `--no-optimize-images` | Do not shrink large inline images |
376
+ | `--json` | Output the saved draft (uid, folder, recipients, files, url) as JSON |
377
+
378
+ Local `<img src>` and Markdown `![](...)` paths in the body (relative to
379
+ `--body-file`, absolute or `file://`) and `--image` files are embedded inline as `cid:` images, so they
380
+ show in the mail itself instead of as a loose attachment. `https://` images
381
+ are downloaded on your machine and embedded too, because mail clients block
382
+ external images by default; one that fails stays a link, with a warning. The
383
+ server shrinks inline images above 200 kB to at most 1600 px wide (PNG
384
+ screenshots without transparency that stay above 1 MB become JPEG); attachments
385
+ are never changed. Images and attachments together may be 20 MB.
386
+
387
+ Other `mail` commands, all read-only except `delete-draft` (removes a draft) and `send` (sends one):
388
+
389
+ | Command | Description |
390
+ | --- | --- |
391
+ | `mail batch <file.json> [--dry-run] [--replace-map f] [--write-map f] [--json]` | Save many drafts; fields as in `mail draft`, paths relative to the file |
392
+ | `mail mailboxes` | Your mailboxes, `--from` addresses and the default signature as text |
393
+ | `mail drafts [--mailbox] [--search] [--limit]` | Newest drafts with UID and link |
394
+ | `mail delete-draft <uid> [--mailbox]` (alias `draft-delete`) | Delete one draft, only from Concepten |
395
+ | `mail messages [--mailbox] [--folder\|--sent] [--search] [--limit]` | List or search messages to find a UID |
396
+ | `mail sent [--mailbox] [--search] [--limit]` | The newest messages in Verzonden |
397
+ | `mail read <uid> [--mailbox] [--folder\|--drafts\|--sent]` | Show one message as text; never marks it read. With `--json` and `--drafts`/`--sent`: recipients incl. Bcc, subject, body and `signatureCount` |
398
+ | `mail status <uid\|message-id> [--mailbox]` | Is it still a draft, really in Verzonden, or gone? |
399
+ | `mail send [uids...] [--all-drafts [--filter q]] --yes` | Send existing drafts; see below |
400
+
401
+ `--mailbox` accepts the mailbox id, its address or an alias. The same draft
402
+ actions exist for Claude Desktop/claude.ai in the Calendar mail MCP
403
+ (`/mcp`): `save_mail_draft`, `list_mail_drafts` and `delete_mail_draft`.
404
+
405
+ ### Sending drafts
406
+
407
+ `mail send` sends drafts that are already in Concepten, the same way the
408
+ **Verzenden** button does: SMTP from the draft's own sender address, a copy in
409
+ Verzonden, the draft removed. The draft goes out unchanged (same Message-ID,
410
+ signature, inline images and attachments); only the Bcc header is dropped from
411
+ the sent message (Bcc recipients still receive it) and the date is set to now.
412
+ Only drafts in a mailbox you have access to can be sent, from an address that
413
+ mailbox may send from. **Run it only when the user explicitly asked to send.**
414
+
415
+ ```bash
416
+ infaris-calendar mail drafts # find the UIDs
417
+ infaris-calendar mail send 4812 --dry-run # recipients, subject, files, signatures
418
+ infaris-calendar mail send 4812 --yes # really send
419
+
420
+ # A batch: 10 s between mails by default, at most 100 per run
421
+ infaris-calendar mail send --all-drafts --filter "subject:Meting" --dry-run
422
+ infaris-calendar mail send --all-drafts --filter "subject:Meting" --yes --delay 15
305
423
 
306
- infaris-calendar mail draft --to client@example.com --subject "Screenshot" \
307
- --body-file ./mail.html --from support@ipulse.one --json
424
+ # After an error or timeout: is it in Verzonden or not?
425
+ infaris-calendar mail status 4812
426
+ infaris-calendar mail sent --search "Meting" --limit 5
308
427
  ```
309
428
 
310
429
  | Flag | Description |
311
430
  | --- | --- |
312
- | `--to <email>` | Recipient (required) |
313
- | `--subject <text>` | Subject |
314
- | `--body <html>` / `--body-file <path>` | HTML body (file `-` = stdin) |
315
- | `--from <email>` | Preferred sender mailbox address |
316
- | `--attach <path>` | Image or PDF to attach (max 10 MB) |
317
- | `--json` | Output the draft id/url as JSON |
431
+ | `--yes` | Required to send. Without it you get the overview and an error |
432
+ | `--dry-run` | Show recipients (To/Cc/Bcc), subject, files and signature count; sends nothing |
433
+ | `--all-drafts` / `--filter <query>` | Every draft in Concepten, narrowed with a Mail search; or pass UIDs |
434
+ | `--delay <seconds>` | Pause between mails of a batch (default 10, `0` for none) |
435
+ | `--max <n>` | Refuse to send more than n (never more than 100 per run) |
436
+ | `--mailbox <id-or-address>` | Mailbox with the drafts (default: your first) |
437
+ | `--json` | Output `{ sent, alreadySent, failed, notAttempted }` |
438
+
439
+ Every draft is checked before the first one goes out; if one is blocked (no
440
+ recipient, a sender you may not use, the signature twice) nothing is sent. A
441
+ failure stops the batch and lists what already went out and what did not run.
442
+ A draft is never sent twice: one that is already in Verzonden is skipped (and
443
+ its leftover draft removed), and a request without an answer is reported as
444
+ "may have been sent": check `mail status` before trying again. The server
445
+ allows 200 sends per login per hour (`MAIL_SEND_HOURLY_LIMIT`).
318
446
 
319
447
  ## List tasks
320
448
 
package/dist/api.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { readFileSync } from "node:fs";
2
- import { extname } from "node:path";
2
+ import { basename, extname } from "node:path";
3
3
  import { getValidCredentials } from "./auth.js";
4
4
  export const PRODUCTS = [
5
5
  "general",
@@ -129,8 +129,16 @@ const EXTENSION_MIME_TYPES = {
129
129
  ".webp": "image/webp",
130
130
  ".svg": "image/svg+xml",
131
131
  ".pdf": "application/pdf",
132
+ ".txt": "text/plain",
133
+ ".csv": "text/csv",
134
+ ".html": "text/html",
135
+ ".ics": "text/calendar",
136
+ ".zip": "application/zip",
137
+ ".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
138
+ ".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
139
+ ".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
132
140
  };
133
- function guessMimeType(filePath) {
141
+ export function guessMimeType(filePath) {
134
142
  return (EXTENSION_MIME_TYPES[extname(filePath).toLowerCase()] ??
135
143
  "application/octet-stream");
136
144
  }
@@ -299,36 +307,26 @@ export async function deleteHours(input) {
299
307
  const data = (await res.json());
300
308
  return { deleted: Boolean(data.deleted), baseUrl };
301
309
  }
310
+ // --- Mail drafts (concepts) ---
311
+ /** Totaal aan bijlagen en inline afbeeldingen per concept (spiegelt de server). */
312
+ export const MAX_DRAFT_BYTES = 20 * 1024 * 1024;
313
+ /** Reads a local file as a draft attachment. */
314
+ export function readDraftAttachment(filePath) {
315
+ return {
316
+ filename: basename(filePath) || "bijlage",
317
+ contentBase64: readFileSync(filePath).toString("base64"),
318
+ contentType: guessMimeType(filePath),
319
+ };
320
+ }
302
321
  /**
303
- * Stages a mail draft in the user's own webmail (never sends). Returns the URL
304
- * to open in `/dashboard/mail` to review, edit and send it.
322
+ * Saves a real draft in the Drafts folder (Concepten) of one of the user's
323
+ * own mailboxes, with the default signature below the body. Never sends.
305
324
  */
306
325
  export async function createMailDraft(input) {
307
- let attachmentBase64;
308
- let attachmentFilename;
309
- let attachmentMimeType;
310
- if (input.attachmentPath) {
311
- const fileBytes = readFileSync(input.attachmentPath);
312
- if (fileBytes.byteLength > MAX_ATTACHMENT_BYTES) {
313
- throw new Error(`${input.attachmentPath} exceeds the 10 MB attachment limit`);
314
- }
315
- attachmentBase64 = fileBytes.toString("base64");
316
- attachmentFilename =
317
- input.attachmentPath.split(/[/\\]/).pop() || "attachment";
318
- attachmentMimeType = guessMimeType(input.attachmentPath);
319
- }
320
326
  const { res, baseUrl } = await authedFetch("/api/mail/drafts", {
321
327
  method: "POST",
322
328
  headers: { "content-type": "application/json" },
323
- body: JSON.stringify({
324
- to: input.to,
325
- subject: input.subject,
326
- html: input.html,
327
- fromAddress: input.fromAddress,
328
- attachmentBase64,
329
- attachmentFilename,
330
- attachmentMimeType,
331
- }),
329
+ body: JSON.stringify(input),
332
330
  });
333
331
  if (!res.ok) {
334
332
  throw new Error(await describeError(res));
@@ -336,6 +334,127 @@ export async function createMailDraft(input) {
336
334
  const data = (await res.json());
337
335
  return { ...data, baseUrl };
338
336
  }
337
+ /**
338
+ * Newest drafts in Concepten of one mailbox (id, address or alias; default the
339
+ * first), optionally narrowed with a Mail search query.
340
+ */
341
+ export async function listMailDrafts(opts = {}) {
342
+ const params = new URLSearchParams();
343
+ if (opts.mailbox)
344
+ params.set("mailbox", opts.mailbox);
345
+ if (opts.limit)
346
+ params.set("limit", String(opts.limit));
347
+ if (opts.search)
348
+ params.set("search", opts.search);
349
+ const { res } = await authedFetch(`/api/mail/drafts?${params}`, {
350
+ method: "GET",
351
+ });
352
+ if (!res.ok)
353
+ throw new Error(await describeError(res));
354
+ return (await res.json());
355
+ }
356
+ /** Deletes one draft from Concepten; never touches another folder. */
357
+ export async function deleteMailDraft(uid, mailbox) {
358
+ const params = new URLSearchParams({ uid: String(uid) });
359
+ if (mailbox)
360
+ params.set("mailbox", mailbox);
361
+ const { res } = await authedFetch(`/api/mail/drafts?${params}`, {
362
+ method: "DELETE",
363
+ });
364
+ if (!res.ok)
365
+ throw new Error(await describeError(res));
366
+ return (await res.json());
367
+ }
368
+ function mailParams(opts) {
369
+ const params = new URLSearchParams();
370
+ if (opts.mailbox)
371
+ params.set("mailbox", opts.mailbox);
372
+ if (opts.kind)
373
+ params.set("kind", opts.kind);
374
+ else if (opts.folder)
375
+ params.set("folder", opts.folder);
376
+ return params;
377
+ }
378
+ /** Your mailboxes, their sender addresses and the default signature as text. */
379
+ export async function listMailboxes() {
380
+ const { res } = await authedFetch("/api/mail/mailboxes", { method: "GET" });
381
+ if (!res.ok)
382
+ throw new Error(await describeError(res));
383
+ return (await res.json());
384
+ }
385
+ /** Recent or matching messages in one folder; reading never marks them read. */
386
+ export async function listMailMessages(opts = {}) {
387
+ const params = mailParams(opts);
388
+ if (opts.search)
389
+ params.set("search", opts.search);
390
+ if (opts.limit)
391
+ params.set("limit", String(opts.limit));
392
+ const { res } = await authedFetch(`/api/mail/messages?${params}`, {
393
+ method: "GET",
394
+ });
395
+ if (!res.ok)
396
+ throw new Error(await describeError(res));
397
+ return (await res.json());
398
+ }
399
+ /** One message as plain text, without marking it read. */
400
+ export async function readMailMessage(uid, opts = {}) {
401
+ const params = mailParams(opts);
402
+ params.set("uid", String(uid));
403
+ const { res } = await authedFetch(`/api/mail/messages?${params}`, {
404
+ method: "GET",
405
+ });
406
+ if (!res.ok)
407
+ throw new Error(await describeError(res));
408
+ return (await res.json());
409
+ }
410
+ /** No answer to a send request: the mail may or may not have gone out. */
411
+ export class SendOutcomeUnknownError extends Error {
412
+ }
413
+ /**
414
+ * Sends (`confirm`) or checks (`dryRun`) one existing draft in Concepten.
415
+ * Only a draft of a mailbox you have access to goes out, and one that is
416
+ * already sent is never sent twice. A request that gets no answer throws
417
+ * {@link SendOutcomeUnknownError}: look at `mailDraftStatus` before retrying.
418
+ */
419
+ export async function sendMailDraft(input) {
420
+ let res;
421
+ try {
422
+ ({ res } = await authedFetch("/api/mail/send", {
423
+ method: "POST",
424
+ headers: { "content-type": "application/json" },
425
+ body: JSON.stringify({
426
+ uid: input.uid,
427
+ mailbox: input.mailbox,
428
+ dryRun: input.dryRun ?? false,
429
+ confirm: input.confirm ?? false,
430
+ }),
431
+ // Grote bijlagen over SMTP duren langer dan een gewone aanroep.
432
+ signal: AbortSignal.timeout(120_000),
433
+ }));
434
+ }
435
+ catch (err) {
436
+ if (input.dryRun || !input.confirm)
437
+ throw err;
438
+ throw new SendOutcomeUnknownError(`No answer while sending draft ${input.uid} (${err instanceof Error ? err.message : String(err)}). It may have been sent: check \`infaris-calendar mail status ${input.uid}\` before trying again.`);
439
+ }
440
+ if (!res.ok)
441
+ throw new Error(await describeError(res));
442
+ return (await res.json());
443
+ }
444
+ /** Is this draft (uid) or message (Message-ID) really in Verzonden? */
445
+ export async function mailDraftStatus(ref, mailbox) {
446
+ const params = new URLSearchParams();
447
+ if (ref.uid)
448
+ params.set("uid", String(ref.uid));
449
+ if (ref.messageId)
450
+ params.set("messageId", ref.messageId);
451
+ if (mailbox)
452
+ params.set("mailbox", mailbox);
453
+ const { res } = await authedFetch(`/api/mail/send?${params}`, { method: "GET" });
454
+ if (!res.ok)
455
+ throw new Error(await describeError(res));
456
+ return (await res.json());
457
+ }
339
458
  async function describeError(res) {
340
459
  let detail = "";
341
460
  try {
package/dist/crm-index.js CHANGED
@@ -8,7 +8,7 @@ import { createTask, addTaskAttachment, listTeamMembers, listTasks, } from "./ap
8
8
  import { resolveAttendeeIds } from "./selectors.js";
9
9
  const program = new Command()
10
10
  .name("infaris-crm")
11
- .version("0.9.0")
11
+ .version("0.10.0")
12
12
  .description("Customer dossiers, contact people and contact history. Shares infaris-calendar login.");
13
13
  function output(value) {
14
14
  process.stdout.write(`${JSON.stringify(value, null, 2)}\n`);