@yaks/mail 0.0.0-stage → 0.2.36
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/LICENSE +202 -0
- package/README.md +676 -2
- package/_dist/addr.d.ts +40 -0
- package/_dist/addr.d.ts.map +1 -0
- package/_dist/answers.d.ts +18 -0
- package/_dist/answers.d.ts.map +1 -0
- package/_dist/arrive.d.ts +64 -0
- package/_dist/arrive.d.ts.map +1 -0
- package/_dist/capture.d.ts +22 -0
- package/_dist/capture.d.ts.map +1 -0
- package/_dist/comp.d.ts +27 -0
- package/_dist/comp.d.ts.map +1 -0
- package/_dist/effects.d.ts +9 -0
- package/_dist/effects.d.ts.map +1 -0
- package/_dist/gmail.d.ts +16 -0
- package/_dist/gmail.d.ts.map +1 -0
- package/_dist/graph.d.ts +9 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/inbound.d.ts +105 -0
- package/_dist/inbound.d.ts.map +1 -0
- package/_dist/invite.d.ts +69 -0
- package/_dist/invite.d.ts.map +1 -0
- package/_dist/md.d.ts +34 -0
- package/_dist/md.d.ts.map +1 -0
- package/_dist/mod.d.ts +109 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/options.d.ts +38 -0
- package/_dist/options.d.ts.map +1 -0
- package/_dist/plugin.d.ts +38 -0
- package/_dist/plugin.d.ts.map +1 -0
- package/_dist/pull.d.ts +99 -0
- package/_dist/pull.d.ts.map +1 -0
- package/_dist/receipts.d.ts +8 -0
- package/_dist/receipts.d.ts.map +1 -0
- package/_dist/recipient.d.ts +24 -0
- package/_dist/recipient.d.ts.map +1 -0
- package/_dist/routes.d.ts +19 -0
- package/_dist/routes.d.ts.map +1 -0
- package/_dist/send.d.ts +80 -0
- package/_dist/send.d.ts.map +1 -0
- package/_dist/service.d.ts +14 -0
- package/_dist/service.d.ts.map +1 -0
- package/_dist/stash.d.ts +23 -0
- package/_dist/stash.d.ts.map +1 -0
- package/_dist/tools.d.ts +27 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/transport.d.ts +6 -0
- package/_dist/transport.d.ts.map +1 -0
- package/_dist/vocab.d.ts +7 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/addr.js +92 -0
- package/addr.js.map +1 -0
- package/addr.ts +106 -0
- package/answers.js +71 -0
- package/answers.js.map +1 -0
- package/answers.ts +95 -0
- package/arrive.js +122 -0
- package/arrive.js.map +1 -0
- package/arrive.ts +198 -0
- package/capture.js +21 -0
- package/capture.js.map +1 -0
- package/capture.ts +43 -0
- package/comp.js +76 -0
- package/comp.js.map +1 -0
- package/comp.ts +91 -0
- package/deno.json +1 -0
- package/effects.js +90 -0
- package/effects.js.map +1 -0
- package/effects.ts +90 -0
- package/gmail.js +262 -0
- package/gmail.js.map +1 -0
- package/gmail.ts +314 -0
- package/graph.js +26 -0
- package/graph.js.map +1 -0
- package/graph.ts +30 -0
- package/inbound.js +206 -0
- package/inbound.js.map +1 -0
- package/inbound.ts +238 -0
- package/invite.js +102 -0
- package/invite.js.map +1 -0
- package/invite.ts +143 -0
- package/md.js +188 -0
- package/md.js.map +1 -0
- package/md.ts +245 -0
- package/mod.js +109 -0
- package/mod.js.map +1 -0
- package/mod.ts +111 -0
- package/options.js +24 -0
- package/options.js.map +1 -0
- package/options.ts +83 -0
- package/package.json +66 -4
- package/plugin.js +97 -0
- package/plugin.js.map +1 -0
- package/plugin.ts +88 -0
- package/pull.js +344 -0
- package/pull.js.map +1 -0
- package/pull.ts +443 -0
- package/readings.js +24 -0
- package/readings.js.map +1 -0
- package/readings.ts +34 -0
- package/receipts.js +45 -0
- package/receipts.js.map +1 -0
- package/receipts.ts +45 -0
- package/recipient.js +92 -0
- package/recipient.js.map +1 -0
- package/recipient.ts +89 -0
- package/routes.js +103 -0
- package/routes.js.map +1 -0
- package/routes.ts +125 -0
- package/send.js +284 -0
- package/send.js.map +1 -0
- package/send.ts +325 -0
- package/service.js +64 -0
- package/service.js.map +1 -0
- package/service.ts +75 -0
- package/stash.js +29 -0
- package/stash.js.map +1 -0
- package/stash.ts +49 -0
- package/tools.js +343 -0
- package/tools.js.map +1 -0
- package/tools.ts +416 -0
- package/transport.js +23 -0
- package/transport.js.map +1 -0
- package/transport.ts +24 -0
- package/vocab.js +14 -0
- package/vocab.js.map +1 -0
- package/vocab.json +415 -0
- package/vocab.ts +16 -0
- package/work.js +121 -0
- package/work.js.map +1 -0
- package/work.ts +134 -0
package/README.md
CHANGED
|
@@ -1,3 +1,677 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @yaks/mail
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Email storage, incoming-message processing, and outgoing delivery for
|
|
4
|
+
[@yaks/graph](../graph/README.md). Use it to record correspondence, send
|
|
5
|
+
messages when graph entities are created, or read and answer mail through the
|
|
6
|
+
CLI and MCP.
|
|
7
|
+
|
|
8
|
+
The graph's storage adapter stores messages, addresses, recipients, and delivery
|
|
9
|
+
outcomes. This package opens no database of its own. You supply the email
|
|
10
|
+
transport; `stash()` provides an in-memory transport for tests and development.
|
|
11
|
+
A **bundle** is one entity's components as a JSON object. A **batch** is a list
|
|
12
|
+
of bundles applied in one transaction.
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
deno add jsr:@yaks/mail
|
|
18
|
+
# or: npx jsr add @yaks/mail
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Example: membership and messages
|
|
22
|
+
|
|
23
|
+
A message is an entity with these components, here about the entity `potluck`
|
|
24
|
+
and addressed to the entity `ana`:
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
let message = {
|
|
28
|
+
entity: { eid: 'e1' },
|
|
29
|
+
doc: {
|
|
30
|
+
title: 'Potluck Friday',
|
|
31
|
+
body: 'Bring a dish. [Sign up](https://books.example/potluck)',
|
|
32
|
+
},
|
|
33
|
+
mail: { from: 'hello@books.example', target: 'potluck' },
|
|
34
|
+
deliver: { to: 'ana' },
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
| Component | Stored information |
|
|
39
|
+
| ----------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
40
|
+
| `mail` | Sender and recipient addresses, time, related entity (`target`), threading fields, Message-ID (arrived with, or given by the transport), verification result, and where an answer goes (`reply_address`, from `Reply-To:`). |
|
|
41
|
+
| `email` | An entity's email address (`address`). |
|
|
42
|
+
| `recipient` | An `email` row only a letter names; the `recipients()` plugin marks it, and an app keeps it to its owners and editors (`readers: editor`). |
|
|
43
|
+
| `deliver` | The recipient entity id (`to`) and when the message was handed to the sender (`tried`); requests outgoing delivery. |
|
|
44
|
+
| `delivered` | Delivery time (`at`). |
|
|
45
|
+
| `bounced` | Failed delivery time and reason (`at`, `reason`). |
|
|
46
|
+
| `notified` | When a recipient was notified, by whom, and through which entity (`at`, `by`, `via`). |
|
|
47
|
+
|
|
48
|
+
The subject and body use `doc{title, body}` from [@yaks/doc](../doc/README.md).
|
|
49
|
+
Load that package alongside this one. `mail.target` can refer to any entity, so
|
|
50
|
+
a page or task can have related correspondence. `deliver.to` refers to an entity
|
|
51
|
+
whose `email.address` is looked up at send time; `mail.to` records the address
|
|
52
|
+
used once delivery succeeds.
|
|
53
|
+
|
|
54
|
+
## Delivery requests
|
|
55
|
+
|
|
56
|
+
A message with both `mail` and `deliver` requests sending. A message with only
|
|
57
|
+
`mail` is stored without sending, as a draft or received message. Received
|
|
58
|
+
messages have no `deliver`, so receiving them does not send them back out.
|
|
59
|
+
|
|
60
|
+
The declared sending handler runs when either `mail` or `deliver` is created, so
|
|
61
|
+
adding a recipient to a saved draft sends it. The handler skips a message that
|
|
62
|
+
already has `delivered`, `bounced` or `deliver.tried`; retry those by creating a
|
|
63
|
+
new message.
|
|
64
|
+
|
|
65
|
+
Sending is at most once. The handler stamps `deliver.tried` before the message
|
|
66
|
+
reaches the transport, so a crash between the send and its outcome leaves a
|
|
67
|
+
message with `tried` and no outcome, which is never handed over again. A sender
|
|
68
|
+
that knows the message never left rejects with `Unsent` (`@yaks/host/senders`):
|
|
69
|
+
the handler gives the claim back, writes the reason to `deliver.waiting` and
|
|
70
|
+
fails the run, so the pool hands it over again after `retry.after` (six tries).
|
|
71
|
+
The provider senders reject that way for a failed connection and a 429. Each
|
|
72
|
+
write owes its delivery in the durable effect pool, so a worker can pick it up
|
|
73
|
+
after an outage without rediscovering messages. Exhausted transport failures
|
|
74
|
+
remain failed until explicitly retried with `effect retry`.
|
|
75
|
+
|
|
76
|
+
## Delivery after commit
|
|
77
|
+
|
|
78
|
+
`sending()` registers with [@yaks/effects](../effects/README.md) and calls the
|
|
79
|
+
transport after the original transaction commits. It records the outcome through
|
|
80
|
+
a second `graph.apply()` call. This makes the outcome available to queries,
|
|
81
|
+
subscriptions, and journaling when those plugins are configured. A transport
|
|
82
|
+
failure does not roll back the original message. Where the graph keeps no effect
|
|
83
|
+
pool, the send is awaited, so a slow transport delays completion of the call;
|
|
84
|
+
with one, the send is written down with the message and run by a worker.
|
|
85
|
+
|
|
86
|
+
This complete in-memory example sends through `stash()`:
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
import { effects } from '@yaks/effects'
|
|
90
|
+
import { graph } from '@yaks/graph'
|
|
91
|
+
import { ram } from '@yaks/ram'
|
|
92
|
+
import { loadVocab } from '@yaks/vocab'
|
|
93
|
+
import { docDoc, docs } from '@yaks/doc'
|
|
94
|
+
import { mailbox, mailDoc, stash } from '@yaks/mail'
|
|
95
|
+
|
|
96
|
+
let vocab = loadVocab([docDoc, mailDoc])
|
|
97
|
+
let fx = effects(vocab, { write: (b) => g.apply(b, { trusted: true }) })
|
|
98
|
+
let post = stash()
|
|
99
|
+
let g = graph({
|
|
100
|
+
storage: ram(vocab),
|
|
101
|
+
vocab,
|
|
102
|
+
plugins: [
|
|
103
|
+
fx,
|
|
104
|
+
docs(),
|
|
105
|
+
mailbox({ domain: 'books.example', sender: post, effects: fx }),
|
|
106
|
+
],
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
await g.apply([
|
|
110
|
+
{ entity: { eid: 'ana' }, email: { address: 'ana@books.example' } },
|
|
111
|
+
{
|
|
112
|
+
entity: { eid: 'message' },
|
|
113
|
+
doc: { title: 'Thursday', body: 'We meet at seven.' },
|
|
114
|
+
mail: { from: 'hello@books.example' },
|
|
115
|
+
deliver: { to: 'ana' },
|
|
116
|
+
},
|
|
117
|
+
])
|
|
118
|
+
console.log(post.last()?.subject) // Thursday
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
The outcome writer uses `trusted: true` because `delivered` and `bounced`
|
|
122
|
+
properties are server-owned. `docs()` supplies the document component
|
|
123
|
+
separately; `mailbox()` declares only the six mail components and normalizes
|
|
124
|
+
addresses. Pass both `effects` and `sender` to `mailbox()` to enable automatic
|
|
125
|
+
sending.
|
|
126
|
+
|
|
127
|
+
Success writes `delivered{at}`, and the Message-ID the receipt carries as
|
|
128
|
+
`mail.message_id`, which is what a reply to the letter threads on. A rejected
|
|
129
|
+
send writes `bounced{at, reason}`, unless it was `Unsent`. An `Undeliverable`
|
|
130
|
+
rejection, a provider's refusal of an address on the letter, bounces it with
|
|
131
|
+
`refused: true` and ends the run; every other bounce also fails the run, so it
|
|
132
|
+
reaches the host's reporter. Missing recipient addresses and missing sender
|
|
133
|
+
addresses also produce `bounced`.
|
|
134
|
+
|
|
135
|
+
### The transport is supplied by the caller
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
import type { Message, Receipt } from '@yaks/mail'
|
|
139
|
+
|
|
140
|
+
type Sender = {
|
|
141
|
+
send: (message: Message) => Promise<Receipt>
|
|
142
|
+
waiting?: () => string | undefined
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Credentials, endpoints and HTTP belong to the sender, never to this package.
|
|
147
|
+
`stash()` retains messages in `sent`, provides `last()`, and returns receipts
|
|
148
|
+
named `stash-1`, `stash-2`, and so on. `stash({ refuse: 'reason' })` simulates
|
|
149
|
+
delivery failure. A sender may also say why it cannot send yet, such as a
|
|
150
|
+
credential not set, with `waiting()`: a letter then waits instead of being
|
|
151
|
+
handed over.
|
|
152
|
+
|
|
153
|
+
### Named transports
|
|
154
|
+
|
|
155
|
+
`post(sender, host)` selects a sender by name from `host.senders`, or the
|
|
156
|
+
built-in `stash`. A provider's sender is lent by its own plugin, listed in the
|
|
157
|
+
config beside this one with the credentials it needs:
|
|
158
|
+
[@yaks/resend](../resend/README.md) lends `resend` and
|
|
159
|
+
[@yaks/cloudflare-mail](../cloudflare-mail/README.md) lends `cloudflare`. A host
|
|
160
|
+
may lend its own, such as a metered sender, through the same interface.
|
|
161
|
+
|
|
162
|
+
```ts
|
|
163
|
+
import { post } from '@yaks/mail'
|
|
164
|
+
import { equal } from '@yaks/testing'
|
|
165
|
+
|
|
166
|
+
equal(post({ via: 'resend' }), { waiting: 'no sender called "resend"' })
|
|
167
|
+
let sender = { send: () => Promise.resolve({}), waiting: () => 'no token yet' }
|
|
168
|
+
equal(post({ via: 'resend' }, { senders: { resend: sender } }), {
|
|
169
|
+
waiting: 'no token yet',
|
|
170
|
+
})
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
So the configuration names the sender's plugin and selects it:
|
|
174
|
+
|
|
175
|
+
```json
|
|
176
|
+
{
|
|
177
|
+
"plugins": [
|
|
178
|
+
{
|
|
179
|
+
"use": "@yaks/resend",
|
|
180
|
+
"with": { "token": { "secret": "RESEND_TOKEN" } }
|
|
181
|
+
},
|
|
182
|
+
{ "use": "@yaks/mail", "with": { "sender": { "via": "resend" } } }
|
|
183
|
+
]
|
|
184
|
+
}
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
A message's `to` accepts an address or a list, and `replyAddress` chooses where
|
|
188
|
+
replies arrive. `replyTo` remains the RFC Message-ID being answered. A sender
|
|
189
|
+
preserves those fields and `cc`.
|
|
190
|
+
|
|
191
|
+
A provider whose send response carries no RFC Message-ID, as Resend's does not,
|
|
192
|
+
returns an empty receipt rather than an API id as `mail.message_id`. Its plugin
|
|
193
|
+
can take the provider's signed delivery receipt on a route of its own and fill
|
|
194
|
+
`mail.message_id` on the same letter with `receipted(graph, {letter, id})`. A
|
|
195
|
+
receipt arriving before its letter is retried rather than discarded.
|
|
196
|
+
|
|
197
|
+
Selecting another transport retains the same durable delivery requests. Owed
|
|
198
|
+
letters use the selected sender; `delivered`, `bounced` and `deliver.tried` keep
|
|
199
|
+
preventing repeated handovers. No transport failure selects another provider. A
|
|
200
|
+
connection failure known to precede sending or an HTTP 429 rejects as `Unsent`;
|
|
201
|
+
an ambiguous failure retains the attempted handover.
|
|
202
|
+
|
|
203
|
+
If the sender is unavailable, the letter records the reason in
|
|
204
|
+
`deliver.waiting`. Its queued delivery stays pending without being retried while
|
|
205
|
+
that reason still applies. Delivery resumes once a worker has the sender.
|
|
206
|
+
|
|
207
|
+
### Local delivery
|
|
208
|
+
|
|
209
|
+
For addresses whose recipients read messages directly from this graph, pass a
|
|
210
|
+
local domain:
|
|
211
|
+
|
|
212
|
+
```ts
|
|
213
|
+
import { effects } from '@yaks/effects'
|
|
214
|
+
import { loadVocab } from '@yaks/vocab'
|
|
215
|
+
import { docDoc } from '@yaks/doc'
|
|
216
|
+
import { mailbox, mailDoc, stash } from '@yaks/mail'
|
|
217
|
+
|
|
218
|
+
let fx = effects(loadVocab([docDoc, mailDoc]))
|
|
219
|
+
mailbox({
|
|
220
|
+
domain: 'books.example',
|
|
221
|
+
local: 'books.example',
|
|
222
|
+
sender: stash(),
|
|
223
|
+
effects: fx,
|
|
224
|
+
})
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
Recipients at that domain get `delivered` and `mail.to` without a transport
|
|
228
|
+
call. Omit `local` when mail for that domain must reach an external mailbox. The
|
|
229
|
+
configuration-file form uses `local: true` with `domain`; the direct `mailbox()`
|
|
230
|
+
API takes the domain string as `local`.
|
|
231
|
+
|
|
232
|
+
## Incoming messages
|
|
233
|
+
|
|
234
|
+
`inbound()` converts a message into a bundle without querying the graph. It
|
|
235
|
+
accepts `from`, `to`, and a `headers` object with a `get(name)` method, matching
|
|
236
|
+
the relevant parts of a Cloudflare Email Workers message. Parse MIME separately
|
|
237
|
+
and supply the resulting text:
|
|
238
|
+
|
|
239
|
+
```ts
|
|
240
|
+
import { inbound } from '@yaks/mail'
|
|
241
|
+
|
|
242
|
+
let message = {
|
|
243
|
+
from: 'bounces@relay.example',
|
|
244
|
+
to: 'hello@books.example',
|
|
245
|
+
headers: new Headers({
|
|
246
|
+
From: 'Ana <ana@books.example>',
|
|
247
|
+
Subject: 'Thursday',
|
|
248
|
+
'Message-ID': '<message-1@books.example>',
|
|
249
|
+
}),
|
|
250
|
+
}
|
|
251
|
+
let batch = inbound(message, { text: 'See you at seven.' })
|
|
252
|
+
console.log(batch[0].doc) // { title: 'Thursday', body: 'See you at seven.' }
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
The author comes from the `From` header, falling back to the envelope sender.
|
|
256
|
+
The subject, Message-ID, and date come from headers. `Arrival` options can
|
|
257
|
+
supply an entity id, timestamp, target, reply entity, and verification result.
|
|
258
|
+
Defaults use a new UUID and, when no date is supplied, the current time.
|
|
259
|
+
|
|
260
|
+
### The two questions that need a graph
|
|
261
|
+
|
|
262
|
+
`arrived()` adds recipient routing, reply lookup, duplicate detection, and
|
|
263
|
+
author attribution:
|
|
264
|
+
|
|
265
|
+
```ts
|
|
266
|
+
import { graph } from '@yaks/graph'
|
|
267
|
+
import { ram } from '@yaks/ram'
|
|
268
|
+
import { loadVocab } from '@yaks/vocab'
|
|
269
|
+
import { docDoc, docs } from '@yaks/doc'
|
|
270
|
+
import { arrived, mailbox, mailDoc } from '@yaks/mail'
|
|
271
|
+
|
|
272
|
+
let vocab = loadVocab([docDoc, mailDoc])
|
|
273
|
+
let g = graph({
|
|
274
|
+
storage: ram(vocab),
|
|
275
|
+
vocab,
|
|
276
|
+
plugins: [docs(), mailbox({ domain: 'books.example' })],
|
|
277
|
+
})
|
|
278
|
+
await g.apply([{ entity: { eid: 'triage' }, doc: { title: 'Triage' } }])
|
|
279
|
+
|
|
280
|
+
let message = {
|
|
281
|
+
from: 'ana@books.example',
|
|
282
|
+
to: 'hello@books.example',
|
|
283
|
+
headers: new Headers({ Subject: 'Thursday' }),
|
|
284
|
+
}
|
|
285
|
+
let receive = arrived({ graph: g, domain: 'books.example', triage: 'triage' })
|
|
286
|
+
await g.apply(await receive(message, { text: 'See you at seven.' }))
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
`triage` is an existing fallback entity id. `wearer()` finds an entity by its
|
|
290
|
+
`email.address`. For the configured domain, `named()` also resolves an address's
|
|
291
|
+
local part through `graph.address`: for example, `S-31@books.example` can name
|
|
292
|
+
entity S-31 without a separate email record. `routed()` tries the address record
|
|
293
|
+
first, then the id. Unmatched recipients use `triage`, or have no target if it
|
|
294
|
+
is omitted.
|
|
295
|
+
|
|
296
|
+
`known()` finds a stored message by Message-ID, whether it arrived or this graph
|
|
297
|
+
sent it. `arrived()` returns `[]` if that id is already stored, and resolves
|
|
298
|
+
`In-Reply-To` to `mail.reply_to` when possible. This duplicate check is a read
|
|
299
|
+
before the caller writes, not a uniqueness constraint covering concurrent
|
|
300
|
+
arrivals. Messages without a Message-ID cannot be deduplicated this way.
|
|
301
|
+
|
|
302
|
+
The author is found as a recipient is (`routed()`), and writes the letter:
|
|
303
|
+
`$actor.by`, but only when the receiving edge vouched for the From address
|
|
304
|
+
(`Arrival.vouched`: DMARC, or DKIM by the From domain, aligned with it). A From
|
|
305
|
+
header alone is anyone's to write, so an unvouched letter, like an unknown
|
|
306
|
+
sender's, carries an empty `$actor` and stays unattributed rather than being
|
|
307
|
+
signed as someone here. `arrived()` also reads the DKIM result from
|
|
308
|
+
`Authentication-Results` unless the caller supplies `verified`. A failed result
|
|
309
|
+
is recorded as `false`, not grounds for discarding the message.
|
|
310
|
+
|
|
311
|
+
## The arrival endpoint
|
|
312
|
+
|
|
313
|
+
`@yaks/mail/routes` exports `routes()`, which provides `POST /mail/inbound` by
|
|
314
|
+
default. It accepts the message headers and already parsed body:
|
|
315
|
+
|
|
316
|
+
```sh
|
|
317
|
+
curl -X POST http://localhost:8000/mail/inbound \
|
|
318
|
+
-H 'content-type: application/json' -H 'authorization: Bearer TOKEN' -d '{
|
|
319
|
+
"from": "bounces@relay.example",
|
|
320
|
+
"to": "ana@books.example",
|
|
321
|
+
"headers": { "From": "Ana <ana@books.example>", "Subject": "Thursday" },
|
|
322
|
+
"text": "See you at seven."
|
|
323
|
+
}'
|
|
324
|
+
# {"eid":"..."}, or {"eid":null} if that Message-ID was already stored
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
`verified` is an optional top-level boolean when the receiving system supplies a
|
|
328
|
+
DKIM result separately, and `vouched` says the edge authenticated the From
|
|
329
|
+
address. The edge presents the bearer token `door.secret` names; without one
|
|
330
|
+
configured the route answers 401 and records nothing, since a caller who could
|
|
331
|
+
post would also say who wrote the letter. `door.path` changes the path, and the
|
|
332
|
+
top-level `triage` sets the fallback recipient entity.
|
|
333
|
+
|
|
334
|
+
A plugin entry in a `yak serve` configuration can be, beside
|
|
335
|
+
`{"use": "@yaks/cloudflare-mail", "with": {"account": "a1b2", "token": {"secret": "CF_EMAIL_TOKEN"}}}`,
|
|
336
|
+
which lends the `cloudflare` sender:
|
|
337
|
+
|
|
338
|
+
```json
|
|
339
|
+
{
|
|
340
|
+
"use": "@yaks/mail",
|
|
341
|
+
"with": {
|
|
342
|
+
"domain": "books.example",
|
|
343
|
+
"local": true,
|
|
344
|
+
"triage": "triage",
|
|
345
|
+
"sender": { "via": "cloudflare" },
|
|
346
|
+
"door": {
|
|
347
|
+
"path": "/mail/inbound",
|
|
348
|
+
"secret": { "secret": "MAIL_DOOR_SECRET" }
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
The CLI host resolves `{ "secret": "NAME" }` through
|
|
355
|
+
[@yaks/secrets](../secrets); direct TypeScript calls receive strings.
|
|
356
|
+
`sender: { "via": "platform" }` selects a sender named `platform` in
|
|
357
|
+
[`Host.senders`](../host/README.md#mail-senders). A sender lent under the name
|
|
358
|
+
`stash` takes precedence over the built-in one. No sender configuration leaves
|
|
359
|
+
outbound requests waiting too. A sender still waiting for its credentials, or a
|
|
360
|
+
name nothing lends, writes `deliver.waiting` on every owed letter without
|
|
361
|
+
marking it tried. In the pool, that same run remains pending and checks again
|
|
362
|
+
after a minute without spending failed attempts. Direct calls without a pool
|
|
363
|
+
throw the configuration error. The sender is read on each attempt, so repaired
|
|
364
|
+
configuration is used without rebuilding the handler. Successful delivery clears
|
|
365
|
+
the waiting reason. A transport rejection records `bounced.reason` and is also
|
|
366
|
+
reported, without sending that letter twice. `mail check` reports configuration
|
|
367
|
+
problems too.
|
|
368
|
+
|
|
369
|
+
## A letter to a project is work
|
|
370
|
+
|
|
371
|
+
Where `local` is set, a letter that arrives for a project is filed as work in
|
|
372
|
+
it, the way @yaks/heal files a task for each bug the tracker opens. A session of
|
|
373
|
+
the project hears the letter once on its bus (@yaks/session) and marks it
|
|
374
|
+
`notified`; the task is what stays behind until somebody answers.
|
|
375
|
+
|
|
376
|
+
- `mail_task`: an arrival (`mail` with no `deliver`) whose `mail.target` is a
|
|
377
|
+
project, or an address filed under one (`email{address}` + `filed{project}`,
|
|
378
|
+
as hello@yaks.app is filed under the yaks.app project), files `task{}` +
|
|
379
|
+
`doc{title, body}` + `filed{project, priority: 1}` and an `about` edge from
|
|
380
|
+
the task to the letter. The task's eid derives from the letter (`taskEid` in
|
|
381
|
+
./work.ts), so a run that is repeated writes nothing new.
|
|
382
|
+
- `mail_answered`: once a letter answering it is `delivered`, the task is
|
|
383
|
+
completed. A letter we sent answers one when its `mail.reply_to` names it (a
|
|
384
|
+
reply) or its `mail.target` does (one sent `about` it); `answering` in
|
|
385
|
+
@yaks/mail/answers is that one definition, and `yak admin errors` lists
|
|
386
|
+
unanswered letters by it too.
|
|
387
|
+
- `mail_dismissed`: a letter that needs no answer has its task cancelled by
|
|
388
|
+
whoever reads it, and cancelling archives the letter (`archived{at, by, via}`,
|
|
389
|
+
copied from `cancelled`), so `yak admin errors` stops listing it as
|
|
390
|
+
unanswered.
|
|
391
|
+
|
|
392
|
+
All three are declared with `"option": "local"`, so a graph whose mail a person
|
|
393
|
+
reads in a mail client files nothing, and a graph without `task` or
|
|
394
|
+
`filed.project` does nothing.
|
|
395
|
+
|
|
396
|
+
## Pulling from an edge
|
|
397
|
+
|
|
398
|
+
A graph behind a perimeter cannot be posted to. Its edge keeps what arrived
|
|
399
|
+
instead, and `@yaks/mail/service` pulls it: a duty a host runs under the
|
|
400
|
+
`@yaks/mail` lease, so one process over the graph pulls at a time. Name the edge
|
|
401
|
+
under `pull`:
|
|
402
|
+
|
|
403
|
+
```json
|
|
404
|
+
{
|
|
405
|
+
"use": "@yaks/mail",
|
|
406
|
+
"with": {
|
|
407
|
+
"domain": "books.example",
|
|
408
|
+
"pull": {
|
|
409
|
+
"url": "https://edge.books.example",
|
|
410
|
+
"token": { "secret": "EDGE_TOKEN" },
|
|
411
|
+
"every": 10000
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
```
|
|
416
|
+
|
|
417
|
+
The edge holds two trays. `GET /messages?unnotified=1&.dir=in&limit=100` lists
|
|
418
|
+
the letters nobody has taken; each is recorded through `arrived()`, and
|
|
419
|
+
`POST /messages/notified {ids}` tells the edge they are taken.
|
|
420
|
+
`GET /requests?unprocessed=1&limit=100` lists the requests posted to its hook
|
|
421
|
+
paths (a 404 means there is no such tray); where the graph declares
|
|
422
|
+
[@yaks/hook](../hook/README.md), each becomes a `hook` about the mailbox its
|
|
423
|
+
first path segment names (`/hook/books/…` is whoever has `books@<domain>`), and
|
|
424
|
+
`POST /requests/processed {ids}` acknowledges them.
|
|
425
|
+
|
|
426
|
+
Taking is first come, first served at the edge: two graphs must never name the
|
|
427
|
+
same edge, and a copy of a config that pulls takes the mail from the graph it
|
|
428
|
+
was copied from. Every record is idempotent — a letter by its Message-ID, a
|
|
429
|
+
request by the id derived from it — so a crash between recording and
|
|
430
|
+
acknowledging only repeats the pull. An item that cannot be recorded stays at
|
|
431
|
+
the edge and is tried again.
|
|
432
|
+
|
|
433
|
+
The pull takes the oldest letters first. An edge that does not answer is tried
|
|
434
|
+
again on the next pass. The service logs transient connection failures,
|
|
435
|
+
timeouts, HTTP 408, 429 and 5xx responses, and logs recovery when the tray
|
|
436
|
+
answers. A letter that waited longer than `LATE` (a minute) reports to telemetry
|
|
437
|
+
when recorded, with its Message-ID, wait and the outage cause, unless it arrived
|
|
438
|
+
after the previous pass began: then the wait was the process's, stopped or
|
|
439
|
+
frozen between passes, and its own stall report says so. An acknowledgement
|
|
440
|
+
retry does not repeat that report. Authentication, parsing and recording
|
|
441
|
+
failures report immediately. A one-pass host, which has no next pass, reports a
|
|
442
|
+
tray it cannot take at once.
|
|
443
|
+
|
|
444
|
+
## The tools
|
|
445
|
+
|
|
446
|
+
`@yaks/mail/tools` exports `runs()`. A server loading these implementations and
|
|
447
|
+
the vocabulary exposes the following CLI commands and corresponding MCP names
|
|
448
|
+
with underscores, such as `mail_send`:
|
|
449
|
+
|
|
450
|
+
| Tool | Behavior |
|
|
451
|
+
| ----------------- | ------------------------------------------------------------------------------------------- |
|
|
452
|
+
| `mail show <id>` | Show a message and its thread; mark the caller's subscription to the thread `opened`. |
|
|
453
|
+
| `mail reply <id>` | Create a threaded reply; mark the caller's subscription to a received thread `archived`. |
|
|
454
|
+
| `mail send` | Create a message with `deliver`, creating an email entity for an unknown recipient address. |
|
|
455
|
+
| `mail check` | Find received messages with no sender and report unusable sender configuration. |
|
|
456
|
+
|
|
457
|
+
```sh
|
|
458
|
+
yak mail show E-12
|
|
459
|
+
yak mail reply E-12 --body @answer.md
|
|
460
|
+
yak mail send ana@books.example 'Thursday' --body 'We meet at seven.'
|
|
461
|
+
```
|
|
462
|
+
|
|
463
|
+
A thread is a message and the messages that answer it, through `reply_to`. `●`
|
|
464
|
+
means unread, `·` read, and `×` archived. Each mark is removed and added again
|
|
465
|
+
in one batch, so a repeated read or answer gets a fresh time. The `opened` and
|
|
466
|
+
`archived` components belong to [@yaks/kernel](../kernel/README.md) and are
|
|
467
|
+
stored on the caller's [subscription](../notify/README.md), keeping each
|
|
468
|
+
reader's attention apart from the message's own state. Compose the kernel and
|
|
469
|
+
notify vocabularies: graph admission refuses a component the graph does not
|
|
470
|
+
declare. Listing what waits for a reader is [@yaks/inbox](../inbox/README.md)'s.
|
|
471
|
+
|
|
472
|
+
Replies use the original recipient address as sender for received messages, or
|
|
473
|
+
the original sender address when following up on an outgoing message. The reply
|
|
474
|
+
subject has one `Re:` prefix. Sending defaults `from` to the `email.address` of
|
|
475
|
+
the entity the letter is `about`, such as a project's own, so the answer is
|
|
476
|
+
routed back to it; where that entity has none, to the caller's. Both tools
|
|
477
|
+
return bundles for the tool runner to apply; the configured sending effect
|
|
478
|
+
performs delivery. With `sender: { "via": "stash" }`, the same flow records
|
|
479
|
+
outgoing messages in memory.
|
|
480
|
+
|
|
481
|
+
## Membership invitations
|
|
482
|
+
|
|
483
|
+
Register `invited()` as a `created('member')` handler to create a welcome
|
|
484
|
+
message when [@yaks/member](../member/README.md) adds a membership:
|
|
485
|
+
|
|
486
|
+
```ts
|
|
487
|
+
import { effects } from '@yaks/effects'
|
|
488
|
+
import { graph } from '@yaks/graph'
|
|
489
|
+
import { ram } from '@yaks/ram'
|
|
490
|
+
import { loadVocab } from '@yaks/vocab'
|
|
491
|
+
import { docDoc, docs } from '@yaks/doc'
|
|
492
|
+
import { memberDoc } from '@yaks/member'
|
|
493
|
+
import { invited, mailbox, mailDoc, stash } from '@yaks/mail'
|
|
494
|
+
|
|
495
|
+
let vocab = loadVocab([docDoc, mailDoc, memberDoc])
|
|
496
|
+
let fx = effects(vocab, { write: (b) => g.apply(b, { trusted: true }) })
|
|
497
|
+
let g = graph({
|
|
498
|
+
storage: ram(vocab),
|
|
499
|
+
vocab,
|
|
500
|
+
plugins: [
|
|
501
|
+
fx,
|
|
502
|
+
docs(),
|
|
503
|
+
mailbox({ domain: 'books.example', sender: stash(), effects: fx }),
|
|
504
|
+
],
|
|
505
|
+
})
|
|
506
|
+
|
|
507
|
+
fx.created(
|
|
508
|
+
'member',
|
|
509
|
+
invited({
|
|
510
|
+
apply: (bundles) => g.apply(bundles),
|
|
511
|
+
welcome: ({ role }) => ({
|
|
512
|
+
from: 'hello@books.example',
|
|
513
|
+
subject: role == 'owner'
|
|
514
|
+
? 'You run the book club'
|
|
515
|
+
: 'You are in the book club',
|
|
516
|
+
body: 'Welcome. We meet Thursdays.',
|
|
517
|
+
}),
|
|
518
|
+
}),
|
|
519
|
+
)
|
|
520
|
+
```
|
|
521
|
+
|
|
522
|
+
`welcome` can return `null` to skip an invitation. The handler creates a message
|
|
523
|
+
about the membership entity with `deliver.to` set to the member's person id. The
|
|
524
|
+
normal sending effect handles it, including recording `bounced` if that entity
|
|
525
|
+
has no email address.
|
|
526
|
+
|
|
527
|
+
## Addresses
|
|
528
|
+
|
|
529
|
+
`canon(domain)` lowercases addresses at the configured domain and removes
|
|
530
|
+
underscores from their local part. Other domains are unchanged:
|
|
531
|
+
|
|
532
|
+
```ts
|
|
533
|
+
import { canon } from '@yaks/mail'
|
|
534
|
+
|
|
535
|
+
let mine = canon('books.example')
|
|
536
|
+
mine('Book_Club@Books.Example') // 'bookclub@books.example'
|
|
537
|
+
mine('ana@elsewhere.com') // 'ana@elsewhere.com'
|
|
538
|
+
```
|
|
539
|
+
|
|
540
|
+
The underscore rule is this package's Cloudflare-routing compatibility policy.
|
|
541
|
+
`mailbox({ domain })` normalizes `email.address`, `mail.from`, and `mail.to`
|
|
542
|
+
before storage. Normalization does not impose uniqueness on email entities.
|
|
543
|
+
|
|
544
|
+
## Bodies
|
|
545
|
+
|
|
546
|
+
`message()` renders a Markdown body as plain text and HTML. The small renderer
|
|
547
|
+
supports paragraphs, headings, bullets, pipe tables, an image on a line of its
|
|
548
|
+
own (``), links, bold, italic, and inline code. It escapes
|
|
549
|
+
input HTML and emits links only for absolute `http`, `https`, `mailto`, and
|
|
550
|
+
`tel` URLs. Relative links remain text. To use another renderer, construct a
|
|
551
|
+
`Message` with your own `html` and pass it to a sender directly.
|
|
552
|
+
|
|
553
|
+
## Exports
|
|
554
|
+
|
|
555
|
+
The root import `@yaks/mail` provides:
|
|
556
|
+
|
|
557
|
+
| Exports | Purpose |
|
|
558
|
+
| ---------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------- |
|
|
559
|
+
| `mailDoc`, `MAIL`, `EMAIL`, `DELIVER`, `DELIVERED`, `BOUNCED`, `NOTIFIED` | Vocabulary and component names. |
|
|
560
|
+
| `mailbox`, `Mailbox` | Graph plugin and its options. |
|
|
561
|
+
| `sending`, `message`, `addressOf`, `Post`, `Sender`, `Message`, `Receipt`, `Unsent`, `Undeliverable` | Sending handler, message composition, address lookup, and transport types. |
|
|
562
|
+
| `post`, `receipted` | Sender selection by name, and a provider's delivery receipt. |
|
|
563
|
+
| `stash`, `Stash`, `Kept` | In-memory transport. |
|
|
564
|
+
| `inbound`, `author`, `messageId`, `verdict`, `Received`, `Arrival`, `Head` | Incoming-message conversion and header interpretation. |
|
|
565
|
+
| `arrived`, `wearer`, `named`, `routed`, `known`, `Arrivals`, `Book` | Graph lookups used when receiving. |
|
|
566
|
+
| `Reading`, `Read` | What another package makes of an arrival. |
|
|
567
|
+
| `pull`, `edge`, `received`, `hookTo`, `messageIdOf`, `Edge`, `Pulled`, `Down`, `Unavailable`, `LATE` | Pulling arrivals from an edge. |
|
|
568
|
+
| `invited`, `Invite`, `Welcome`, `Seat` | Membership invitation handler and types. |
|
|
569
|
+
| `canon`, `local`, `at`, `parts`, `address` | Address parsing and normalization. |
|
|
570
|
+
| `html`, `text`, `tokens`, `linkable`, `escape`, `Token` | Body rendering. |
|
|
571
|
+
| `Options`, `Transport`, `Door`, `Pull` | Plugin configuration types. |
|
|
572
|
+
|
|
573
|
+
Separate entry points provide `mailDoc` and `docs` from `@yaks/mail/vocab`,
|
|
574
|
+
`plugins()` from `@yaks/mail/graph`, `effects()` and `post()` from
|
|
575
|
+
`@yaks/mail/effects`, `routes()`, `PATH`, and `Posted` from `@yaks/mail/routes`,
|
|
576
|
+
`service()` and `EVERY` from `@yaks/mail/service`, `runs()` plus thread helpers
|
|
577
|
+
from `@yaks/mail/tools`, and from `@yaks/mail/answers` who a letter that arrived
|
|
578
|
+
is from (`sender`, `automated`) and the first delivered reply in its thread
|
|
579
|
+
(`answers`), which `yak admin errors` times. They are not root re-exports. Their
|
|
580
|
+
`host` argument is the process that opened the graph, represented by the
|
|
581
|
+
services each function needs; for example, `routes()` needs `{ graph }`, and
|
|
582
|
+
`{ graph, config }` to ask the configured packages what an arrival means.
|
|
583
|
+
|
|
584
|
+
## What is deliberately not here
|
|
585
|
+
|
|
586
|
+
The package does not parse MIME, speak IMAP or POP, schedule retries, or manage
|
|
587
|
+
credentials. Applications supply those services. A failed message remains
|
|
588
|
+
queryable, and a retry is a new message. `deliver.to` is an entity id; the
|
|
589
|
+
recipient's address is resolved when sending.
|
|
590
|
+
|
|
591
|
+
## Integration
|
|
592
|
+
|
|
593
|
+
Use [@yaks/doc](../doc/README.md) for message text,
|
|
594
|
+
[@yaks/effects](../effects/README.md) for sending and invitations, and
|
|
595
|
+
[@yaks/member](../member/README.md) when invitations should follow membership
|
|
596
|
+
creation. The graph's storage adapter and optional journal retain the data.
|
|
597
|
+
|
|
598
|
+
## Compatibility
|
|
599
|
+
|
|
600
|
+
The core uses standard JavaScript and Web APIs and runs on Deno, Node, browsers,
|
|
601
|
+
and Cloudflare Workers. Its inbound message types are checked against
|
|
602
|
+
`@cloudflare/workers-types` in `conform.ts`.
|
|
603
|
+
|
|
604
|
+
## What else a letter means
|
|
605
|
+
|
|
606
|
+
Mail records a letter: its envelope, its words, whom it is for and which letter
|
|
607
|
+
it answers. Whether it also starts a conversation, answers a question or
|
|
608
|
+
comments on a task is another package's to say, and only at the door, where the
|
|
609
|
+
whole message (its headers included) is still in hand.
|
|
610
|
+
|
|
611
|
+
A package says it through a `./mail` facet, the way an application offers
|
|
612
|
+
@yaks/web its page through `./web`: `reading(options)` takes that package's own
|
|
613
|
+
options from the config and returns a `Reading`, or nothing. Both receiving
|
|
614
|
+
doors, `POST /mail/inbound` and the pull service, ask every configured package
|
|
615
|
+
for its reading at their first letter, and `arrived()` asks each reading in
|
|
616
|
+
config order. The first that answers says what is recorded in place of the
|
|
617
|
+
letter alone; a config that names none records letters and nothing else.
|
|
618
|
+
|
|
619
|
+
A reading is handed the message, what the caller knew beside it (the parsed
|
|
620
|
+
text, a target it named), the letter as mail records it with its `$actor`, the
|
|
621
|
+
letter it answers where this graph has one, its author where the address book
|
|
622
|
+
knows them, and the address canonicalizer. It returns bundles to apply, or
|
|
623
|
+
`undefined` to leave the letter as it is:
|
|
624
|
+
|
|
625
|
+
```ts
|
|
626
|
+
import { graph } from '@yaks/graph'
|
|
627
|
+
import { ram } from '@yaks/ram'
|
|
628
|
+
import { loadVocab } from '@yaks/vocab'
|
|
629
|
+
import { docDoc } from '@yaks/doc'
|
|
630
|
+
import { arrived, mailDoc, type Reading } from '@yaks/mail'
|
|
631
|
+
import { equal } from '@yaks/testing'
|
|
632
|
+
|
|
633
|
+
let vocab = loadVocab([docDoc, mailDoc])
|
|
634
|
+
let g = graph({ storage: ram(vocab), vocab })
|
|
635
|
+
let urgent: Reading = ({ letter, message }) =>
|
|
636
|
+
message.headers.get('importance') == 'high'
|
|
637
|
+
? [{ ...letter, doc: { title: 'URGENT', body: 'Read me first.' } }]
|
|
638
|
+
: undefined
|
|
639
|
+
let receive = arrived({ graph: g, readings: [urgent] })
|
|
640
|
+
let [letter] = await receive({
|
|
641
|
+
from: 'ana@books.example',
|
|
642
|
+
to: 'hello@books.example',
|
|
643
|
+
headers: new Headers({ Importance: 'high', Subject: 'Thursday' }),
|
|
644
|
+
})
|
|
645
|
+
equal(letter.doc, { title: 'URGENT', body: 'Read me first.' })
|
|
646
|
+
```
|
|
647
|
+
|
|
648
|
+
[@yaks/inbox](../inbox/README.md)'s email door is one: with the inbox
|
|
649
|
+
configured, a verified letter from its person to its address starts a
|
|
650
|
+
conversation, and a verified reply to one of its letters answers in that thread.
|
|
651
|
+
|
|
652
|
+
## Original deliveries
|
|
653
|
+
|
|
654
|
+
`capture(incoming)` produces a JSON-safe copy of an inbound delivery, including
|
|
655
|
+
its envelope, text, HTML, attachment bytes and original `sources` supplied by
|
|
656
|
+
the provider. `captured(capture.sources)` and `captured(capture.attachments)`
|
|
657
|
+
read those original files back. `capturing(incoming, file)` instead maps each
|
|
658
|
+
original file to the reference the caller supplies, for keeping large originals
|
|
659
|
+
in separate byte objects. Keep those bytes and their manifest before
|
|
660
|
+
acknowledging intake: an app's parsed letter and accepted uploads are
|
|
661
|
+
projections of it and may not include everything the sender supplied.
|
|
662
|
+
|
|
663
|
+
```ts
|
|
664
|
+
import { capture, captured } from '@yaks/mail'
|
|
665
|
+
import { equal } from '@yaks/testing'
|
|
666
|
+
|
|
667
|
+
let original = new TextEncoder().encode('From: a@example.com\r\n\r\nHello')
|
|
668
|
+
let saved = JSON.stringify(capture({
|
|
669
|
+
from: 'a@example.com',
|
|
670
|
+
to: 'b@example.com',
|
|
671
|
+
headers: new Headers(),
|
|
672
|
+
text: 'Hello',
|
|
673
|
+
attachments: [],
|
|
674
|
+
sources: [{ name: 'message.eml', type: 'message/rfc822', bytes: original }],
|
|
675
|
+
}))
|
|
676
|
+
equal(captured(JSON.parse(saved).sources)[0].bytes, original)
|
|
677
|
+
```
|