Skip to content

Send email through Microsoft 365 with EMAIL_DRIVER=graph - #75

Open
thomassicaud wants to merge 3 commits into
NYLLON-SOFTWARE:mainfrom
thomassicaud:feat/microsoft-graph-email
Open

thomassicaud wants to merge 3 commits into
NYLLON-SOFTWARE:mainfrom
thomassicaud:feat/microsoft-graph-email

Conversation

@thomassicaud

@thomassicaud thomassicaud commented Nov 17, 2026 •

Copy link
Copy Markdown

What and why

Organizations whose mail already lives in Microsoft 365 can now send SignuDoc's email from one of their own mailboxes through Microsoft Graph, instead of signing up for Resend and verifying a domain there. We run SignuDoc for our contracts and our mail is in Exchange Online; adding a second mail provider for one app was the main thing in the way.

  • src/lib/services/email-graph.ts: the app signs in with OAuth client credentials and calls POST /users/{mailbox}/sendMail. No new dependency (plain fetch). The token is cached until a minute before it expires; a 429/503 is retried after Retry-After within the same bounds as the Resend driver (3 tries, at most 5 s each); a 401 gets one fresh token. Microsoft's own error words reach the test email (for example ErrorAccessDenied: ..., or the AADSTS… line without its trace ids).
  • MICROSOFT_GRAPH_SENDER fixes the sender the way RESEND_FROM_EMAIL does. Settings now names whichever variable is in use (senderAddressVariable) instead of always saying RESEND_FROM_EMAIL.
  • Settings, Organization, Email setup shows a Microsoft 365 status instead of the Resend domain steps, and asks for a test email to confirm the app may use the mailbox. A missing variable is named there.
  • Graph takes at most 4 MB per request, so fitsAsAttachments now depends on the driver: above about 2.9 MB the signed copy goes out as links, as it already does above Resend's limit.
  • docs/self-hosting.md has a "Send email through Microsoft 365" section. It insists on scoping the app to that one mailbox with Exchange RBAC for Applications, because Mail.Send granted in Entra lets an app send as anyone in the tenant.

The shape follows #51 (Cloudflare email): one email-<provider>.ts module, one more EMAIL_DRIVER value, one more status in email-status.ts. Whichever lands first, the other is a small rebase, and I'm happy to do it.

Not in this PR

  • The setup page still talks about Resend only.
  • Credentials come from the environment only. A guided "Connect to Microsoft 365" flow in Settings (credentials stored encrypted, prefilled Exchange commands, and a check that warns when the app holds tenant-wide Mail.Send) needs a schema change, so I'd like to discuss it in an issue first.
  • Attachments larger than 3 MB through Graph upload sessions.

Testing

  • npm test: 311 pass, including 15 new tests in email-graph.test.ts (payload, token reuse and renewal, 401 refresh, throttling, error messages, batch, attachment limit). Nothing talks to Microsoft: fetch is injected.
  • npm run typecheck, npm run lint (no new warnings) and prettier --check on the changed files.
  • Deployed on our self-hosted (Docker) evaluation install, sending from a shared mailbox: a real envelope went through end to end (signing invitation, signature, completion emails with the signed copy attached). Checked that the scoped app gets 202 on that mailbox and 403 ErrorAccessDenied on any other.

🤖 Generated with Claude Code

Note

Add Microsoft 365 Graph email driver with EMAIL_DRIVER=graph

  • Adds a Graph driver that sends mail through Microsoft 365 via OAuth client-credentials tokens, caching tokens until one minute before expiry and refreshing once on a 401. Retries 429/503 responses up to three times with a bounded Retry-After wait. See email-graph.ts.
  • Configuration requires four variables: MICROSOFT_GRAPH_TENANT_ID, MICROSOFT_GRAPH_CLIENT_ID, MICROSOFT_GRAPH_CLIENT_SECRET, and MICROSOFT_GRAPH_SENDER. All send from one shared mailbox and save to its Sent Items.
  • The sender address is read from the driver-specific variable via senderAddressVariable in env.ts, and the settings panel shows Graph status, missing variables, and the sender mailbox in organization-settings.tsx.
  • Batch sending rejects any batch containing attachments; individual messages with attachments beyond the smaller Graph limit send as links instead. The self-hosting guide in self-hosting.md covers mailbox setup and Entra app registration.
  • Behavioral Change: the Graph driver uses a lower attachment-size limit (GRAPH_MAX_ATTACHMENT_BYTES) than Resend, and senders are shared process-wide per configuration hash.

