@novacraft-engineering/mailbox 0.1.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/.env.example +110 -0
- package/LICENSE +21 -0
- package/README.md +75 -0
- package/app/api/mail/accessors/route.ts +197 -0
- package/app/api/mail/attachments/route.ts +54 -0
- package/app/api/mail/company-signature/route.ts +36 -0
- package/app/api/mail/contacts/route.ts +13 -0
- package/app/api/mail/emails/[id]/attachments/download/route.ts +69 -0
- package/app/api/mail/emails/[id]/attachments/forward/route.ts +65 -0
- package/app/api/mail/emails/[id]/route.ts +127 -0
- package/app/api/mail/emails/route.ts +116 -0
- package/app/api/mail/events/route.ts +12 -0
- package/app/api/mail/fonts/route.ts +44 -0
- package/app/api/mail/inbox/attachments/download/route.ts +50 -0
- package/app/api/mail/inbox/attachments/forward/route.ts +54 -0
- package/app/api/mail/inbox/attachments/route.ts +99 -0
- package/app/api/mail/inbox/body/route.ts +14 -0
- package/app/api/mail/inbox/counts/route.ts +25 -0
- package/app/api/mail/inbox/route.ts +506 -0
- package/app/api/mail/link-check/route.ts +95 -0
- package/app/api/mail/login/route.ts +38 -0
- package/app/api/mail/logout/route.ts +9 -0
- package/app/api/mail/maintenance/attachments/route.ts +26 -0
- package/app/api/mail/maintenance/bodies/route.ts +30 -0
- package/app/api/mail/maintenance/list-columns/route.ts +25 -0
- package/app/api/mail/maintenance/threads/route.ts +32 -0
- package/app/api/mail/me/route.ts +13 -0
- package/app/api/mail/outgoing-upload/route.ts +37 -0
- package/app/api/mail/password/route.ts +64 -0
- package/app/api/mail/pixel/[id]/route.ts +20 -0
- package/app/api/mail/push/route.ts +42 -0
- package/app/api/mail/render-template/route.ts +58 -0
- package/app/api/mail/request-reset/route.ts +65 -0
- package/app/api/mail/reset/route.ts +46 -0
- package/app/api/mail/send/route.ts +205 -0
- package/app/api/mail/settings/route.ts +35 -0
- package/app/api/mail/share/attachment/route.ts +91 -0
- package/app/api/mail/share/route.ts +130 -0
- package/app/api/mail/signature-logo/route.ts +67 -0
- package/app/api/mail/stash/route.ts +55 -0
- package/app/api/mail/threads/route.ts +18 -0
- package/app/api/mail/upload/route.ts +43 -0
- package/app/api/share/[id]/route.ts +85 -0
- package/app/apple-icon.png +0 -0
- package/app/brand/[file]/route.ts +37 -0
- package/app/globals.css +14 -0
- package/app/icon.png +0 -0
- package/app/layout.tsx +30 -0
- package/app/mail/AccessCheck.tsx +45 -0
- package/app/mail/AttachmentLightbox.tsx +241 -0
- package/app/mail/ConfirmDialog.tsx +88 -0
- package/app/mail/MailSelect.tsx +138 -0
- package/app/mail/RichEditor.tsx +859 -0
- package/app/mail/layout.tsx +20 -0
- package/app/mail/page.module.css +6320 -0
- package/app/mail/page.tsx +7971 -0
- package/app/mail/pwa.ts +191 -0
- package/app/mail/reset/page.tsx +132 -0
- package/app/mail/search.ts +172 -0
- package/app/manifest.ts +21 -0
- package/app/page.tsx +5 -0
- package/app/robots.ts +22 -0
- package/app/share/[id]/page.tsx +166 -0
- package/app/share/[id]/share.module.css +148 -0
- package/eslint.config.mjs +9 -0
- package/lib/accent-ramp.ts +54 -0
- package/lib/attachments.ts +52 -0
- package/lib/brand.client.ts +52 -0
- package/lib/brand.ts +87 -0
- package/lib/d1.ts +64 -0
- package/lib/default-signature.ts +68 -0
- package/lib/dev-auth.ts +222 -0
- package/lib/email-html.test.ts +74 -0
- package/lib/email-html.ts +169 -0
- package/lib/emails/index.ts +281 -0
- package/lib/emails/templates/academy-followup.html +68 -0
- package/lib/emails/templates/auto-reply.html +33 -0
- package/lib/emails/templates/contact-followup.html +58 -0
- package/lib/emails/templates/field-row.html +4 -0
- package/lib/emails/templates/notification.html +23 -0
- package/lib/fonts.ts +23 -0
- package/lib/link-safety.ts +81 -0
- package/lib/mail-provider.ts +183 -0
- package/lib/mailbox.ts +1888 -0
- package/lib/password.ts +71 -0
- package/lib/public-url.ts +14 -0
- package/lib/push.ts +37 -0
- package/lib/r2.ts +157 -0
- package/lib/rate-limit.ts +43 -0
- package/lib/session.test.ts +43 -0
- package/lib/session.ts +80 -0
- package/lib/signature.ts +28 -0
- package/lib/threads.test.ts +96 -0
- package/lib/threads.ts +72 -0
- package/lib/turso.ts +53 -0
- package/next.config.ts +51 -0
- package/package.json +69 -0
- package/public/brand/README.md +35 -0
- package/public/icon-192.png +0 -0
- package/public/icon-512.png +0 -0
- package/public/icon-maskable-512.png +0 -0
- package/public/sw.js +41 -0
- package/scripts/backfill-attachments.mjs +96 -0
- package/scripts/backfill-content-ids.mjs +101 -0
- package/scripts/backfill-list-columns.mjs +80 -0
- package/scripts/offload-bodies.mjs +152 -0
- package/scripts/sample-files.mjs +119 -0
- package/scripts/seed-dev.mjs +240 -0
- package/tools/README.md +18 -0
- package/tools/db-backup.py +158 -0
- package/tools/doh.py +46 -0
- package/tools/import-mbox.py +703 -0
- package/tools/import-takeout.sh +49 -0
- package/tsconfig.json +35 -0
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One seam between the app and whichever email vendor is in use. Everything above this
|
|
3
|
+
* file speaks these types; everything vendor-specific lives below. Switching providers
|
|
4
|
+
* is then a config change plus one adapter, not a rewrite.
|
|
5
|
+
*
|
|
6
|
+
* Selected with MAIL_PROVIDER=brevo|resend (defaults to resend so nothing changes until
|
|
7
|
+
* the switch is deliberate).
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export type MailProvider = 'resend' | 'brevo'
|
|
11
|
+
|
|
12
|
+
export type SendAttachment = { filename: string; content: string; contentType?: string }
|
|
13
|
+
|
|
14
|
+
export type SendPayload = {
|
|
15
|
+
from: string
|
|
16
|
+
fromName?: string
|
|
17
|
+
to: string[]
|
|
18
|
+
cc?: string[]
|
|
19
|
+
bcc?: string[]
|
|
20
|
+
replyTo?: string
|
|
21
|
+
subject: string
|
|
22
|
+
html?: string | null
|
|
23
|
+
text?: string | null
|
|
24
|
+
headers?: Record<string, string>
|
|
25
|
+
scheduledAt?: string
|
|
26
|
+
attachments?: SendAttachment[]
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type SendResult = { id: string | null }
|
|
30
|
+
|
|
31
|
+
/** Provider-neutral shape of a received message, matching what the inbox stores. */
|
|
32
|
+
export type NormalizedInbound = {
|
|
33
|
+
id: string
|
|
34
|
+
from: string
|
|
35
|
+
to: string[]
|
|
36
|
+
cc: string[]
|
|
37
|
+
bcc: string[]
|
|
38
|
+
replyTo: string[]
|
|
39
|
+
subject: string
|
|
40
|
+
html: string | null
|
|
41
|
+
text: string | null
|
|
42
|
+
headers: Record<string, unknown>
|
|
43
|
+
receivedAt: string
|
|
44
|
+
attachments: Array<{ filename: string; contentType?: string; url?: string; size?: number }>
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function activeProvider(): MailProvider {
|
|
48
|
+
return process.env.MAIL_PROVIDER === 'brevo' ? 'brevo' : 'resend'
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function splitAddress(raw: string): { email: string; name?: string } {
|
|
52
|
+
const match = raw.match(/^\s*(.*?)\s*<([^>]+)>\s*$/)
|
|
53
|
+
if (match) return { email: match[2].trim(), name: match[1].replace(/^"|"$/g, '') || undefined }
|
|
54
|
+
return { email: raw.trim() }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// ── Sending ─────────────────────────────────────────────────────
|
|
58
|
+
async function sendViaResend(payload: SendPayload): Promise<SendResult> {
|
|
59
|
+
const apiKey = process.env.RESEND_API_KEY
|
|
60
|
+
if (!apiKey) throw new Error('RESEND_API_KEY is not configured')
|
|
61
|
+
const { Resend } = await import('resend')
|
|
62
|
+
const resend = new Resend(apiKey)
|
|
63
|
+
const from = payload.fromName ? `${payload.fromName} <${payload.from}>` : payload.from
|
|
64
|
+
const { data, error } = await resend.emails.send({
|
|
65
|
+
from,
|
|
66
|
+
to: payload.to,
|
|
67
|
+
subject: payload.subject,
|
|
68
|
+
text: payload.text?.trim() || ' ',
|
|
69
|
+
...(payload.html ? { html: payload.html } : {}),
|
|
70
|
+
...(payload.cc?.length ? { cc: payload.cc } : {}),
|
|
71
|
+
...(payload.bcc?.length ? { bcc: payload.bcc } : {}),
|
|
72
|
+
...(payload.replyTo ? { replyTo: payload.replyTo } : {}),
|
|
73
|
+
...(payload.scheduledAt ? { scheduledAt: payload.scheduledAt } : {}),
|
|
74
|
+
...(payload.attachments?.length ? { attachments: payload.attachments.map(a => ({ filename: a.filename, content: a.content })) } : {}),
|
|
75
|
+
...(payload.headers ? { headers: payload.headers } : {}),
|
|
76
|
+
})
|
|
77
|
+
if (error) throw new Error(error.message)
|
|
78
|
+
return { id: data?.id ?? null }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function sendViaBrevo(payload: SendPayload): Promise<SendResult> {
|
|
82
|
+
const apiKey = process.env.BREVO_API_KEY
|
|
83
|
+
if (!apiKey) throw new Error('BREVO_API_KEY is not configured')
|
|
84
|
+
// Brevo has no scheduling on the transactional endpoint we use; the caller decides
|
|
85
|
+
// whether to hold the message instead of silently sending it now.
|
|
86
|
+
if (payload.scheduledAt) throw new Error('Scheduled sending is not supported on Brevo')
|
|
87
|
+
|
|
88
|
+
const body: Record<string, unknown> = {
|
|
89
|
+
sender: { email: payload.from, ...(payload.fromName ? { name: payload.fromName } : {}) },
|
|
90
|
+
to: payload.to.map(splitAddress),
|
|
91
|
+
subject: payload.subject,
|
|
92
|
+
...(payload.html ? { htmlContent: payload.html } : {}),
|
|
93
|
+
...(payload.text?.trim() ? { textContent: payload.text.trim() } : {}),
|
|
94
|
+
...(payload.cc?.length ? { cc: payload.cc.map(splitAddress) } : {}),
|
|
95
|
+
...(payload.bcc?.length ? { bcc: payload.bcc.map(splitAddress) } : {}),
|
|
96
|
+
...(payload.replyTo ? { replyTo: splitAddress(payload.replyTo) } : {}),
|
|
97
|
+
...(payload.headers ? { headers: payload.headers } : {}),
|
|
98
|
+
...(payload.attachments?.length
|
|
99
|
+
? { attachment: payload.attachments.map(a => ({ name: a.filename, content: a.content })) }
|
|
100
|
+
: {}),
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const response = await fetch('https://api.brevo.com/v3/smtp/email', {
|
|
104
|
+
method: 'POST',
|
|
105
|
+
headers: { 'api-key': apiKey, 'Content-Type': 'application/json', accept: 'application/json' },
|
|
106
|
+
body: JSON.stringify(body),
|
|
107
|
+
})
|
|
108
|
+
const result = (await response.json().catch(() => ({}))) as { messageId?: string; message?: string; code?: string }
|
|
109
|
+
if (!response.ok) throw new Error(result.message || `Brevo send failed (HTTP ${response.status})`)
|
|
110
|
+
return { id: result.messageId ? String(result.messageId).replace(/^<|>$/g, '') : null }
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function sendMail(payload: SendPayload): Promise<SendResult> {
|
|
114
|
+
return activeProvider() === 'brevo' ? sendViaBrevo(payload) : sendViaResend(payload)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// ── Receiving ───────────────────────────────────────────────────
|
|
118
|
+
const asArray = (value: unknown): string[] => (Array.isArray(value) ? value.map(String) : [])
|
|
119
|
+
|
|
120
|
+
type BrevoAddress = { Address?: string; Name?: string } | string
|
|
121
|
+
type BrevoAttachment = { Name?: string; ContentType?: string; ContentLength?: number; DownloadToken?: string }
|
|
122
|
+
type BrevoInboundItem = {
|
|
123
|
+
Headers?: Record<string, unknown>
|
|
124
|
+
MessageId?: string
|
|
125
|
+
Uuid?: string | string[]
|
|
126
|
+
From?: BrevoAddress
|
|
127
|
+
To?: BrevoAddress[]
|
|
128
|
+
Cc?: BrevoAddress[]
|
|
129
|
+
Bcc?: BrevoAddress[]
|
|
130
|
+
ReplyTo?: BrevoAddress
|
|
131
|
+
Subject?: string
|
|
132
|
+
RawHtmlBody?: string | null
|
|
133
|
+
HtmlBody?: string | null
|
|
134
|
+
RawTextBody?: string | null
|
|
135
|
+
TextBody?: string | null
|
|
136
|
+
SentAtDate?: string
|
|
137
|
+
Attachments?: BrevoAttachment[]
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Brevo inbound parsing delivers `items[]`; addresses arrive as {Address, Name}. */
|
|
141
|
+
export function normalizeBrevoInbound(raw: Record<string, unknown>): NormalizedInbound {
|
|
142
|
+
const item = raw as BrevoInboundItem
|
|
143
|
+
const addr = (entry: BrevoAddress | undefined): string => {
|
|
144
|
+
if (typeof entry === 'string') return entry
|
|
145
|
+
return entry?.Name ? `${entry.Name} <${entry.Address}>` : String(entry?.Address ?? '')
|
|
146
|
+
}
|
|
147
|
+
const list = (value: BrevoAddress[] | undefined): string[] =>
|
|
148
|
+
Array.isArray(value) ? value.map(addr).filter(Boolean) : []
|
|
149
|
+
const headers: Record<string, unknown> = { ...(item.Headers ?? {}) }
|
|
150
|
+
if (item.MessageId && !Object.keys(headers).some(key => key.toLowerCase() === 'message-id')) {
|
|
151
|
+
headers['message-id'] = item.MessageId
|
|
152
|
+
}
|
|
153
|
+
return {
|
|
154
|
+
id: String(item.MessageId ?? item.Uuid?.[0] ?? item.Uuid ?? `in_${Date.now()}`).replace(/^<|>$/g, ''),
|
|
155
|
+
from: addr(item.From),
|
|
156
|
+
to: list(item.To),
|
|
157
|
+
cc: list(item.Cc),
|
|
158
|
+
bcc: list(item.Bcc),
|
|
159
|
+
replyTo: item.ReplyTo ? [addr(item.ReplyTo)] : [],
|
|
160
|
+
subject: String(item.Subject ?? '(no subject)'),
|
|
161
|
+
html: item.RawHtmlBody ?? item.HtmlBody ?? null,
|
|
162
|
+
text: item.RawTextBody ?? item.TextBody ?? null,
|
|
163
|
+
headers,
|
|
164
|
+
receivedAt: item.SentAtDate ? new Date(item.SentAtDate).toISOString() : new Date().toISOString(),
|
|
165
|
+
attachments: Array.isArray(item.Attachments)
|
|
166
|
+
? item.Attachments.map(attachment => ({
|
|
167
|
+
filename: String(attachment.Name ?? 'attachment'),
|
|
168
|
+
contentType: attachment.ContentType ? String(attachment.ContentType) : undefined,
|
|
169
|
+
size: typeof attachment.ContentLength === 'number' ? attachment.ContentLength : undefined,
|
|
170
|
+
// Brevo exposes a token-based download URL; Phase 0's rule applies — fetch and
|
|
171
|
+
// re-host rather than trusting a provider URL to outlive the message.
|
|
172
|
+
url: attachment.DownloadToken ? `https://api.brevo.com/v3/inbound/attachments/${attachment.DownloadToken}` : undefined,
|
|
173
|
+
}))
|
|
174
|
+
: [],
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** True when the payload looks like Brevo inbound parsing rather than a Resend webhook. */
|
|
179
|
+
export function isBrevoInbound(body: Record<string, unknown>): boolean {
|
|
180
|
+
return Array.isArray(body?.items) || Boolean(body?.Uuid || body?.RawHtmlBody || body?.SentAtDate)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export { asArray }
|