Backend/Email

Email

Send real emails from your product: sign-in codes, invites and notifications, and connect each user's own mailbox to the Emails screen. This page shows where Accordee would send and read email, and how to make it real.

Where your product sends email

These screens look like they send email, but in the demo nothing leaves the browser:

WhereWhat happens today
Sign-up and Forgot passwordsendCode in src/lib/auth.ts sends nothing, and every code is 123456
Settings › People: Invite people and Resend inviteAdds an invited member to the list
New workspace: Invite your teamAdds the invites to the new workspace
Invoice editor: SendMarks the invoice as sent and shows a confirmation
Settings › NotificationsEmail as a channel, Send test, and the morning digest email
New meetingAttendees from the customer's contacts, shown as a calendar invite
Workflows: Send email stepUpdates the app, sends nothing
Emails: connect, sync and SendThe mailbox is sample threads in src/lib/data/emails.ts; Send adds the email to the thread in the browser

Two kinds of email

  • Email your product sends (this page). Codes, invites, notifications and digests, sent from your own domain through an email service. Also called transactional email.
  • Email from a user's own mailbox. Settings › Email (Google, Microsoft or IMAP accounts, open tracking, BCC logging and a signature), the Emails screen and the Email inbox app in the App Library read and send from each user's own address. That needs access to their mailbox through Google's or Microsoft's APIs; see Connect a real mailbox below.

Sending an invoice and the workflow Send email step can go either way: from your product's address, or from the user's connected mailbox.

Connect an email service

Choose an email service. Resend, Postmark and SendGrid are common examples; any service with an API works the same way.

Verify your sending domain. The service gives you DNS records (SPF and DKIM) to add at your domain host; add a DMARC record too. Without these, emails land in spam.

Send from the server only: a Server Action or Route Handler, with the API key in an environment variable.

Sending an invite (Resend example)
"use server"
import { Resend } from "resend"

const resend = new Resend(process.env.RESEND_API_KEY)

export async function sendInvite(to: string, link: string) {
  // Check that the signed-in person may invite to this workspace first
  const { error } = await resend.emails.send({
    from: "Your Product <team@yourdomain.com>",
    to,
    subject: "You're invited to join your team",
    html: `<p>Join your team: <a href="${link}">accept the invite</a></p>`,
  })
  if (error) throw new Error(error.message)
}

Hook up the flows one at a time. Start with sign-in codes, then invites, then notifications. Many sign-in providers send their own codes and reset emails, so you may only need to customise those (see Sign-in).

Before you send to customers

  • Respect each person's choices in Settings › Notifications, and add an unsubscribe link to notification and digest emails.
  • Send invites and codes with links that expire, and check on the server that the link is still valid.
  • Test every email with a test inbox before real addresses, and check them in dark mode and on a phone.
  • Watch bounces and complaints in your email service's dashboard.

Connect a real mailbox

The Emails screen runs on sample data. Everything it needs comes through one provider, src/components/providers/emails-provider.tsx: swap its seeded threads for your API and the screens, the customer timeline and Rune keep working.

The sample user starts with a Gmail mailbox already connected (mailbox in DEFAULT_PREFERENCES, src/lib/data/preferences.ts), so the demo opens on the inbox. With real accounts, start it as null: each person then sees the connect screen and links their own mailbox.

Pick the provider APIs. Gmail and Google Workspace use the Gmail API; Outlook and Microsoft 365 use Microsoft Graph. Most teams need both.

Sign in with OAuth and ask only for what you use: read and send (for Gmail, gmail.readonly and gmail.send; for Graph, Mail.Read, Mail.Send and offline_access). Keep the refresh token on the server, encrypted, never in the browser. Google reviews apps that read mail before you can go live with real users, so start that early.

Sync on the server. Import recent mail once (the first-run screen offers 12 months), then get changes as they happen: Gmail watch with Pub/Sub, or Graph change notifications. Run a full sync now and then as a fallback, because push messages can be missed.

Map threads and people. Store each thread with its messages, match addresses to contacts and domains to customers to set customerId, and turn unknown senders into Suggested records instead of creating them straight away.

Index for search. The filters work on fields you already store; searching by topic ("about the renewal") needs a full-text index, or embeddings if you want search by meaning. src/lib/email-ai.ts shows the questions Rune answers.

Enforce the sharing choice on the server. Settings › Email lets each person pick Private, Share with team or Subject lines only; filter what each teammate can read in your queries, not just in the interface. Never send an email the user hasn't sent themselves: Rune's drafts only open in the composer.

Sending a reply through Gmail (server)
"use server"
// After OAuth: a fresh access token for the signed-in user's mailbox
export async function sendReply(accessToken: string, raw: string, threadId: string) {
  // raw: the RFC 822 message, base64url-encoded
  const res = await fetch("https://gmail.googleapis.com/gmail/v1/users/me/messages/send", {
    method: "POST",
    headers: { Authorization: `Bearer ${accessToken}`, "Content-Type": "application/json" },
    body: JSON.stringify({ raw, threadId }),
  })
  if (!res.ok) throw new Error(`Gmail send failed: ${res.status}`)
  return res.json()
}