emailengine-app 2.76.0 → 2.78.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.
Files changed (165) hide show
  1. package/.github/workflows/release.yaml +24 -19
  2. package/.github/workflows/test.yml +80 -0
  3. package/.ncurc.js +14 -2
  4. package/.release-please-manifest.json +3 -0
  5. package/CHANGELOG.md +54 -0
  6. package/README.md +1 -1
  7. package/bin/emailengine.js +1 -1
  8. package/data/google-crawlers.json +1 -1
  9. package/encrypt.js +72 -5
  10. package/lib/account/expected-identity.js +88 -0
  11. package/lib/account.js +75 -26
  12. package/lib/address-strategies.js +18 -0
  13. package/lib/api-reference/code-samples.js +194 -0
  14. package/lib/api-reference/format.js +193 -0
  15. package/lib/api-reference/index.js +222 -0
  16. package/lib/api-reference/model.js +338 -0
  17. package/lib/api-reference/schema-tree.js +366 -0
  18. package/lib/api-routes/account-routes.js +136 -39
  19. package/lib/api-routes/behavior-notes.js +57 -0
  20. package/lib/api-routes/blocklist-routes.js +9 -9
  21. package/lib/api-routes/changes-routes.js +6 -4
  22. package/lib/api-routes/chat-routes.js +3 -3
  23. package/lib/api-routes/delivery-test-routes.js +24 -6
  24. package/lib/api-routes/export-routes.js +14 -12
  25. package/lib/api-routes/gateway-routes.js +11 -11
  26. package/lib/api-routes/license-routes.js +7 -7
  27. package/lib/api-routes/mailbox-routes.js +18 -9
  28. package/lib/api-routes/message-routes.js +55 -35
  29. package/lib/api-routes/oauth2-app-routes.js +22 -16
  30. package/lib/api-routes/outbox-routes.js +7 -7
  31. package/lib/api-routes/pubsub-routes.js +3 -3
  32. package/lib/api-routes/settings-routes.js +10 -9
  33. package/lib/api-routes/stats-routes.js +3 -3
  34. package/lib/api-routes/submit-routes.js +58 -8
  35. package/lib/api-routes/template-routes.js +13 -13
  36. package/lib/api-routes/token-routes.js +15 -10
  37. package/lib/api-routes/webhook-route-routes.js +5 -5
  38. package/lib/append-list.js +3 -14
  39. package/lib/arf-detect.js +23 -1
  40. package/lib/autodetect-imap-settings.js +40 -16
  41. package/lib/base32.js +30 -0
  42. package/lib/bounce-detect.js +182 -62
  43. package/lib/consts.js +7 -0
  44. package/lib/egress-filter.js +89 -8
  45. package/lib/email-client/base-client.js +72 -82
  46. package/lib/email-client/gmail-client.js +3 -3
  47. package/lib/email-client/imap/listing-diff.js +3 -3
  48. package/lib/email-client/imap/mailbox.js +5 -69
  49. package/lib/email-client/imap-client.js +3 -2
  50. package/lib/email-client/message-builder.js +13 -13
  51. package/lib/email-client/outlook-client.js +1 -1
  52. package/lib/enum-descriptions.js +130 -0
  53. package/lib/export.js +1 -1
  54. package/lib/gateway.js +1 -1
  55. package/lib/get-raw-email.js +1 -1
  56. package/lib/imapproxy/imap-core/lib/imap-connection.js +2 -2
  57. package/lib/imapproxy/imap-core/lib/imap-server.js +2 -2
  58. package/lib/imapproxy/imap-server.js +42 -28
  59. package/lib/imapproxy/upstream-client.js +38 -0
  60. package/lib/license-beacon.js +1 -1
  61. package/lib/llm-pre-process.js +1 -1
  62. package/lib/msgpack.js +62 -0
  63. package/lib/oauth/decode-jwt-payload.js +39 -0
  64. package/lib/oauth/outlook-identity.js +61 -0
  65. package/lib/oauth/retry-setup.js +38 -0
  66. package/lib/oauth2-apps.js +10 -4
  67. package/lib/openapi/build-document.js +318 -0
  68. package/lib/openapi/index.js +95 -0
  69. package/lib/openapi/joi-schema.js +523 -0
  70. package/lib/outbox.js +66 -3
  71. package/lib/pre-process.js +1 -1
  72. package/lib/read-env-value.js +37 -0
  73. package/lib/routes-ui.js +5 -1
  74. package/lib/schemas.js +88 -17
  75. package/lib/swagger-options.js +173 -0
  76. package/lib/templates.js +1 -1
  77. package/lib/tokens.js +70 -6
  78. package/lib/tools.js +117 -48
  79. package/lib/totp.js +67 -0
  80. package/lib/ui-routes/account-routes.js +91 -15
  81. package/lib/ui-routes/admin-config-routes.js +2 -7
  82. package/lib/ui-routes/admin-entities-routes.js +3 -0
  83. package/lib/ui-routes/auth-routes.js +6 -24
  84. package/lib/ui-routes/dashboard-routes.js +2 -26
  85. package/lib/ui-routes/reference-routes.js +198 -0
  86. package/lib/ui-routes/route-helpers.js +39 -0
  87. package/lib/utils/is-email.js +34 -0
  88. package/lib/utils/to-camel-case.js +17 -0
  89. package/lib/webhook-egress.js +37 -0
  90. package/lib/webhooks.js +1 -1
  91. package/package.json +27 -31
  92. package/prepare-bundle-deps.sh +28 -0
  93. package/release-please-config.json +11 -0
  94. package/sbom.json +1 -1
  95. package/server.js +11 -3
  96. package/static/css/flyonui.css +1 -1
  97. package/static/css/public.css +3 -0
  98. package/static/css/src/app.tailwind.css +113 -0
  99. package/static/js/app.js +69 -0
  100. package/static/js/ee-client.js +238 -8
  101. package/static/js/reference.js +1021 -0
  102. package/static/js/ui.js +61 -33
  103. package/static/licenses.html +658 -1560
  104. package/translations/README.md +64 -0
  105. package/translations/de.mo +0 -0
  106. package/translations/de.po +164 -120
  107. package/translations/en.po +200 -161
  108. package/translations/et.mo +0 -0
  109. package/translations/et.po +163 -120
  110. package/translations/fr.mo +0 -0
  111. package/translations/fr.po +163 -120
  112. package/translations/ja.mo +0 -0
  113. package/translations/ja.po +163 -120
  114. package/translations/messages.pot +84 -54
  115. package/translations/nl.mo +0 -0
  116. package/translations/nl.po +164 -120
  117. package/translations/pl.mo +0 -0
  118. package/translations/pl.po +163 -120
  119. package/views/account-identity-error.hbs +34 -0
  120. package/views/accounts/edit.hbs +1 -0
  121. package/views/accounts/register/imap.hbs +12 -2
  122. package/views/config/ai.hbs +1 -1
  123. package/views/config/document-store/chat.hbs +5 -5
  124. package/views/config/document-store/index.hbs +2 -2
  125. package/views/config/document-store/pre-processing/index.hbs +1 -1
  126. package/views/config/email-processing.hbs +1 -1
  127. package/views/config/imap-proxy.hbs +1 -1
  128. package/views/config/license.hbs +1 -1
  129. package/views/config/oauth/index.hbs +3 -3
  130. package/views/config/security.hbs +1 -1
  131. package/views/config/smtp.hbs +2 -2
  132. package/views/dashboard.hbs +1 -1
  133. package/views/layout/app.hbs +43 -8
  134. package/views/oauth-scope-error.hbs +1 -1
  135. package/views/partials/oauth_form.hbs +4 -4
  136. package/views/partials/reference/anchor-link.hbs +20 -0
  137. package/views/partials/reference/disclosure.hbs +37 -0
  138. package/views/partials/reference/nav.hbs +120 -0
  139. package/views/partials/reference/operation.hbs +127 -0
  140. package/views/partials/reference/params.hbs +20 -0
  141. package/views/partials/reference/schema-node.hbs +99 -0
  142. package/views/partials/reference/schema.hbs +19 -0
  143. package/views/partials/reference/section-heading.hbs +16 -0
  144. package/views/partials/reference/token-alert.hbs +49 -0
  145. package/views/partials/reference/try-field.hbs +31 -0
  146. package/views/partials/reference/try-it.hbs +78 -0
  147. package/views/partials/side_menu.hbs +1 -1
  148. package/views/partials/ui/badge.hbs +4 -2
  149. package/views/partials/ui/page-header.hbs +1 -1
  150. package/views/partials/ui/search-input.hbs +31 -0
  151. package/views/partials/ui/setting-ref.hbs +1 -1
  152. package/views/partials/ui/stat-card.hbs +4 -2
  153. package/views/partials/ui/tab.hbs +6 -1
  154. package/views/partials/ui/tabs.hbs +2 -2
  155. package/views/reference/index.hbs +76 -0
  156. package/views/reference/redirect.hbs +49 -0
  157. package/views/reference/tag.hbs +28 -0
  158. package/views/reference/token.hbs +49 -0
  159. package/views/tokens/index.hbs +9 -1
  160. package/views/webhooks/index.hbs +3 -3
  161. package/workers/api.js +194 -302
  162. package/workers/imap.js +1 -1
  163. package/workers/submit.js +1 -1
  164. package/workers/webhooks.js +2 -13
  165. package/views/swagger/index.hbs +0 -82
@@ -874,6 +874,9 @@ dialog.ee-modal.ee-modal-wide {
874
874
  .ee-dl dd {
875
875
  margin: 0 0 0.5rem;
876
876
  min-width: 0;
877
+ /* Values here can be email addresses, which offer no natural break opportunity and would otherwise
878
+ run past the single-column grid on a narrow screen. */
879
+ overflow-wrap: anywhere;
877
880
  }
878
881
 
879
882
  @media (min-width: 640px) {
@@ -55,6 +55,7 @@
55
55
  @source '../../js/ui.js';
56
56
  @source '../../js/passkey-register.js';
57
57
  @source '../../js/login-passkey.js';
58
+ @source '../../js/reference.js';
58
59
  @source '../../../node_modules/flyonui/flyonui.js';
59
60
 
60
61
  /* Safelist for classes assembled dynamically - in JS (badge-${type} etc.) and
@@ -84,9 +85,29 @@
84
85
  /* single source for the sidebar width: w-sidebar on the aside,
85
86
  * lg:ps-sidebar on the content column */
86
87
  --spacing-sidebar: 18rem;
88
+ /* height of the sticky topbar: the secondary nav column sticks directly
89
+ * below it (lg:top-topbar) and sizes its scroll region against it, so the
90
+ * two must agree */
91
+ --spacing-topbar: 2.9375rem;
92
+ /* one step below text-xs, for the dense metadata labels in the API
93
+ * reference (required markers, constraint chips, nav method labels) */
94
+ --text-2xs: 0.6875rem;
95
+ --text-2xs--line-height: calc(1 / 0.6875);
87
96
  }
88
97
 
89
98
  @layer base {
99
+ /* The admin shell's topbar is sticky, and a page may stick a band of its own under it
100
+ * (the API reference's "no access token" banner, whose measured height static/js/
101
+ * reference.js publishes). Telling the scroller about that inset covers every way
102
+ * something is scrolled into view - fragment links, scrollIntoView, find-in-page, the
103
+ * focus moving through a Try it panel - instead of only the classes that carry a
104
+ * scroll-margin of their own. On the class rather than on html because the login and
105
+ * public layouts load this stylesheet too and have no topbar to clear; the class is on
106
+ * <html> in views/layout/app.hbs. */
107
+ .ee-app-chrome {
108
+ scroll-padding-top: calc(var(--spacing-topbar) + var(--ee-sticky-band-h, 0px));
109
+ }
110
+
90
111
  /* Tailwind preflight leaves hr at "1px solid currentColor" - a harsh
91
112
  * full-contrast line in both themes; default every hr to the standard
92
113
  * content-border token instead of classing each instance */
@@ -199,6 +220,98 @@
199
220
  line-height: 1.4286;
200
221
  }
201
222
 
223
+ /* Inline code / metadata pill. The block-level counterpart is
224
+ * .ee-code-block above; there was no inline equivalent before. .ee-code is
225
+ * emitted from lib/api-reference/format.js for every backtick span in a
226
+ * schema description, which the Tailwind scanner never sees - so this has
227
+ * to be a class, not utilities. */
228
+ .ee-code,
229
+ .ee-chip {
230
+ background-color: color-mix(in oklab, var(--color-base-content) 8%, transparent);
231
+ border-radius: var(--radius-field, 0.375rem);
232
+ padding: 0.0625rem 0.375rem;
233
+ font-family: var(--font-mono, ui-monospace, monospace);
234
+ font-size: 0.75rem;
235
+ white-space: nowrap;
236
+ }
237
+ /* .ee-chip is the muted variant, for constraint and default markers */
238
+ .ee-chip {
239
+ color: color-mix(in oklab, var(--color-base-content) 65%, transparent);
240
+ }
241
+
242
+ /* --- API reference (/admin/reference) ---------------------------------
243
+ * The schema trees are deeply nested lists rendered by a recursive
244
+ * partial, so their look comes from a class rather than utilities. These
245
+ * class names double as the e2e selectors in test/e2e/reference.spec.js. */
246
+ .ee-ref-props {
247
+ display: flex;
248
+ flex-direction: column;
249
+ gap: 0.625rem;
250
+ }
251
+ .ee-ref-nest {
252
+ border-inline-start: 1px solid color-mix(in oklab, var(--color-base-content) 12%, transparent);
253
+ padding-inline-start: 0.75rem;
254
+ }
255
+
256
+ /* Room to breathe above a jump target - operations linked from the sidebar and from
257
+ * #operationId, property rows from #operation.body.field. The sticky chrome above them is
258
+ * NOT in here: that is scroll-padding on the scroller (.ee-app-chrome in @layer base), and
259
+ * counting it twice would park every deep link a band's height below where it belongs.
260
+ * Also keep any scroll-mt-* utility off these elements - utilities are emitted after this
261
+ * layer and would silently replace it. */
262
+ .ee-ref-op,
263
+ .ee-ref-prop {
264
+ scroll-margin-top: 2rem;
265
+ }
266
+
267
+ /* Deep-link icons (reference/anchor-link), on operation headings and on every
268
+ * property row. One definition instead of the utility string repeated at every
269
+ * call site - a tag page renders up to 434 of these. */
270
+ .ee-ref-anchor {
271
+ color: color-mix(in oklab, var(--color-base-content) 40%, transparent);
272
+ flex-shrink: 0;
273
+ }
274
+ .ee-ref-anchor:hover {
275
+ color: var(--color-primary);
276
+ }
277
+
278
+ /* Property links stay hidden until their row is hovered. The rule hangs off the
279
+ * header row rather than the list item because the item CONTAINS the nested rows,
280
+ * so hovering a leaf would reveal the link on every one of its ancestors too. The
281
+ * header row has no rows inside it, which makes it exact at every depth (same
282
+ * reasoning as the disclosure chevron below). */
283
+ .ee-ref-prop-anchor {
284
+ opacity: 0;
285
+ transition: opacity 0.15s;
286
+ }
287
+ .ee-ref-prop-head:hover .ee-ref-prop-anchor,
288
+ .ee-ref-prop-anchor:focus-visible {
289
+ opacity: 1;
290
+ }
291
+
292
+ /* Arriving on a linked property. :target needs no script, so the row is marked
293
+ * even if the deep link lands on an already-expanded group. */
294
+ .ee-ref-prop:target > .ee-ref-prop-head {
295
+ background: color-mix(in oklab, var(--color-primary) 12%, transparent);
296
+ border-radius: var(--radius-field);
297
+ box-shadow: 0 0 0 0.25rem color-mix(in oklab, var(--color-primary) 12%, transparent);
298
+ }
299
+ .ee-ref-prop:target > .ee-ref-prop-head .ee-ref-prop-anchor {
300
+ opacity: 1;
301
+ }
302
+
303
+ /* Collapsible section (reference/disclosure). The chevron rotation is here
304
+ * rather than on Tailwind's group-open variant because these disclosures
305
+ * nest: a named group matches ANY open ancestor, so an inner collapsed
306
+ * group's chevron would rotate whenever an outer one was open. The child
307
+ * combinator below is exact at every depth. */
308
+ .ee-ref-disclosure > summary .ee-ref-chevron {
309
+ transition: transform 0.2s;
310
+ }
311
+ .ee-ref-disclosure[open] > summary .ee-ref-chevron {
312
+ transform: rotate(90deg);
313
+ }
314
+
202
315
  /* Typography island for server-rendered markdown (marked output: the EULA
203
316
  * on /license.html and the branding preview page). Preflight strips all
204
317
  * default heading sizes, margins and list bullets, so restore a minimal
package/static/js/app.js CHANGED
@@ -219,6 +219,75 @@ document.addEventListener('DOMContentLoaded', () => {
219
219
  });
220
220
  }
221
221
 
222
+ // Secondary navigation column (views/layout/app.hbs): a full column beside the content
223
+ // from lg up, a disclosure below that. aria-expanded on the button is the whole state -
224
+ // the panel derives its visibility from it in CSS (peer-aria-expanded:block), so there is
225
+ // no second flag to keep in step. Above lg the button is display:none and lg:block on the
226
+ // panel outranks the peer rule, so the writes below are inert rather than wrong there.
227
+ let secondaryNavToggle = document.getElementById('secondary-nav-toggle');
228
+ let secondaryNavPanel = document.getElementById('secondary-nav-panel');
229
+ if (secondaryNavToggle && secondaryNavPanel) {
230
+ let setSecondaryNav = expanded => secondaryNavToggle.setAttribute('aria-expanded', String(expanded));
231
+
232
+ secondaryNavToggle.addEventListener('click', () => setSecondaryNav(secondaryNavToggle.getAttribute('aria-expanded') !== 'true'));
233
+
234
+ // Closing takes focus with it when the caret is inside the panel: the subtree becomes
235
+ // display:none, and focus would fall back to <body> - the reader's next Tab would
236
+ // restart from the top of the page. Hand it back to the control they came from.
237
+ let closeSecondaryNav = () => {
238
+ let focused = document.activeElement;
239
+ setSecondaryNav(false);
240
+ if (focused && secondaryNavPanel.contains(focused)) {
241
+ secondaryNavToggle.focus();
242
+ }
243
+ };
244
+
245
+ // Page scripts have business with this too - the reference's filter shortcut has to
246
+ // open the panel before it can focus the field inside it, and its group rows behave
247
+ // differently depending on which mode the column is in. Publishing the facts they need
248
+ // keeps the toggle's id, and the fact that a click is what opens it, in this file.
249
+ // `collapsible` reads the button's own box: it is lg:hidden, so being laid out at all
250
+ // is the answer, and the breakpoint stays in the stylesheet.
251
+ window.uiSecondaryNav = {
252
+ // the elements too: the toggle so a page script can watch its box for the mode
253
+ // change, the panel because below lg it is the scroller the nav lives in
254
+ toggle: secondaryNavToggle,
255
+ panel: secondaryNavPanel,
256
+ collapsible: () => !!secondaryNavToggle.offsetParent,
257
+ open: () => setSecondaryNav(true),
258
+ close: closeSecondaryNav
259
+ };
260
+
261
+ secondaryNavPanel.addEventListener('click', event => {
262
+ // A link to another page reloads and closes this anyway, but an in-page anchor
263
+ // does not - it would leave the panel expanded over the section just jumped to.
264
+ // Closing before the browser acts on the fragment also keeps the landing position
265
+ // right: the page shrinks first and the scroll is measured after.
266
+ //
267
+ // Two links are left alone: one something else already handled (defaultPrevented -
268
+ // the reference's group rows open their own list in place), and one opened
269
+ // elsewhere by a modifier, which leaves this page exactly where it was.
270
+ if (event.defaultPrevented || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) {
271
+ return;
272
+ }
273
+
274
+ if (event.target.closest('a')) {
275
+ closeSecondaryNav();
276
+ }
277
+ });
278
+
279
+ // Escape is the way out of any disclosure. Without it the only way to put the panel
280
+ // away is to find the row that opened it, which by then may be a screen above. Same
281
+ // rule as the click handler: a keystroke something else has already acted on is not
282
+ // ours - the reference's filter takes Escape to clear its query, and clearing a query
283
+ // should not also close the panel it was being typed into.
284
+ document.addEventListener('keydown', event => {
285
+ if (event.key === 'Escape' && !event.defaultPrevented && secondaryNavToggle.getAttribute('aria-expanded') === 'true') {
286
+ closeSecondaryNav();
287
+ }
288
+ });
289
+ }
290
+
222
291
  // show or hide the error tooltip attached to a state badge (the badge sits
223
292
  // inside a ui/state-badge tooltip wrapper). The empty state is gated with
224
293
  // ee-tooltip-empty on the wrapper, not with hidden on the content -
@@ -310,14 +310,25 @@ export class EmailEngineClient {
310
310
  }
311
311
  }
312
312
 
313
+ _normalizeRecipients(to) {
314
+ return Array.isArray(to)
315
+ ? to.map(addr => (typeof addr === 'string' ? { address: addr } : addr))
316
+ : [typeof to === 'string' ? { address: to } : to];
317
+ }
318
+
319
+ // Account details (name, email address) fetched once and cached for the lifetime
320
+ // of the instance; used as the From identity of saved drafts.
321
+ async _getAccountInfo() {
322
+ if (!this._accountInfo) {
323
+ this._accountInfo = await this.apiRequest('GET', `/v1/account/${this.account}`);
324
+ }
325
+ return this._accountInfo;
326
+ }
327
+
313
328
  async sendMessage(to, subject, text) {
314
329
  try {
315
- const toAddresses = Array.isArray(to)
316
- ? to.map(addr => (typeof addr === 'string' ? { address: addr } : addr))
317
- : [typeof to === 'string' ? { address: to } : to];
318
-
319
330
  const messageData = {
320
- to: toAddresses,
331
+ to: this._normalizeRecipients(to),
321
332
  subject: subject,
322
333
  text: text
323
334
  };
@@ -332,6 +343,77 @@ export class EmailEngineClient {
332
343
  }
333
344
  }
334
345
 
346
+ async saveDraft(to, subject, text) {
347
+ try {
348
+ if (!this.folders.length) {
349
+ await this.loadFolders();
350
+ }
351
+ const draftsFolder = this.folders.find(f => f.specialUse === '\\Drafts');
352
+ if (!draftsFolder) {
353
+ throw new Error('No Drafts folder found for this account');
354
+ }
355
+
356
+ const messageData = {
357
+ path: draftsFolder.path,
358
+ flags: ['\\Draft'],
359
+ subject: subject,
360
+ text: text
361
+ };
362
+
363
+ const recipients = to ? this._normalizeRecipients(to) : [];
364
+ if (recipients.length) {
365
+ messageData.to = recipients;
366
+ }
367
+
368
+ // The upload endpoint does not add a From header on its own, but the draft
369
+ // needs one to be deliverable later - use the account's own identity.
370
+ try {
371
+ const accountInfo = await this._getAccountInfo();
372
+ if (accountInfo && accountInfo.email) {
373
+ messageData.from = { name: accountInfo.name || '', address: accountInfo.email };
374
+ }
375
+ } catch (err) {
376
+ // Non-fatal: the draft is saved without a From header
377
+ }
378
+
379
+ const response = await this.apiRequest('POST', `/v1/account/${this.account}/message`, messageData);
380
+ return response;
381
+ } catch (error) {
382
+ console.error('Failed to save draft:', error);
383
+ this._parseApiError(error);
384
+ throw error;
385
+ }
386
+ }
387
+
388
+ async submitDraft(messageId, options = null) {
389
+ try {
390
+ const response = await this.apiRequest(
391
+ 'POST',
392
+ `/v1/account/${this.account}/message/${messageId}/submit`,
393
+ options
394
+ );
395
+ return response;
396
+ } catch (error) {
397
+ console.error('Failed to submit draft:', error);
398
+ this._parseApiError(error);
399
+ throw error;
400
+ }
401
+ }
402
+
403
+ // A message counts as a draft when the server marks it as one (the \Draft flag on IMAP,
404
+ // the DRAFT label on Gmail, a draft message on MS Graph) or when it sits in the Drafts
405
+ // special-use folder.
406
+ _isDraftMessage(msg) {
407
+ if (!msg) {
408
+ return false;
409
+ }
410
+ if (msg.draft || msg.messageSpecialUse === '\\Drafts') {
411
+ return true;
412
+ }
413
+ const folder = this.folders.find(f => f.path === this.currentFolder);
414
+ return !!(folder && folder.specialUse === '\\Drafts');
415
+ }
416
+
335
417
  _parseApiError(error) {
336
418
  try {
337
419
  // If error has response text, try to parse it
@@ -368,7 +450,10 @@ export class EmailEngineClient {
368
450
  return mainMessage;
369
451
  }
370
452
 
371
- // Fallback to generic error message
453
+ // No field-level details - fall back to the API-provided message, then to a generic one
454
+ if (error.details && typeof error.details.message === 'string' && error.details.message) {
455
+ return error.details.message;
456
+ }
372
457
  return 'Failed to send email. Please check your input and try again.';
373
458
  }
374
459
 
@@ -1095,11 +1180,34 @@ export class EmailEngineClient {
1095
1180
  font-size: 14px;
1096
1181
  cursor: pointer;
1097
1182
  }
1098
-
1183
+
1099
1184
  .ee-compose-cancel:hover {
1100
1185
  background: #545b62;
1101
1186
  }
1102
1187
 
1188
+ .ee-compose-save-draft {
1189
+ background: transparent;
1190
+ color: #007bff;
1191
+ border: 1px solid #007bff;
1192
+ padding: 10px 20px;
1193
+ border-radius: 4px;
1194
+ font-size: 14px;
1195
+ font-weight: 500;
1196
+ cursor: pointer;
1197
+ }
1198
+
1199
+ .ee-compose-save-draft:hover:not(:disabled) {
1200
+ background: #007bff;
1201
+ color: white;
1202
+ }
1203
+
1204
+ .ee-compose-save-draft:disabled {
1205
+ color: #6c757d;
1206
+ border-color: #6c757d;
1207
+ cursor: not-allowed;
1208
+ opacity: 0.6;
1209
+ }
1210
+
1103
1211
  /* Dark mode toggle button */
1104
1212
  .ee-dark-mode-toggle {
1105
1213
  position: absolute;
@@ -1314,6 +1422,16 @@ export class EmailEngineClient {
1314
1422
  .ee-dark-mode .ee-compose-cancel:hover {
1315
1423
  background: #555;
1316
1424
  }
1425
+
1426
+ .ee-dark-mode .ee-compose-save-draft {
1427
+ color: #66b2ff;
1428
+ border-color: #66b2ff;
1429
+ }
1430
+
1431
+ .ee-dark-mode .ee-compose-save-draft:hover:not(:disabled) {
1432
+ background: #66b2ff;
1433
+ color: #1a1a1a;
1434
+ }
1317
1435
  `;
1318
1436
  document.head.appendChild(style);
1319
1437
  this._styleElement = style;
@@ -1564,8 +1682,10 @@ export class EmailEngineClient {
1564
1682
 
1565
1683
  const msg = this.currentMessage;
1566
1684
  const isUnseen = msg.unseen;
1685
+ const isDraft = this._isDraftMessage(msg);
1567
1686
  const html = `
1568
1687
  <div class="ee-message-actions">
1688
+ ${isDraft ? '<button class="ee-button" data-action="send-draft">Send Draft</button>' : ''}
1569
1689
  <button class="ee-button" data-action="toggle-read">Mark as ${isUnseen ? 'seen' : 'unseen'}</button>
1570
1690
  <button class="ee-button" data-action="delete">Delete</button>
1571
1691
  <button class="ee-button" data-action="download-original">Download Original</button>
@@ -1640,6 +1760,41 @@ export class EmailEngineClient {
1640
1760
 
1641
1761
  this._labelCollapsedThreads(viewer);
1642
1762
 
1763
+ const sendDraftButton = viewer.querySelector('[data-action="send-draft"]');
1764
+ if (sendDraftButton) {
1765
+ sendDraftButton.addEventListener('click', async () => {
1766
+ const result = await this.confirmMethod(
1767
+ 'Send this draft to its recipients now? The draft is removed from the Drafts folder once it has been sent.',
1768
+ 'Send Draft',
1769
+ 'Cancel',
1770
+ 'Send'
1771
+ );
1772
+ if (!result) {
1773
+ return;
1774
+ }
1775
+
1776
+ const originalText = sendDraftButton.textContent;
1777
+ sendDraftButton.disabled = true;
1778
+ sendDraftButton.textContent = 'Sending...';
1779
+
1780
+ try {
1781
+ await this.submitDraft(msg.id);
1782
+ await this.alertMethod('Draft queued for delivery.', 'Success', null, 'OK');
1783
+ // Refresh the folder view - the provider removes the draft once it is sent,
1784
+ // immediately for Gmail and MS Graph, after SMTP delivery for IMAP accounts.
1785
+ if (!this._destroyed && this.currentFolder) {
1786
+ await this.loadMessages(this.currentFolder);
1787
+ }
1788
+ } catch (error) {
1789
+ console.error('Failed to send draft:', error);
1790
+ const errorMessage = this._formatSendError(error);
1791
+ await this.alertMethod(errorMessage, 'Send Error', null, 'OK');
1792
+ sendDraftButton.disabled = false;
1793
+ sendDraftButton.textContent = originalText;
1794
+ }
1795
+ });
1796
+ }
1797
+
1643
1798
  viewer.querySelector('[data-action="toggle-read"]').addEventListener('click', () => {
1644
1799
  const currentlyUnseen = msg.unseen;
1645
1800
  this.markAsRead(msg.id, currentlyUnseen);
@@ -1754,6 +1909,7 @@ export class EmailEngineClient {
1754
1909
  </form>
1755
1910
  <div class="ee-compose-actions">
1756
1911
  <button type="button" class="ee-compose-cancel">Cancel</button>
1912
+ <button type="button" class="ee-compose-save-draft">Save Draft</button>
1757
1913
  <button type="button" class="ee-compose-send">Send</button>
1758
1914
  </div>
1759
1915
  </div>
@@ -1776,6 +1932,7 @@ export class EmailEngineClient {
1776
1932
  const modal = this.container.querySelector('.ee-compose-modal');
1777
1933
  const closeButton = this.container.querySelector('.ee-compose-close');
1778
1934
  const cancelButton = this.container.querySelector('.ee-compose-cancel');
1935
+ const saveDraftButton = this.container.querySelector('.ee-compose-save-draft');
1779
1936
  const sendButton = this.container.querySelector('.ee-compose-send');
1780
1937
  const form = this.container.querySelector('.ee-compose-form');
1781
1938
 
@@ -1811,6 +1968,52 @@ export class EmailEngineClient {
1811
1968
  };
1812
1969
  document.addEventListener('keydown', this._composeKeydownHandler);
1813
1970
 
1971
+ // Save the composed message as a draft instead of sending it
1972
+ saveDraftButton.addEventListener('click', async () => {
1973
+ const formData = new FormData(form);
1974
+ const to = formData.get('to').trim();
1975
+ const subject = formData.get('subject').trim();
1976
+ const message = formData.get('message').trim();
1977
+
1978
+ if (!to && !subject && !message) {
1979
+ modal.classList.remove('show');
1980
+ await this.alertMethod(
1981
+ 'Nothing to save - fill in at least one field first.',
1982
+ 'Validation Error',
1983
+ null,
1984
+ 'OK'
1985
+ );
1986
+ modal.classList.add('show');
1987
+ setTimeout(() => form.querySelector('input[name="to"]').focus(), 100);
1988
+ return;
1989
+ }
1990
+
1991
+ const originalText = saveDraftButton.textContent;
1992
+ saveDraftButton.disabled = true;
1993
+ saveDraftButton.textContent = 'Saving...';
1994
+
1995
+ try {
1996
+ const response = await this.saveDraft(to || null, subject, message);
1997
+ closeModal();
1998
+ await this.alertMethod('Draft saved to the Drafts folder.', 'Success', null, 'OK');
1999
+ // Refresh the list when the user is looking at the Drafts folder
2000
+ if (!this._destroyed && response && response.path && this.currentFolder === response.path) {
2001
+ await this.loadMessages(this.currentFolder);
2002
+ }
2003
+ } catch (error) {
2004
+ console.error('Failed to save draft:', error);
2005
+ // Close modal before showing error alert, then reopen with preserved values
2006
+ modal.classList.remove('show');
2007
+ const errorMessage = this._formatSendError(error);
2008
+ await this.alertMethod(errorMessage, 'Save Error', null, 'OK');
2009
+ modal.classList.add('show');
2010
+ setTimeout(() => form.querySelector('input[name="to"]').focus(), 100);
2011
+ } finally {
2012
+ saveDraftButton.disabled = false;
2013
+ saveDraftButton.textContent = originalText;
2014
+ }
2015
+ });
2016
+
1814
2017
  // Send email
1815
2018
  sendButton.addEventListener('click', async () => {
1816
2019
  const formData = new FormData(form);
@@ -1889,6 +2092,17 @@ export class EmailEngineClient {
1889
2092
  const buttonSize = 56; // Button width/height
1890
2093
  const margin = 20; // Desired margin from edges
1891
2094
 
2095
+ // A hidden container (display: none) reports a zero-size rect; positioning
2096
+ // against it would push the fixed-position button off-screen. Hide the button
2097
+ // instead and let the ResizeObserver below restore it once the container is
2098
+ // shown - hosts often create the client inside a container that is only made
2099
+ // visible afterwards.
2100
+ if (!containerRect.width && !containerRect.height) {
2101
+ composeButton.style.display = 'none';
2102
+ return;
2103
+ }
2104
+ composeButton.style.display = '';
2105
+
1892
2106
  // Calculate the ideal position (bottom-right of container with margin)
1893
2107
  const idealBottom = window.innerHeight - containerRect.bottom + margin;
1894
2108
  const idealRight = window.innerWidth - containerRect.right + margin;
@@ -1917,10 +2131,26 @@ export class EmailEngineClient {
1917
2131
  window.addEventListener('scroll', updateWithThrottle);
1918
2132
  window.addEventListener('resize', updateWithThrottle);
1919
2133
 
1920
- // Store cleanup function for potential future use
2134
+ // Reposition when the container itself changes size or becomes visible - window
2135
+ // scroll/resize events never fire for those. Calls the update directly instead of
2136
+ // the throttled wrapper: the throttle drops trailing calls, which would leave the
2137
+ // button hidden when the observer fires twice in quick succession (initial
2138
+ // notification followed by the container being shown). Observer callbacks are
2139
+ // already batched per frame by the browser, so there is nothing to throttle.
2140
+ let resizeObserver = null;
2141
+ if (typeof ResizeObserver !== 'undefined') {
2142
+ resizeObserver = new ResizeObserver(updateButtonPosition);
2143
+ resizeObserver.observe(this.container);
2144
+ }
2145
+
2146
+ // Cleanup runs from destroy()
1921
2147
  this._composeButtonCleanup = () => {
1922
2148
  window.removeEventListener('scroll', updateWithThrottle);
1923
2149
  window.removeEventListener('resize', updateWithThrottle);
2150
+ if (resizeObserver) {
2151
+ resizeObserver.disconnect();
2152
+ resizeObserver = null;
2153
+ }
1924
2154
  };
1925
2155
  }
1926
2156