Macroscope summarized 1a24f15.

@vercel

vercel Bot commented Nov 17, 2026

Copy link
Copy Markdown

@thomassicaud-asi is attempting to deploy a commit to the Basic Intelligence Team on Vercel.

A member of the Team first needs to authorize it.

@greptile-apps

greptile-apps Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[High impact] The PR appears safe to merge, with the earlier non-blocking readiness-display issue still open.

Findings

  1. P2 Old checks mark Graph ready. ▶

Summary

Adds Microsoft 365 email sending with EMAIL_DRIVER=graph.

  • SignuDoc can send its email through a Microsoft 365 mailbox.
  • Graph sends large signed copies as download links instead of attachments.
  • Organization Settings shows which Microsoft 365 email setup is ready.

The token-cache and token-refresh findings are fixed. No new findings were accepted.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Email service] --> B{Email driver}
  B -->|graph| C[Shared Graph sender]
  B -->|resend| D[Resend]
  B -->|capture| E[Local outbox]
  C --> F{Cached token valid?}
  F -->|No| G[Request access token]
  F -->|Yes| H[Send from configured mailbox]
  G --> H
  H --> I[Microsoft 365]
Loading

Reviews (4) · Last reviewed commit: "Reuse Graph tokens across email services..." · Reviewed by Greptile

Comment thread src/lib/email-status.ts
Comment on lines +422 to +424
ready: resolvedSettings.emailReadyAt !== null,
senderAddress: graph.sender,
state: "graph",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Old checks mark Graph ready. Switching an existing install from Resend to Graph keeps its saved emailReadyAt. This branch then reports “Email is working” before any Graph test succeeds, even when the new app cannot use the mailbox. Tie the saved check to the active provider and mailbox so switching providers asks for a new test.

Comment thread src/lib/services/email-graph.ts Resolved
Organizations whose mail already lives in Microsoft 365 can send from one
of their own mailboxes through Microsoft Graph instead of bringing in
Resend. The app signs in with OAuth client credentials and calls
POST /users/{mailbox}/sendMail, with no new dependency.

- New driver in src/lib/services/email-graph.ts: token cache, retries on
  429/503 within the same bounds as Resend, one token refresh on 401, and
  Microsoft's own error words passed through to the test email.
- MICROSOFT_GRAPH_SENDER fixes the sender, like RESEND_FROM_EMAIL does;
  Settings names whichever variable is in use.
- Settings, Organization shows a Microsoft 365 status instead of the
  Resend domain steps, and asks for a test email to confirm the mailbox.
- Attachments above about 3 MB go out as links, since Graph takes at most
  4 MB per message.
- docs/self-hosting.md explains how to scope the app to that one mailbox
  with Exchange RBAC for Applications.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread src/lib/services/email-graph.ts Resolved
thomassicaud-asi and others added 2 commits November 23, 2026 09:53
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Addresses two review notes on NYLLON-SOFTWARE#75:

- One Graph sender per configuration for the whole process, so the token
  is reused by every email service instead of fetched again for each
  envelope. A rotated secret or another mailbox gets a sender of its own.
- A 401 that follows a throttled try now still replaces the token once:
  token refresh and throttling retries are counted apart.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thomassicaud

Copy link
Copy Markdown
Author

Merged main into this branch, so it no longer conflicts. I also addressed two of Greptile's notes in a separate commit: the Graph token is now shared across email services (it was fetched again for every envelope), and a 401 that follows a throttled try now still refreshes the token.

The settings flow from #76 is up as #254, on top of this one.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants