@open-workspace/email 0.4.1 → 0.5.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/README.md +9 -4
- package/alchemy/index.ts +130 -2
- package/drizzle/mailbox/0002_good_komodo.sql +2 -0
- package/drizzle/mailbox/0003_after_0_4_2.sql +180 -0
- package/drizzle/mailbox/meta/0002_snapshot.json +1017 -0
- package/drizzle/mailbox/meta/0003_snapshot.json +1507 -0
- package/drizzle/mailbox/meta/_journal.json +14 -0
- package/drizzle/mailbox/migrations.js +5 -1
- package/migrations/0001_overrated_ricochet.sql +2 -0
- package/migrations/0002_after_0_4_2.sql +35 -0
- package/migrations/meta/0001_snapshot.json +564 -0
- package/migrations/meta/0002_snapshot.json +807 -0
- package/migrations/meta/_journal.json +14 -0
- package/package.json +23 -7
- package/shared/attachment-preview.ts +18 -8
- package/shared/folder-order.ts +31 -6
- package/shared/mail-headers.ts +3 -3
- package/shared/mail-html.ts +25 -0
- package/shared/mail-import.ts +0 -7
- package/shared/mail-received.ts +4 -4
- package/shared/mail-rule-actions.ts +47 -0
- package/shared/mail.ts +70 -4
- package/shared/office-preview.ts +0 -4
- package/shared/pdfjs-assets.ts +1 -3
- package/shared/rich-content.ts +35 -0
- package/shared/signature.ts +15 -0
- package/src/admin/address-auto-reply.tsx +49 -0
- package/src/admin/blocked-senders.tsx +86 -66
- package/src/admin/forward-destinations.tsx +140 -0
- package/src/admin/mail-archive.tsx +134 -0
- package/src/admin/team-signature.tsx +90 -0
- package/src/client.tsx +130 -16
- package/src/mail/attachment-preview-dialog.tsx +13 -5
- package/src/mail/auto-reply-banner.tsx +61 -0
- package/src/mail/blocked-senders.tsx +124 -91
- package/src/mail/compose-window-chrome.tsx +20 -8
- package/src/mail/compose-window.tsx +387 -365
- package/src/mail/composer-asset-bookmarks.ts +47 -12
- package/src/mail/composer-attachment-list.tsx +42 -17
- package/src/mail/composer-editor-document.ts +54 -10
- package/src/mail/composer-editor.tsx +44 -44
- package/src/mail/composer-link-text.ts +42 -0
- package/src/mail/composer-recipient-fields.tsx +25 -16
- package/src/mail/composer-recipients.ts +62 -16
- package/src/mail/composer-session.ts +107 -23
- package/src/mail/composer-toolbar.tsx +15 -1
- package/src/mail/composer-upload-client.ts +29 -1
- package/src/mail/composer-upload-progress.ts +2 -2
- package/src/mail/conversation-actions-model.ts +56 -0
- package/src/mail/conversation-list.tsx +263 -128
- package/src/mail/conversation-loading-skeleton.tsx +165 -90
- package/src/mail/conversation-message.tsx +257 -102
- package/src/mail/conversation-order.ts +12 -0
- package/src/mail/conversation-reply.tsx +313 -189
- package/src/mail/conversation-selection-toolbar.tsx +188 -143
- package/src/mail/conversation-view.tsx +320 -85
- package/src/mail/delivery-indicator.tsx +33 -12
- package/src/mail/draft-attachments.ts +194 -0
- package/src/mail/email-html-body.tsx +97 -53
- package/src/mail/folder-cleanup-bar.tsx +193 -0
- package/src/mail/folder-glyph.tsx +5 -0
- package/src/mail/folder-manager.tsx +71 -38
- package/src/mail/mail-app-bar.tsx +15 -3
- package/src/mail/mail-dock.tsx +19 -3
- package/src/mail/mail-focus.ts +48 -0
- package/src/mail/mail-folders.ts +14 -2
- package/src/mail/mail-layout.css +20 -0
- package/src/mail/mail-mailbox-sheet.tsx +22 -69
- package/src/mail/mail-nav-rail.tsx +7 -4
- package/src/mail/mail-nav.tsx +66 -46
- package/src/mail/mail-prefetch.ts +30 -9
- package/src/mail/mail-primitives.tsx +6 -4
- package/src/mail/mail-queries.ts +122 -20
- package/src/mail/mail-query-cache.ts +1085 -173
- package/src/mail/mail-search-params.ts +35 -0
- package/src/mail/mail-shell.tsx +32 -9
- package/src/mail/mailbox-ai-settings.tsx +56 -42
- package/src/mail/mailbox-realtime.tsx +4 -12
- package/src/mail/mailbox-rules.tsx +898 -0
- package/src/mail/message-actions.tsx +38 -26
- package/src/mail/message-attachments.tsx +20 -12
- package/src/mail/message-body.ts +53 -0
- package/src/mail/message-format.ts +8 -2
- package/src/mail/message-header.tsx +3 -1
- package/src/mail/message-translation.tsx +34 -67
- package/src/mail/original-message-sheet.tsx +63 -50
- package/src/mail/original-message.ts +6 -6
- package/src/mail/pane-splitter.tsx +3 -3
- package/src/mail/pdf-preview.tsx +70 -62
- package/src/mail/plain-message-text.tsx +28 -0
- package/src/mail/print-message.ts +31 -36
- package/src/mail/reader-pane.tsx +134 -102
- package/src/mail/recipient-combobox.tsx +52 -29
- package/src/mail/recipient-field-row.tsx +3 -0
- package/src/mail/rich-content.ts +128 -0
- package/src/mail/sanitize-email-html.ts +57 -33
- package/src/mail/sender-actions.tsx +49 -39
- package/src/mail/sender-authentication.tsx +1 -1
- package/src/mail/sender-dialogs.tsx +67 -43
- package/src/mail/sender-security.tsx +9 -10
- package/src/mail/signature.ts +43 -0
- package/src/mail/translation-html.ts +3 -3
- package/src/mail/types.ts +12 -0
- package/src/mail/use-composer-attachment-preflight.ts +23 -60
- package/src/mail/use-composer-core.ts +286 -0
- package/src/mail/use-composer-navigation.ts +120 -0
- package/src/mail/use-composer-submission.ts +154 -19
- package/src/mail/use-conversation-actions.ts +292 -0
- package/src/mail/use-conversation-position.ts +51 -0
- package/src/mail/use-conversation-selection.ts +192 -89
- package/src/mail/use-draft-autosave.ts +394 -0
- package/src/mail/use-drafts.ts +194 -0
- package/src/mail/use-mail-controller.ts +754 -213
- package/src/mail/use-mail-data.ts +210 -100
- package/src/mail/use-mail-safety.ts +19 -38
- package/src/mail/use-mail-send.ts +18 -6
- package/src/mail/use-pdf-translation.ts +1 -1
- package/src/mail/use-reply-cc.ts +61 -0
- package/src/mail/use-visible-message-read.ts +128 -64
- package/src/pwa/push-key.ts +18 -0
- package/src/pwa/push-subscription.ts +88 -26
- package/src/settings/auto-reply-editor.tsx +454 -0
- package/src/settings/auto-reply-page.tsx +59 -0
- package/src/settings/mail-apps-page.tsx +52 -43
- package/src/settings/notifications-page.tsx +82 -67
- package/src/settings/rich-content-editor.tsx +182 -0
- package/src/settings/settings-queries.ts +57 -0
- package/src/settings/signature-editor.tsx +142 -0
- package/src/settings/signature-page.tsx +70 -0
- package/worker/admin/blocked-sender-routes.ts +10 -16
- package/worker/backups/eml.ts +185 -0
- package/worker/backups/file-parts.ts +140 -0
- package/worker/backups/mail-backup.ts +644 -0
- package/worker/backups/settings-backup.ts +866 -0
- package/worker/backups/stream-eml.ts +73 -0
- package/worker/db/schema.ts +98 -2
- package/worker/jmap/attachment-blobs.ts +113 -0
- package/worker/jmap/blobs.ts +162 -0
- package/worker/jmap/draft-content.ts +42 -0
- package/worker/jmap/drafts.ts +70 -0
- package/worker/jmap/email-body.ts +152 -0
- package/worker/jmap/email-methods.ts +1223 -0
- package/worker/jmap/eventsource.ts +9 -8
- package/worker/jmap/import-methods.ts +205 -0
- package/worker/jmap/mailbox-methods.ts +187 -0
- package/worker/jmap/method-core.ts +379 -0
- package/worker/jmap/methods.ts +108 -1815
- package/worker/jmap/push-subscription-methods.ts +373 -0
- package/worker/jmap/push-types.ts +46 -2
- package/worker/jmap/push.ts +24 -31
- package/worker/jmap/routes.ts +144 -153
- package/worker/jmap/submission-methods.ts +395 -0
- package/worker/mail/access.ts +35 -12
- package/worker/mail/ai-classification.ts +2 -2
- package/worker/mail/archive-object.ts +96 -0
- package/worker/mail/archive-routes.ts +35 -0
- package/worker/mail/archive.ts +682 -0
- package/worker/mail/auth-headers.ts +48 -40
- package/worker/mail/auto-reply-routes.ts +255 -0
- package/worker/mail/auto-reply.ts +243 -0
- package/worker/mail/bimi.ts +53 -36
- package/worker/mail/bootstrap.ts +11 -3
- package/worker/mail/capabilities.ts +69 -0
- package/worker/mail/classification-signals.ts +1 -1
- package/worker/mail/confusables-data.ts +9 -0
- package/worker/mail/conversation-cursor.ts +6 -15
- package/worker/mail/delivery-events.ts +9 -7
- package/worker/mail/delivery-facts.ts +1 -1
- package/worker/mail/demo-routes.ts +3 -3
- package/worker/mail/doh.ts +3 -3
- package/worker/mail/download-links.ts +153 -0
- package/worker/mail/downloads.ts +2 -6
- package/worker/mail/draft-routes.ts +473 -0
- package/worker/mail/draft-sender.ts +54 -0
- package/worker/mail/edge-cache.ts +41 -55
- package/worker/mail/email-routing.ts +123 -0
- package/worker/mail/forward-destinations.ts +114 -0
- package/worker/mail/forward-downloads.ts +115 -0
- package/worker/mail/forward-html.ts +85 -0
- package/worker/mail/header-metadata.ts +2 -5
- package/worker/mail/impersonation.ts +102 -101
- package/worker/mail/import-routes.ts +29 -82
- package/worker/mail/import.ts +130 -0
- package/worker/mail/inbound-delivery.ts +6 -5
- package/worker/mail/inbound.ts +58 -28
- package/worker/mail/ip-intel.ts +8 -17
- package/worker/mail/isolate-cache.ts +1 -1
- package/worker/mail/mailbox-deletion.ts +33 -12
- package/worker/mail/mailbox-directory.ts +39 -65
- package/worker/mail/mailing-list.ts +29 -58
- package/worker/mail/message-security.ts +3 -3
- package/worker/mail/message-view.ts +12 -10
- package/worker/mail/office-conversion.ts +3 -5
- package/worker/mail/office-preview-container.ts +3 -8
- package/worker/mail/outbound-content.ts +139 -65
- package/worker/mail/outbound-delivery.ts +20 -17
- package/worker/mail/outbound-service.ts +34 -9
- package/worker/mail/outbound.ts +146 -75
- package/worker/mail/outgoing-context.ts +60 -30
- package/worker/mail/participants.ts +10 -15
- package/worker/mail/plain-body.ts +39 -0
- package/worker/mail/push-endpoint.ts +17 -0
- package/worker/mail/push-notifications.ts +87 -43
- package/worker/mail/quote-parser.ts +107 -25
- package/worker/mail/r2-presigned-urls.ts +7 -0
- package/worker/mail/receive.ts +146 -39
- package/worker/mail/recipients.ts +1 -1
- package/worker/mail/remote-proxy.ts +3 -3
- package/worker/mail/repeat.ts +1 -1
- package/worker/mail/reply-plan.ts +124 -26
- package/worker/mail/rfc.ts +15 -7
- package/worker/mail/rich-content.ts +207 -0
- package/worker/mail/routes.ts +696 -304
- package/worker/mail/routing.ts +274 -0
- package/worker/mail/rules.ts +230 -0
- package/worker/mail/safety-routes.ts +88 -80
- package/worker/mail/schemas.ts +222 -31
- package/worker/mail/search.ts +25 -2
- package/worker/mail/security-settings.ts +4 -10
- package/worker/mail/send-message.ts +238 -0
- package/worker/mail/sender-intel.ts +12 -20
- package/worker/mail/signature-routes.ts +201 -0
- package/worker/mail/signatures.ts +195 -0
- package/worker/mail/spamhaus.ts +28 -16
- package/worker/mail/storage-keys.ts +35 -0
- package/worker/mail/unsubscribe.ts +19 -41
- package/worker/mail/uploads.ts +193 -80
- package/worker/mail/webhook-source.ts +3 -2
- package/worker/mail/workspace-safety.ts +194 -60
- package/worker/mailbox/demo.ts +13 -15
- package/worker/mailbox/email-index.ts +92 -13
- package/worker/mailbox/folder-model.ts +0 -20
- package/worker/mailbox/import-placement.ts +21 -36
- package/worker/mailbox/inbound-decision.ts +114 -0
- package/worker/mailbox/inbox-counts.ts +45 -25
- package/worker/mailbox/index.ts +843 -2888
- package/worker/mailbox/jmap-change-log.ts +195 -0
- package/worker/mailbox/jmap-email-rows.ts +186 -0
- package/worker/mailbox/jmap-mailboxes.ts +116 -0
- package/worker/mailbox/jmap-placement.ts +139 -0
- package/worker/mailbox/jmap-query-filter.ts +231 -0
- package/worker/mailbox/jmap-store.ts +167 -299
- package/worker/mailbox/mailbox-auto-reply.ts +301 -0
- package/worker/mailbox/mailbox-backup.ts +538 -0
- package/worker/mailbox/mailbox-drafts.ts +417 -0
- package/worker/mailbox/mailbox-folders.ts +242 -0
- package/worker/mailbox/mailbox-import.ts +450 -0
- package/worker/mailbox/mailbox-inbound.ts +934 -0
- package/worker/mailbox/mailbox-jmap.ts +294 -0
- package/worker/mailbox/mailbox-outbound.ts +413 -0
- package/worker/mailbox/mailbox-rules.ts +336 -0
- package/worker/mailbox/mailbox-senders.ts +294 -0
- package/worker/mailbox/mailbox-storage.ts +438 -0
- package/worker/mailbox/mailbox-views.ts +891 -0
- package/worker/mailbox/model.ts +1 -0
- package/worker/mailbox/office-preview-jobs.ts +59 -47
- package/worker/mailbox/placement.ts +933 -0
- package/worker/mailbox/restore-thread.ts +52 -0
- package/worker/mailbox/retention.ts +36 -0
- package/worker/mailbox/schema.ts +211 -1
- package/worker/mailbox/senders.ts +216 -178
- package/worker/mailbox/spam-undo-filing.ts +138 -0
- package/worker/mailbox/spam-undo.ts +240 -0
- package/worker/mailbox/support.ts +416 -0
- package/worker/mcp/mail-action-tools.ts +156 -0
- package/worker/mcp/mail-compose-tools.ts +8 -2
- package/worker/mcp/mail-safety-tools.ts +4 -4
- package/worker/module.ts +23 -2
- package/src/mail/use-compose-submission.ts +0 -208
- package/src/mail/use-reply-submission.ts +0 -114
- package/worker/mail/capped-body.ts +0 -36
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
/** The mailbox's auto-reply: its settings, and answering incoming mail. */
|
|
2
|
+
import {
|
|
3
|
+
createDb,
|
|
4
|
+
mailboxAddressSql,
|
|
5
|
+
mailDomainAllowed,
|
|
6
|
+
normalizeMailboxAddress,
|
|
7
|
+
} from "@open-workspace/core/module";
|
|
8
|
+
import { mailboxes } from "@open-workspace/core/schema";
|
|
9
|
+
import { and, eq, gt, inArray, lt, sql } from "drizzle-orm";
|
|
10
|
+
|
|
11
|
+
import { sendArchived } from "../mail/archive";
|
|
12
|
+
import {
|
|
13
|
+
AUTO_REPLY_HOURLY_CAP,
|
|
14
|
+
autoReplyContentError,
|
|
15
|
+
autoReplyImagePrefix,
|
|
16
|
+
autoReplyRecipient,
|
|
17
|
+
autoReplyStatus,
|
|
18
|
+
buildAutoReply,
|
|
19
|
+
messageSkipReason,
|
|
20
|
+
parseHeaderBlock,
|
|
21
|
+
} from "../mail/auto-reply";
|
|
22
|
+
import { relaxedAligned } from "../mail/org-domain";
|
|
23
|
+
import { replyAlias } from "../mail/reply-plan";
|
|
24
|
+
import { inlineImages } from "../mail/rich-content";
|
|
25
|
+
import {
|
|
26
|
+
autoReplyLog,
|
|
27
|
+
autoReplySettings,
|
|
28
|
+
conversations,
|
|
29
|
+
senders,
|
|
30
|
+
} from "./schema";
|
|
31
|
+
import type { AutoReplySettings, Email, PendingInbound } from "./schema";
|
|
32
|
+
import type { AutoReplyInput, MailboxContext } from "./support";
|
|
33
|
+
import { DAY_MS } from "./support";
|
|
34
|
+
|
|
35
|
+
export function getAutoReply(m: MailboxContext) {
|
|
36
|
+
return m.db.select().from(autoReplySettings).all().at(0) ?? null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function setAutoReply(
|
|
40
|
+
m: MailboxContext,
|
|
41
|
+
input: AutoReplyInput,
|
|
42
|
+
userId: string
|
|
43
|
+
) {
|
|
44
|
+
const previous = getAutoReply(m);
|
|
45
|
+
|
|
46
|
+
// Resolve an untouched switch at write time: the Inbox banner can turn it off.
|
|
47
|
+
const enabled = input.enabled ?? previous?.enabled ?? false;
|
|
48
|
+
|
|
49
|
+
const error = autoReplyContentError(enabled, input);
|
|
50
|
+
|
|
51
|
+
if (error) return { error };
|
|
52
|
+
|
|
53
|
+
const settings: AutoReplySettings = {
|
|
54
|
+
...input,
|
|
55
|
+
enabled,
|
|
56
|
+
id: 1,
|
|
57
|
+
updatedBy: userId,
|
|
58
|
+
updatedAt: new Date(),
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// Each absence starts fresh: switching on or moving the window forgets
|
|
62
|
+
// who has been answered.
|
|
63
|
+
const newAbsence =
|
|
64
|
+
enabled &&
|
|
65
|
+
(!previous?.enabled ||
|
|
66
|
+
previous.startsAt?.getTime() !== input.startsAt?.getTime() ||
|
|
67
|
+
previous.endsAt?.getTime() !== input.endsAt?.getTime());
|
|
68
|
+
|
|
69
|
+
m.db.transaction((tx) => {
|
|
70
|
+
tx.insert(autoReplySettings)
|
|
71
|
+
.values(settings)
|
|
72
|
+
.onConflictDoUpdate({ target: autoReplySettings.id, set: settings })
|
|
73
|
+
.run();
|
|
74
|
+
|
|
75
|
+
if (newAbsence) tx.delete(autoReplyLog).run();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
return settings;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function turnOffAutoReply(m: MailboxContext, userId: string) {
|
|
82
|
+
m.db
|
|
83
|
+
.update(autoReplySettings)
|
|
84
|
+
.set({ enabled: false, updatedBy: userId, updatedAt: new Date() })
|
|
85
|
+
.run();
|
|
86
|
+
|
|
87
|
+
return getAutoReply(m);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Whether this mailbox has written to any of `addresses` (To, Cc or Bcc),
|
|
92
|
+
* as its correspondents record keeps it, even after that mail is deleted.
|
|
93
|
+
*/
|
|
94
|
+
export function hasWrittenTo(m: MailboxContext, addresses: readonly string[]) {
|
|
95
|
+
const unique = [
|
|
96
|
+
...new Set(addresses.map((address) => address.trim().toLowerCase())),
|
|
97
|
+
].filter(Boolean);
|
|
98
|
+
|
|
99
|
+
return (
|
|
100
|
+
unique.length > 0 &&
|
|
101
|
+
m.db
|
|
102
|
+
.select({ address: senders.address })
|
|
103
|
+
.from(senders)
|
|
104
|
+
.where(
|
|
105
|
+
and(inArray(senders.address, unique), gt(senders.outboundCount, 0))
|
|
106
|
+
)
|
|
107
|
+
.limit(1)
|
|
108
|
+
.all().length > 0
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* The automatic reply to a live delivery (never an import): the message
|
|
114
|
+
* for people inside the workspace or the one for everyone else. It is
|
|
115
|
+
* sent, not stored, and never retried: the sender is logged before
|
|
116
|
+
* sending, so a failure can't turn into a loop.
|
|
117
|
+
*/
|
|
118
|
+
export async function maybeAutoReply(
|
|
119
|
+
m: MailboxContext,
|
|
120
|
+
job: PendingInbound,
|
|
121
|
+
email: Email,
|
|
122
|
+
rawMime: ArrayBuffer
|
|
123
|
+
) {
|
|
124
|
+
const settings = getAutoReply(m);
|
|
125
|
+
const now = new Date();
|
|
126
|
+
|
|
127
|
+
if (!settings || autoReplyStatus(settings, now) !== "active") return;
|
|
128
|
+
|
|
129
|
+
const conversation = m.db
|
|
130
|
+
.select({ mailboxState: conversations.mailboxState })
|
|
131
|
+
.from(conversations)
|
|
132
|
+
.where(eq(conversations.id, email.conversationId))
|
|
133
|
+
.all()[0];
|
|
134
|
+
|
|
135
|
+
const mailboxId = m.state.id.name ?? "";
|
|
136
|
+
const db = createDb(m.env.DB);
|
|
137
|
+
|
|
138
|
+
const [mailbox, aliases] = await Promise.all([
|
|
139
|
+
db
|
|
140
|
+
.select({
|
|
141
|
+
address: mailboxAddressSql,
|
|
142
|
+
displayName: mailboxes.displayName,
|
|
143
|
+
})
|
|
144
|
+
.from(mailboxes)
|
|
145
|
+
.where(eq(mailboxes.id, mailboxId))
|
|
146
|
+
.limit(1)
|
|
147
|
+
.then((rows) => rows[0]),
|
|
148
|
+
db
|
|
149
|
+
.select({
|
|
150
|
+
address: mailboxAddressSql,
|
|
151
|
+
displayName: mailboxes.displayName,
|
|
152
|
+
})
|
|
153
|
+
.from(mailboxes)
|
|
154
|
+
.where(eq(mailboxes.aliasOf, mailboxId)),
|
|
155
|
+
]);
|
|
156
|
+
|
|
157
|
+
if (!mailbox) return;
|
|
158
|
+
|
|
159
|
+
const mailboxAddress = normalizeMailboxAddress(mailbox.address);
|
|
160
|
+
|
|
161
|
+
if (
|
|
162
|
+
messageSkipReason({
|
|
163
|
+
headers: parseHeaderBlock(rawMime),
|
|
164
|
+
envelopeFrom: job.envelopeFrom,
|
|
165
|
+
deliveredTo: job.envelopeTo,
|
|
166
|
+
ownAddresses: [
|
|
167
|
+
mailboxAddress,
|
|
168
|
+
...aliases.map((alias) => alias.address),
|
|
169
|
+
].map((address) => address.toLowerCase()),
|
|
170
|
+
listAddress: job.listAddress,
|
|
171
|
+
mailboxState: conversation?.mailboxState,
|
|
172
|
+
email,
|
|
173
|
+
})
|
|
174
|
+
) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// The reply goes to the envelope sender (RFC 3834), but who wrote is the
|
|
179
|
+
// From address: the envelope sender of forwarded mail, newsletters and
|
|
180
|
+
// many companies is a rewritten bounce address. From counts only when
|
|
181
|
+
// DMARC verified it, so it can't be forged.
|
|
182
|
+
const recipient = autoReplyRecipient(job.envelopeFrom);
|
|
183
|
+
// One reply per sender, however they spell their address's case.
|
|
184
|
+
const sender = recipient.toLowerCase();
|
|
185
|
+
const writer = email.fromJson[0]?.address ?? null;
|
|
186
|
+
|
|
187
|
+
const author =
|
|
188
|
+
writer && email.authenticationResultsJson?.verdict === "verified"
|
|
189
|
+
? writer
|
|
190
|
+
: null;
|
|
191
|
+
|
|
192
|
+
// The workspace's own message also needs the reply to go to From's
|
|
193
|
+
// domain: a colleague's signed message resent with another envelope
|
|
194
|
+
// sender (a replay) would otherwise hand it to whoever resent it.
|
|
195
|
+
const inside = Boolean(
|
|
196
|
+
author && mailDomainAllowed(author) && relaxedAligned(author, recipient)
|
|
197
|
+
);
|
|
198
|
+
|
|
199
|
+
const message = inside ? settings.inside : settings.outside;
|
|
200
|
+
|
|
201
|
+
if (!message.enabled) return;
|
|
202
|
+
|
|
203
|
+
if (
|
|
204
|
+
!inside &&
|
|
205
|
+
settings.outside.audience === "knownSenders" &&
|
|
206
|
+
!hasWrittenTo(m, author ? [author, recipient] : [recipient])
|
|
207
|
+
) {
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const intervalMs = settings.replyIntervalDays * DAY_MS;
|
|
212
|
+
|
|
213
|
+
const reserved = m.db.transaction((tx) => {
|
|
214
|
+
tx.delete(autoReplyLog)
|
|
215
|
+
.where(
|
|
216
|
+
lt(autoReplyLog.lastRepliedAt, new Date(now.getTime() - intervalMs))
|
|
217
|
+
)
|
|
218
|
+
.run();
|
|
219
|
+
|
|
220
|
+
if (
|
|
221
|
+
tx
|
|
222
|
+
.select({ sender: autoReplyLog.senderAddress })
|
|
223
|
+
.from(autoReplyLog)
|
|
224
|
+
.where(eq(autoReplyLog.senderAddress, sender))
|
|
225
|
+
.all().length > 0
|
|
226
|
+
) {
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const lastHour =
|
|
231
|
+
tx
|
|
232
|
+
.select({ count: sql<number>`count(*)` })
|
|
233
|
+
.from(autoReplyLog)
|
|
234
|
+
.where(
|
|
235
|
+
gt(autoReplyLog.lastRepliedAt, new Date(now.getTime() - 3_600_000))
|
|
236
|
+
)
|
|
237
|
+
.all()[0]?.count ?? 0;
|
|
238
|
+
|
|
239
|
+
if (lastHour >= AUTO_REPLY_HOURLY_CAP) return false;
|
|
240
|
+
|
|
241
|
+
tx.insert(autoReplyLog)
|
|
242
|
+
.values({
|
|
243
|
+
senderAddress: sender,
|
|
244
|
+
lastRepliedAt: now,
|
|
245
|
+
lastOriginalEmailId: email.id,
|
|
246
|
+
})
|
|
247
|
+
.run();
|
|
248
|
+
|
|
249
|
+
return true;
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
if (!reserved) return;
|
|
253
|
+
|
|
254
|
+
// Mail that came through an alias is answered from the alias.
|
|
255
|
+
const from = replyAlias(email, mailboxAddress, aliases) ?? mailbox;
|
|
256
|
+
|
|
257
|
+
try {
|
|
258
|
+
const images = await Promise.all(
|
|
259
|
+
inlineImages(
|
|
260
|
+
autoReplyImagePrefix(mailboxId),
|
|
261
|
+
message.html,
|
|
262
|
+
message.images
|
|
263
|
+
).map(async (image) => {
|
|
264
|
+
const object = await m.env.MAIL_STORAGE.get(image.r2Key);
|
|
265
|
+
|
|
266
|
+
if (!object) throw new Error(`Image ${image.contentId} is missing`);
|
|
267
|
+
|
|
268
|
+
return {
|
|
269
|
+
contentId: image.contentId,
|
|
270
|
+
filename: image.filename,
|
|
271
|
+
type: image.contentType,
|
|
272
|
+
content: await object.arrayBuffer(),
|
|
273
|
+
};
|
|
274
|
+
})
|
|
275
|
+
);
|
|
276
|
+
|
|
277
|
+
// Sent mail, so the archive keeps it; it's never stored, so its own id.
|
|
278
|
+
const result = await sendArchived(
|
|
279
|
+
m.env,
|
|
280
|
+
{ id: `ar_${crypto.randomUUID()}`, mailboxId },
|
|
281
|
+
buildAutoReply({
|
|
282
|
+
from: {
|
|
283
|
+
address: normalizeMailboxAddress(from.address),
|
|
284
|
+
name: from.displayName,
|
|
285
|
+
},
|
|
286
|
+
recipient,
|
|
287
|
+
original: email,
|
|
288
|
+
message,
|
|
289
|
+
images,
|
|
290
|
+
})
|
|
291
|
+
);
|
|
292
|
+
|
|
293
|
+
m.db
|
|
294
|
+
.update(autoReplyLog)
|
|
295
|
+
.set({ lastOutboundMessageId: result.messageId })
|
|
296
|
+
.where(eq(autoReplyLog.senderAddress, sender))
|
|
297
|
+
.run();
|
|
298
|
+
} catch (error) {
|
|
299
|
+
console.error(`Auto-reply to ${recipient} failed`, error);
|
|
300
|
+
}
|
|
301
|
+
}
|