emailengine-app 2.81.2 → 2.82.1
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/.ncurc.js +33 -10
- package/.release-please-manifest.json +1 -1
- package/CHANGELOG.md +71 -0
- package/DOCKER_DEPLOYMENT.md +13 -12
- package/Dockerfile +2 -0
- package/bin/emailengine.js +105 -25
- package/config/default.toml +0 -5
- package/config/test.toml +0 -5
- package/data/google-crawlers.json +1 -1
- package/encrypt.js +70 -42
- package/getswagger.sh +0 -4
- package/lib/account/account-state.js +2 -110
- package/lib/account/expected-identity.js +1 -1
- package/lib/account-assignment.js +138 -1
- package/lib/account.js +289 -869
- package/lib/api-reference/index.js +2 -3
- package/lib/api-reference/schema-tree.js +53 -3
- package/lib/api-routes/account-binding.js +58 -0
- package/lib/api-routes/account-routes.js +65 -37
- package/lib/api-routes/behavior-notes.js +4 -0
- package/lib/api-routes/blocklist-routes.js +2 -2
- package/lib/api-routes/bull-board-routes.js +1 -6
- package/lib/api-routes/export-routes.js +2 -22
- package/lib/api-routes/gateway-routes.js +15 -16
- package/lib/api-routes/index.js +1 -13
- package/lib/api-routes/mailbox-routes.js +2 -7
- package/lib/api-routes/mcp-oauth-routes.js +2 -1
- package/lib/api-routes/mcp-routes.js +7 -7
- package/lib/api-routes/message-routes.js +50 -238
- package/lib/api-routes/mint-bounds.js +110 -0
- package/lib/api-routes/oauth2-app-routes.js +23 -32
- package/lib/api-routes/operation-impact.js +1 -1
- package/lib/api-routes/outbox-routes.js +3 -3
- package/lib/api-routes/permission-map.js +1 -7
- package/lib/api-routes/route-helpers.js +11 -5
- package/lib/api-routes/settings-routes.js +20 -21
- package/lib/api-routes/stats-routes.js +1 -1
- package/lib/api-routes/submit-routes.js +17 -18
- package/lib/api-routes/template-routes.js +4 -9
- package/lib/api-routes/token-routes.js +28 -6
- package/lib/api-token-scheme.js +71 -0
- package/lib/arf-detect.js +33 -6
- package/lib/auth-token.js +44 -24
- package/lib/autodetect-imap-settings.js +305 -123
- package/lib/bounce-detect.js +25 -16
- package/lib/calendar-event.js +78 -0
- package/lib/consts.js +54 -5
- package/lib/create-imap-client.js +24 -0
- package/lib/db.js +77 -30
- package/lib/delivery-error.js +70 -2
- package/lib/egress-fetch.js +54 -3
- package/lib/egress-filter.js +11 -0
- package/lib/email-client/api-retry.js +147 -0
- package/lib/email-client/base-client.js +383 -536
- package/lib/email-client/credential-errors.js +21 -5
- package/lib/email-client/gmail/gmail-api.js +38 -125
- package/lib/email-client/gmail/send-request.js +1 -1
- package/lib/email-client/gmail-client.js +77 -51
- package/lib/email-client/imap/mailbox.js +158 -324
- package/lib/email-client/imap/stale-view.js +109 -0
- package/lib/email-client/imap/subconnection.js +46 -9
- package/lib/email-client/imap/sync-operations.js +5 -37
- package/lib/email-client/imap-client.js +190 -65
- package/lib/email-client/message-builder.js +50 -25
- package/lib/email-client/notification-handler.js +5 -219
- package/lib/email-client/outlook/graph-api.js +147 -126
- package/lib/email-client/outlook/send-request.js +10 -4
- package/lib/email-client/outlook-client.js +389 -463
- package/lib/email-client/smtp-pool-manager.js +3 -2
- package/lib/encrypt.js +184 -19
- package/lib/enum-descriptions.js +6 -3
- package/lib/export.js +141 -41
- package/lib/gateway.js +25 -6
- package/lib/get-raw-email.js +26 -44
- package/lib/get-secret.js +1 -21
- package/lib/handlebars-helpers.js +31 -10
- package/lib/imap-proxy-auth.js +13 -29
- package/lib/imapproxy/imap-core/README.md +1 -0
- package/lib/imapproxy/imap-core/lib/handler/imap-parser.js +12 -0
- package/lib/imapproxy/imap-core/lib/imap-command.js +74 -10
- package/lib/imapproxy/imap-core/lib/imap-connection.js +9 -1
- package/lib/imapproxy/imap-core/lib/imap-server.js +112 -26
- package/lib/imapproxy/imap-core/lib/imap-stream.js +41 -1
- package/lib/imapproxy/imap-core/lib/imap-tools.js +10 -2
- package/lib/imapproxy/imap-core/lib/indexer/indexer.js +4 -2
- package/lib/imapproxy/imap-server.js +17 -177
- package/lib/imapproxy/proxy-handoff.js +235 -0
- package/lib/imapproxy/response-error.js +33 -0
- package/lib/imapproxy/upstream-client.js +2 -2
- package/lib/license-beacon.js +3 -9
- package/lib/llm-pre-process.js +182 -43
- package/lib/logger.js +1 -1
- package/lib/lua/ee-export-queue-add.lua +39 -0
- package/lib/lua/ee-get-idempotency.lua +15 -3
- package/lib/lua/ee-release-attempts.lua +23 -0
- package/lib/lua/ee-reserve-attempts.lua +42 -0
- package/lib/lua/h-set-bigger.lua +12 -2
- package/lib/lua/s-list-accounts.lua +6 -3
- package/lib/mcp/index.js +1 -2
- package/lib/mcp/listen.js +221 -18
- package/lib/mcp/oauth.js +44 -5
- package/lib/mcp/resources.js +8 -2
- package/lib/mcp/tools.js +108 -36
- package/lib/message-port-stream.js +64 -6
- package/lib/oauth/external-account-signer.js +16 -13
- package/lib/oauth/gmail.js +64 -32
- package/lib/oauth/mail-ru.js +24 -17
- package/lib/oauth/outlook.js +53 -40
- package/lib/oauth/pubsub/google.js +38 -2
- package/lib/oauth/scope-checker.js +39 -8
- package/lib/oauth/scope-list.js +55 -0
- package/lib/oauth/token-fetch.js +47 -0
- package/lib/oauth/verify-app.js +2 -2
- package/lib/oauth2-apps.js +154 -90
- package/lib/openapi/build-document.js +1 -1
- package/lib/openapi/joi-schema.js +78 -9
- package/lib/outbox.js +2 -7
- package/lib/parse-message.js +21 -0
- package/lib/payload-examples-webhooks.json +4 -7
- package/lib/pbkdf2-phc.js +101 -0
- package/lib/privileged-settings.js +0 -14
- package/lib/queue-retention.js +4 -2
- package/lib/rate-limit.js +54 -15
- package/lib/read-env-value.js +33 -2
- package/lib/redis-scan-delete.js +56 -29
- package/lib/redis-url.js +3 -1
- package/lib/respawn-backoff.js +5 -4
- package/lib/response-stream.js +25 -4
- package/lib/rewrite-text-nodes.js +27 -40
- package/lib/routes-ui.js +0 -5
- package/lib/schemas.js +180 -253
- package/lib/sentry.js +21 -5
- package/lib/settings.js +58 -59
- package/lib/smtp-auth.js +6 -2
- package/lib/smtp-message-processor.js +28 -7
- package/lib/sso.js +7 -3
- package/lib/stream-encrypt.js +14 -13
- package/lib/swagger-options.js +3 -1
- package/lib/tls/provision.js +69 -3
- package/lib/tls/store.js +14 -1
- package/lib/token-audit-log.js +3 -2
- package/lib/tools.js +114 -53
- package/lib/totp.js +16 -1
- package/lib/ui-routes/account-routes.js +67 -70
- package/lib/ui-routes/admin-config-routes.js +111 -134
- package/lib/ui-routes/admin-entities-routes.js +101 -191
- package/lib/ui-routes/ai-options.js +104 -0
- package/lib/ui-routes/auth-routes.js +98 -18
- package/lib/ui-routes/export-routes.js +3 -23
- package/lib/ui-routes/internals-routes.js +12 -9
- package/lib/ui-routes/network-config-routes.js +8 -18
- package/lib/ui-routes/oauth-config-routes.js +83 -228
- package/lib/ui-routes/route-helpers.js +63 -67
- package/lib/ui-routes/special-use-paths.js +33 -0
- package/lib/ui-routes/unsubscribe-routes.js +2 -2
- package/lib/utils/decode-headers.js +32 -5
- package/lib/utils/header-map.js +62 -1
- package/lib/utils/network.js +11 -17
- package/lib/web-safe-html.js +77 -4
- package/lib/webhook-request.js +9 -1
- package/lib/webhook-routing.js +27 -1
- package/lib/webhooks.js +120 -43
- package/lib/worker-rpc-error.js +43 -2
- package/list-generate.js +13 -2
- package/package.json +38 -46
- package/sbom.json +1 -1
- package/scripts/refresh-test-tokens.js +42 -14
- package/server.js +338 -292
- package/setup-production.sh +16 -7
- package/static/css/flyonui.css +1 -1
- package/static/css/src/app.tailwind.css +7 -8
- package/static/js/ee-client.js +145 -70
- package/static/js/ui.js +350 -154
- package/static/licenses.html +349 -1740
- package/translations/messages.pot +129 -111
- package/views/account-workspace-error.hbs +30 -0
- package/views/accounts/account.hbs +9 -4
- package/views/accounts/browse.hbs +10 -4
- package/views/accounts/edit.hbs +11 -2
- package/views/config/ai.hbs +49 -41
- package/views/config/oauth/app.hbs +2 -1
- package/views/dashboard.hbs +18 -16
- package/views/internals/index.hbs +1 -1
- package/views/layout/app.hbs +0 -22
- package/views/layout/login.hbs +2 -1
- package/views/partials/filter_editor_js.hbs +4 -3
- package/views/partials/gmail_api_scopes.hbs +16 -0
- package/views/partials/oauth_form.hbs +67 -94
- package/views/partials/oauth_pubsub_app_select.hbs +43 -0
- package/views/partials/scope_info.hbs +14 -23
- package/views/partials/side_menu.hbs +0 -4
- package/views/partials/ui/account-picker.hbs +6 -32
- package/views/partials/ui/model-picker.hbs +31 -0
- package/views/partials/ui/pagination.hbs +6 -1
- package/views/partials/ui/picker.hbs +47 -0
- package/views/unsubscribe.hbs +1 -1
- package/workers/api.js +82 -47
- package/workers/export.js +55 -60
- package/workers/imap.js +132 -49
- package/workers/submit.js +5 -6
- package/workers/webhooks.js +46 -27
- package/.githooks/pre-commit +0 -80
- package/.github/CODE_OF_CONDUCT.md +0 -76
- package/.github/contributing.md +0 -17
- package/.github/dependabot.yml +0 -22
- package/.github/docker-smoke-test.sh +0 -61
- package/.github/workflows/deploy.yml +0 -215
- package/.github/workflows/e2e.yml +0 -55
- package/.github/workflows/release.yaml +0 -217
- package/.github/workflows/test.yml +0 -307
- package/lib/api-routes/chat-routes.js +0 -535
- package/lib/document-store.js +0 -75
- package/lib/es.js +0 -757
- package/lib/generate-text-preview.js +0 -61
- package/lib/imapproxy/imap-core/memory-notifier.js +0 -129
- package/lib/pre-process.js +0 -196
- package/lib/threads.js +0 -155
- package/lib/ui-routes/document-store-routes.js +0 -806
- package/playwright.config.js +0 -47
- package/views/config/document-store/chat.hbs +0 -286
- package/views/config/document-store/index.hbs +0 -146
- package/views/config/document-store/mappings/index.hbs +0 -108
- package/views/config/document-store/mappings/new.hbs +0 -68
- package/views/config/document-store/pre-processing/index.hbs +0 -103
- package/views/partials/document_store_header.hbs +0 -30
- package/workers/documents.js +0 -1128
|
@@ -4,7 +4,6 @@ const { threadId: workerThreadId } = require('worker_threads');
|
|
|
4
4
|
const crypto = require('crypto');
|
|
5
5
|
const logger = require('../logger');
|
|
6
6
|
const settings = require('../settings');
|
|
7
|
-
const { isDocumentStoreEnabled } = require('../document-store');
|
|
8
7
|
const msgpack = require('../msgpack');
|
|
9
8
|
const { templates } = require('../templates');
|
|
10
9
|
const { Gateway } = require('../gateway');
|
|
@@ -21,15 +20,17 @@ const { arfDetect, camelCaseComplaint, FEEDBACK_REPORT_TYPE, ORIGINAL_MESSAGE_TY
|
|
|
21
20
|
const { createTransaction } = require('../redis-operations');
|
|
22
21
|
const { ACCOUNT_STATES } = require('../account/account-state');
|
|
23
22
|
const { credentialErrorStatus, isTransientCredentialError, resolveAuthServerCredentials } = require('./credential-errors');
|
|
24
|
-
const
|
|
23
|
+
const { parseMessage } = require('../parse-message');
|
|
25
24
|
const libmime = require('libmime');
|
|
26
|
-
const { bounceDetect, applyDeliveryReport } = require('../bounce-detect');
|
|
27
|
-
const
|
|
28
|
-
const { llmPreProcess } = require('../llm-pre-process');
|
|
25
|
+
const { bounceDetect, applyDeliveryReport, PARSER_OPTIONS: DSN_PARSER_OPTIONS } = require('../bounce-detect');
|
|
26
|
+
const { parseCalendarEvent, sortCalendarAttachments, CALENDAR_CONTENT_TYPES } = require('../calendar-event');
|
|
27
|
+
const { llmPreProcess, summaryFetchHeaders, summaryProvider } = require('../llm-pre-process');
|
|
28
|
+
const { notificationHeaderFields, narrowHeaders } = require('../utils/header-map');
|
|
29
29
|
const { oauth2Apps } = require('../oauth2-apps');
|
|
30
30
|
const { Account } = require('../account');
|
|
31
31
|
const { removeBcc } = require('../get-raw-email');
|
|
32
32
|
const {
|
|
33
|
+
applyTemplate,
|
|
33
34
|
SmtpConfigBuilder,
|
|
34
35
|
NetworkRoutingBuilder,
|
|
35
36
|
NotificationBuilder,
|
|
@@ -54,7 +55,7 @@ const {
|
|
|
54
55
|
getDateBuckets,
|
|
55
56
|
useAuthServerForOAuth2,
|
|
56
57
|
collectCidReferences,
|
|
57
|
-
|
|
58
|
+
embedCidImages
|
|
58
59
|
} = require('../tools');
|
|
59
60
|
|
|
60
61
|
// Import application constants
|
|
@@ -164,6 +165,74 @@ function applyReferenceRecipients(data, referencedMessage) {
|
|
|
164
165
|
}
|
|
165
166
|
}
|
|
166
167
|
|
|
168
|
+
// Message-ids kept in an outgoing References header besides the thread root, so a long thread
|
|
169
|
+
// does not grow the header without bound
|
|
170
|
+
const MAX_REFERENCES_TAIL = 20;
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Builds the References header of a reply or forward (RFC 5322 3.6.4): the parent's References,
|
|
174
|
+
* or its In-Reply-To when it has none, followed by the parent's own Message-ID last. JWZ-style
|
|
175
|
+
* threading reads the last entry as the direct parent, so the order carries meaning. A repeated
|
|
176
|
+
* id keeps its last position, and a long chain is cut to the thread root plus the most recent
|
|
177
|
+
* MAX_REFERENCES_TAIL entries
|
|
178
|
+
* @param {Object} referencedMessage - The message being replied to or forwarded
|
|
179
|
+
* @returns {string[]} Message-ids in angle brackets, in header order
|
|
180
|
+
*/
|
|
181
|
+
function buildReferences(referencedMessage) {
|
|
182
|
+
const split = value =>
|
|
183
|
+
[]
|
|
184
|
+
.concat(value || [])
|
|
185
|
+
.flatMap(line => (typeof line === 'string' ? line.split(/\s+/) : []))
|
|
186
|
+
.map(ref => ref.trim())
|
|
187
|
+
.filter(ref => ref)
|
|
188
|
+
.map(ref => {
|
|
189
|
+
// Ensure proper Message-ID format
|
|
190
|
+
if (!/^</.test(ref)) {
|
|
191
|
+
ref = '<' + ref;
|
|
192
|
+
}
|
|
193
|
+
if (!/>$/.test(ref)) {
|
|
194
|
+
ref = ref + '>';
|
|
195
|
+
}
|
|
196
|
+
return ref;
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
let chain = split(referencedMessage.headers && referencedMessage.headers.references);
|
|
200
|
+
if (!chain.length) {
|
|
201
|
+
chain = split(referencedMessage.inReplyTo);
|
|
202
|
+
}
|
|
203
|
+
chain = chain.concat(split(referencedMessage.messageId));
|
|
204
|
+
|
|
205
|
+
// Walked from the end, so the last occurrence of a repeated id is the one kept, and pushed
|
|
206
|
+
// then reversed once: the sender controls the length of this header, and an unshift per
|
|
207
|
+
// entry made the dedupe quadratic
|
|
208
|
+
let seen = new Set();
|
|
209
|
+
let references = [];
|
|
210
|
+
for (let i = chain.length - 1; i >= 0; i--) {
|
|
211
|
+
if (!seen.has(chain[i])) {
|
|
212
|
+
seen.add(chain[i]);
|
|
213
|
+
references.push(chain[i]);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
references.reverse();
|
|
217
|
+
|
|
218
|
+
if (references.length > MAX_REFERENCES_TAIL + 1) {
|
|
219
|
+
references = [references[0]].concat(references.slice(-MAX_REFERENCES_TAIL));
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return references;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Whether a parsed bounce reports a failed delivery. A DSN also reports delivered, delayed,
|
|
227
|
+
* relayed and expanded recipients, and none of those is a bounce to store or to fire emailBounce
|
|
228
|
+
* for; a delayed DSN that landed in Junk used to reach the bounce path with its action set
|
|
229
|
+
* @param {Object} bounce - Result of bounceDetect()
|
|
230
|
+
* @returns {boolean}
|
|
231
|
+
*/
|
|
232
|
+
function isFailedDelivery(bounce) {
|
|
233
|
+
return !!bounce && typeof bounce.action === 'string' && bounce.action.trim().toLowerCase() === 'failed' && !!bounce.recipient && !!bounce.messageId;
|
|
234
|
+
}
|
|
235
|
+
|
|
167
236
|
// Finds the attachments of a referenced message whose content id the given HTML links to
|
|
168
237
|
function findReferencedCidAttachments(html, attachments) {
|
|
169
238
|
let cidAttachments = [];
|
|
@@ -340,8 +409,6 @@ class BaseClient {
|
|
|
340
409
|
// Message queue connections
|
|
341
410
|
this.notifyQueue = this.options.notifyQueue;
|
|
342
411
|
this.submitQueue = this.options.submitQueue;
|
|
343
|
-
this.documentsQueue = this.options.documentsQueue;
|
|
344
|
-
this.flowProducer = this.options.flowProducer;
|
|
345
412
|
|
|
346
413
|
// Inter-process communication handler
|
|
347
414
|
this.call = this.options.call;
|
|
@@ -377,9 +444,7 @@ class BaseClient {
|
|
|
377
444
|
// Initialize notification handler for webhook and event delivery
|
|
378
445
|
this.notificationHandler = new NotificationHandler({
|
|
379
446
|
account: this.account,
|
|
380
|
-
logger: this.logger
|
|
381
|
-
flowProducer: this.flowProducer,
|
|
382
|
-
documentsQueue: this.documentsQueue
|
|
447
|
+
logger: this.logger
|
|
383
448
|
});
|
|
384
449
|
}
|
|
385
450
|
|
|
@@ -409,10 +474,6 @@ class BaseClient {
|
|
|
409
474
|
return null;
|
|
410
475
|
}
|
|
411
476
|
|
|
412
|
-
async subconnections() {
|
|
413
|
-
return [];
|
|
414
|
-
}
|
|
415
|
-
|
|
416
477
|
async getQuota() {
|
|
417
478
|
return false;
|
|
418
479
|
}
|
|
@@ -687,13 +748,13 @@ class BaseClient {
|
|
|
687
748
|
return;
|
|
688
749
|
}
|
|
689
750
|
|
|
690
|
-
let parsed = await
|
|
751
|
+
let parsed = await parseMessage(content, DSN_PARSER_OPTIONS);
|
|
691
752
|
if (applyDeliveryReport(parsed, logContext, this.logger)) {
|
|
692
753
|
return;
|
|
693
754
|
}
|
|
694
755
|
|
|
695
756
|
let bounce = await bounceDetect({ parsed });
|
|
696
|
-
if (bounce
|
|
757
|
+
if (isFailedDelivery(bounce)) {
|
|
697
758
|
messageData.isBounce = true;
|
|
698
759
|
messageData.relatedMessageId = bounce.messageId;
|
|
699
760
|
}
|
|
@@ -1263,7 +1324,7 @@ class BaseClient {
|
|
|
1263
1324
|
* @param {Object} data - Event data payload
|
|
1264
1325
|
* @param {Object} extraOpts - Additional options
|
|
1265
1326
|
*/
|
|
1266
|
-
async notify(mailbox, event, data
|
|
1327
|
+
async notify(mailbox, event, data) {
|
|
1267
1328
|
// Handle error state tracking for connection/auth errors
|
|
1268
1329
|
// This is BaseClient-specific logic that must run before notification
|
|
1269
1330
|
switch (event) {
|
|
@@ -1279,8 +1340,8 @@ class BaseClient {
|
|
|
1279
1340
|
}
|
|
1280
1341
|
}
|
|
1281
1342
|
|
|
1282
|
-
// Delegate to notification handler for webhook
|
|
1283
|
-
await this.notificationHandler.notify(mailbox, event, data
|
|
1343
|
+
// Delegate to notification handler for webhook delivery
|
|
1344
|
+
await this.notificationHandler.notify(mailbox, event, data);
|
|
1284
1345
|
}
|
|
1285
1346
|
|
|
1286
1347
|
/**
|
|
@@ -1412,47 +1473,42 @@ class BaseClient {
|
|
|
1412
1473
|
* @returns {Object} Final delegated account data
|
|
1413
1474
|
*/
|
|
1414
1475
|
async getDelegatedAccount(accountData) {
|
|
1415
|
-
let redirect = 0;
|
|
1416
1476
|
let providerAccountData = accountData;
|
|
1417
|
-
let
|
|
1418
|
-
|
|
1419
|
-
|
|
1477
|
+
let seenAccounts = new Set([accountData.account]);
|
|
1478
|
+
const hopsAllowed = 20;
|
|
1479
|
+
|
|
1480
|
+
// Follow delegation chain. Each hop is built from the account the previous hop resolved
|
|
1481
|
+
// to - building it from the original account reloaded the first hop on every pass, so a
|
|
1482
|
+
// valid A->B->C chain was reported as a loop
|
|
1483
|
+
for (let hop = 0; hop < hopsAllowed; hop++) {
|
|
1484
|
+
const nextAccount = providerAccountData.oauth2.auth.delegatedAccount;
|
|
1485
|
+
|
|
1486
|
+
// Detect delegation loops
|
|
1487
|
+
if (seenAccounts.has(nextAccount)) {
|
|
1488
|
+
throw new Error('Delegation looping detected');
|
|
1489
|
+
}
|
|
1490
|
+
seenAccounts.add(nextAccount);
|
|
1420
1491
|
|
|
1421
|
-
// Follow delegation chain
|
|
1422
|
-
while (redirect++ < hopsAllowed) {
|
|
1423
1492
|
// Load delegated account if not cached
|
|
1424
|
-
if (!this.delegatedAccountObject || this.delegatedAccountObject.account !==
|
|
1493
|
+
if (!this.delegatedAccountObject || this.delegatedAccountObject.account !== nextAccount) {
|
|
1425
1494
|
this.delegatedAccountObject = new Account({
|
|
1426
|
-
account:
|
|
1495
|
+
account: nextAccount,
|
|
1427
1496
|
redis: this.accountObject.redis,
|
|
1428
1497
|
call: this.accountObject.call,
|
|
1429
1498
|
secret: this.accountObject.secret,
|
|
1430
1499
|
timeout: this.accountObject.timeout
|
|
1431
1500
|
});
|
|
1432
1501
|
}
|
|
1433
|
-
delegatedAccountData = await this.delegatedAccountObject.loadAccountData();
|
|
1502
|
+
const delegatedAccountData = await this.delegatedAccountObject.loadAccountData();
|
|
1434
1503
|
|
|
1435
1504
|
// Check if this account also delegates to another
|
|
1436
|
-
if (delegatedAccountData.oauth2
|
|
1437
|
-
|
|
1438
|
-
if (seenAccounts.has(providerAccountData.account)) {
|
|
1439
|
-
// loop detected
|
|
1440
|
-
let error = new Error('Delegation looping detected');
|
|
1441
|
-
throw error;
|
|
1442
|
-
}
|
|
1443
|
-
seenAccounts.add(providerAccountData.account);
|
|
1444
|
-
providerAccountData = delegatedAccountData;
|
|
1445
|
-
continue;
|
|
1505
|
+
if (!(delegatedAccountData.oauth2?.auth?.delegatedUser && delegatedAccountData.oauth2.auth.delegatedAccount)) {
|
|
1506
|
+
return delegatedAccountData;
|
|
1446
1507
|
}
|
|
1447
|
-
|
|
1448
|
-
}
|
|
1449
|
-
|
|
1450
|
-
if (redirect >= hopsAllowed) {
|
|
1451
|
-
let error = new Error('Too many delegation hops');
|
|
1452
|
-
throw error;
|
|
1508
|
+
providerAccountData = delegatedAccountData;
|
|
1453
1509
|
}
|
|
1454
1510
|
|
|
1455
|
-
|
|
1511
|
+
throw new Error('Too many delegation hops');
|
|
1456
1512
|
}
|
|
1457
1513
|
|
|
1458
1514
|
/**
|
|
@@ -1676,11 +1732,9 @@ class BaseClient {
|
|
|
1676
1732
|
let gatewayObject;
|
|
1677
1733
|
if (data.gateway) {
|
|
1678
1734
|
gatewayObject = new Gateway({ gateway: data.gateway, redis: this.redis, secret: this.secret });
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
this.logger.warn({ msg: 'Failed to load gateway data', messageId: data.messageId, gateway: data.gateway, err });
|
|
1683
|
-
}
|
|
1735
|
+
// The message was asked to go through this gateway, so it must not fall through to
|
|
1736
|
+
// the account's own SMTP server: a missing gateway throws GatewayNotFound (404)
|
|
1737
|
+
gatewayData = await gatewayObject.loadGatewayData();
|
|
1684
1738
|
}
|
|
1685
1739
|
|
|
1686
1740
|
// Verify SMTP capability
|
|
@@ -1713,17 +1767,7 @@ class BaseClient {
|
|
|
1713
1767
|
throw err;
|
|
1714
1768
|
}
|
|
1715
1769
|
|
|
1716
|
-
|
|
1717
|
-
data.render = data.render || {};
|
|
1718
|
-
data.render.format = templateData.format;
|
|
1719
|
-
}
|
|
1720
|
-
|
|
1721
|
-
// Merge template content into message
|
|
1722
|
-
for (let key of Object.keys(templateData.content || {})) {
|
|
1723
|
-
data[key] = templateData.content[key];
|
|
1724
|
-
}
|
|
1725
|
-
|
|
1726
|
-
delete data.template;
|
|
1770
|
+
applyTemplate(data, templateData);
|
|
1727
1771
|
}
|
|
1728
1772
|
|
|
1729
1773
|
// Handle single message or mail merge
|
|
@@ -1863,88 +1907,62 @@ class BaseClient {
|
|
|
1863
1907
|
delete data.tz;
|
|
1864
1908
|
|
|
1865
1909
|
let referencedMessage;
|
|
1866
|
-
let documentStoreUsed = false;
|
|
1867
1910
|
|
|
1868
1911
|
// Resolve reference and update reference/in-reply-to headers
|
|
1869
1912
|
if (data.reference && data.reference.message) {
|
|
1870
|
-
//
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1913
|
+
// Fetch from IMAP/API with retry logic for transient errors
|
|
1914
|
+
let extendedData = data.reference.inline || data.reference.forwardAttachments;
|
|
1915
|
+
|
|
1916
|
+
try {
|
|
1917
|
+
referencedMessage = await retryOnTransientError(
|
|
1918
|
+
async () => {
|
|
1919
|
+
return await this.getMessage(
|
|
1920
|
+
data.reference.message,
|
|
1921
|
+
{
|
|
1922
|
+
fields: !extendedData
|
|
1923
|
+
? {
|
|
1924
|
+
uid: true,
|
|
1925
|
+
flags: true,
|
|
1926
|
+
envelope: true,
|
|
1927
|
+
headers: ['references']
|
|
1928
|
+
}
|
|
1929
|
+
: false,
|
|
1930
|
+
textType: extendedData ? '*' : false
|
|
1931
|
+
},
|
|
1932
|
+
connectionOptions
|
|
1933
|
+
);
|
|
1934
|
+
},
|
|
1935
|
+
{
|
|
1936
|
+
maxAttempts: 3,
|
|
1937
|
+
baseDelay: 1000,
|
|
1938
|
+
logger: this.logger
|
|
1939
|
+
}
|
|
1940
|
+
);
|
|
1941
|
+
} catch (err) {
|
|
1942
|
+
// Check if this is a transient error that exhausted retries
|
|
1943
|
+
const isTransient = isTransientError(err);
|
|
1944
|
+
|
|
1945
|
+
if (isTransient && data.reference.ignoreMissing) {
|
|
1946
|
+
// Transient error but ignoreMissing is true - log and continue without reference
|
|
1947
|
+
this.logger.warn({
|
|
1948
|
+
msg: 'Failed to fetch referenced message due to transient error, continuing without reference',
|
|
1949
|
+
messageId: data.reference.message,
|
|
1950
|
+
err,
|
|
1951
|
+
statusCode: err.statusCode || (err.oauthRequest && err.oauthRequest.status)
|
|
1876
1952
|
});
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
let error = new Error('
|
|
1882
|
-
error.
|
|
1883
|
-
|
|
1884
|
-
statusCode: err.meta && err.meta.statusCode
|
|
1885
|
-
};
|
|
1886
|
-
error.code = 'ESRequestError';
|
|
1887
|
-
error.statusCode = (err.meta && err.meta.statusCode) || 500;
|
|
1953
|
+
referencedMessage = null;
|
|
1954
|
+
} else if (!isTransient && err.statusCode === 404) {
|
|
1955
|
+
// Permanent 404 error - respect ignoreMissing
|
|
1956
|
+
if (!data.reference.ignoreMissing) {
|
|
1957
|
+
let error = new Error('Referenced message was not found');
|
|
1958
|
+
error.code = 'ReferenceNotFound';
|
|
1959
|
+
error.statusCode = 404;
|
|
1888
1960
|
throw error;
|
|
1889
1961
|
}
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
let extendedData = data.reference.inline || data.reference.forwardAttachments;
|
|
1895
|
-
|
|
1896
|
-
try {
|
|
1897
|
-
referencedMessage = await retryOnTransientError(
|
|
1898
|
-
async () => {
|
|
1899
|
-
return await this.getMessage(
|
|
1900
|
-
data.reference.message,
|
|
1901
|
-
{
|
|
1902
|
-
fields: !extendedData
|
|
1903
|
-
? {
|
|
1904
|
-
uid: true,
|
|
1905
|
-
flags: true,
|
|
1906
|
-
envelope: true,
|
|
1907
|
-
headers: ['references']
|
|
1908
|
-
}
|
|
1909
|
-
: false,
|
|
1910
|
-
header: extendedData ? true : false,
|
|
1911
|
-
textType: extendedData ? '*' : false
|
|
1912
|
-
},
|
|
1913
|
-
connectionOptions
|
|
1914
|
-
);
|
|
1915
|
-
},
|
|
1916
|
-
{
|
|
1917
|
-
maxAttempts: 3,
|
|
1918
|
-
baseDelay: 1000,
|
|
1919
|
-
logger: this.logger
|
|
1920
|
-
}
|
|
1921
|
-
);
|
|
1922
|
-
} catch (err) {
|
|
1923
|
-
// Check if this is a transient error that exhausted retries
|
|
1924
|
-
const isTransient = isTransientError(err);
|
|
1925
|
-
|
|
1926
|
-
if (isTransient && data.reference.ignoreMissing) {
|
|
1927
|
-
// Transient error but ignoreMissing is true - log and continue without reference
|
|
1928
|
-
this.logger.warn({
|
|
1929
|
-
msg: 'Failed to fetch referenced message due to transient error, continuing without reference',
|
|
1930
|
-
messageId: data.reference.message,
|
|
1931
|
-
err,
|
|
1932
|
-
statusCode: err.statusCode || (err.oauthRequest && err.oauthRequest.status)
|
|
1933
|
-
});
|
|
1934
|
-
referencedMessage = null;
|
|
1935
|
-
} else if (!isTransient && err.statusCode === 404) {
|
|
1936
|
-
// Permanent 404 error - respect ignoreMissing
|
|
1937
|
-
if (!data.reference.ignoreMissing) {
|
|
1938
|
-
let error = new Error('Referenced message was not found');
|
|
1939
|
-
error.code = 'ReferenceNotFound';
|
|
1940
|
-
error.statusCode = 404;
|
|
1941
|
-
throw error;
|
|
1942
|
-
}
|
|
1943
|
-
referencedMessage = null;
|
|
1944
|
-
} else {
|
|
1945
|
-
// Either transient error with ignoreMissing=false, or other permanent error
|
|
1946
|
-
throw err;
|
|
1947
|
-
}
|
|
1962
|
+
referencedMessage = null;
|
|
1963
|
+
} else {
|
|
1964
|
+
// Either transient error with ignoreMissing=false, or other permanent error
|
|
1965
|
+
throw err;
|
|
1948
1966
|
}
|
|
1949
1967
|
}
|
|
1950
1968
|
|
|
@@ -1965,25 +1983,7 @@ class BaseClient {
|
|
|
1965
1983
|
}
|
|
1966
1984
|
|
|
1967
1985
|
// Build references header chain
|
|
1968
|
-
let references =
|
|
1969
|
-
.concat(referencedMessage.messageId || [])
|
|
1970
|
-
.concat(referencedMessage.inReplyTo || [])
|
|
1971
|
-
.concat((referencedMessage.headers && referencedMessage.headers.references) || [])
|
|
1972
|
-
.flatMap(line => line.split(/\s+/))
|
|
1973
|
-
.map(ref => ref.trim())
|
|
1974
|
-
.filter(ref => ref)
|
|
1975
|
-
.map(ref => {
|
|
1976
|
-
// Ensure proper Message-ID format
|
|
1977
|
-
if (!/^</.test(ref)) {
|
|
1978
|
-
ref = '<' + ref;
|
|
1979
|
-
}
|
|
1980
|
-
if (!/>$/.test(ref)) {
|
|
1981
|
-
ref = ref + '>';
|
|
1982
|
-
}
|
|
1983
|
-
return ref;
|
|
1984
|
-
});
|
|
1985
|
-
|
|
1986
|
-
references = Array.from(new Set(references));
|
|
1986
|
+
let references = buildReferences(referencedMessage);
|
|
1987
1987
|
if (references.length) {
|
|
1988
1988
|
if (!data.headers) {
|
|
1989
1989
|
data.headers = {};
|
|
@@ -2158,7 +2158,7 @@ class BaseClient {
|
|
|
2158
2158
|
// Generate raw MIME message
|
|
2159
2159
|
let { raw, emailObject, messageId } = await getRawEmail(data, null, options);
|
|
2160
2160
|
|
|
2161
|
-
return { raw, emailObject, messageId,
|
|
2161
|
+
return { raw, emailObject, messageId, referencedMessage };
|
|
2162
2162
|
}
|
|
2163
2163
|
|
|
2164
2164
|
/**
|
|
@@ -2300,90 +2300,64 @@ class BaseClient {
|
|
|
2300
2300
|
}
|
|
2301
2301
|
|
|
2302
2302
|
let referencedMessage;
|
|
2303
|
-
let documentStoreUsed = false;
|
|
2304
2303
|
|
|
2305
2304
|
// Resolve reference and update reference/in-reply-to headers
|
|
2306
2305
|
if (data.reference && data.reference.message) {
|
|
2307
|
-
//
|
|
2308
|
-
|
|
2309
|
-
try {
|
|
2310
|
-
referencedMessage = await this.accountObject.getMessage(data.reference.message, {
|
|
2311
|
-
documentStore: true,
|
|
2312
|
-
textType: '*'
|
|
2313
|
-
});
|
|
2314
|
-
} catch (err) {
|
|
2315
|
-
if (err.meta && err.meta.statusCode === 404) {
|
|
2316
|
-
// not found
|
|
2317
|
-
} else {
|
|
2318
|
-
let error = new Error('ElasticSearch request failed');
|
|
2319
|
-
error.info = {
|
|
2320
|
-
response: (err.meta && err.meta.body) || err.message,
|
|
2321
|
-
statusCode: err.meta && err.meta.statusCode
|
|
2322
|
-
};
|
|
2323
|
-
error.code = 'ESRequestError';
|
|
2324
|
-
error.statusCode = (err.meta && err.meta.statusCode) || 500;
|
|
2325
|
-
throw error;
|
|
2326
|
-
}
|
|
2327
|
-
}
|
|
2328
|
-
documentStoreUsed = true;
|
|
2329
|
-
} else {
|
|
2330
|
-
// Fetch from IMAP/API with retry logic for transient errors
|
|
2331
|
-
this.checkIMAPConnection(connectionOptions);
|
|
2306
|
+
// Fetch from IMAP/API with retry logic for transient errors
|
|
2307
|
+
this.checkIMAPConnection(connectionOptions);
|
|
2332
2308
|
|
|
2333
|
-
|
|
2309
|
+
let extendedData = data.reference.inline || data.reference.forwardAttachments;
|
|
2334
2310
|
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2338
|
-
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2349
|
-
|
|
2350
|
-
|
|
2351
|
-
|
|
2352
|
-
|
|
2353
|
-
|
|
2354
|
-
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
|
|
2358
|
-
logger: this.logger
|
|
2359
|
-
}
|
|
2360
|
-
);
|
|
2361
|
-
} catch (err) {
|
|
2362
|
-
// Check if this is a transient error that exhausted retries
|
|
2363
|
-
const isTransient = isTransientError(err);
|
|
2364
|
-
|
|
2365
|
-
if (isTransient && data.reference.ignoreMissing) {
|
|
2366
|
-
// Transient error but ignoreMissing is true - log and continue without reference
|
|
2367
|
-
this.logger.warn({
|
|
2368
|
-
msg: 'Failed to fetch referenced message due to transient error, continuing without reference',
|
|
2369
|
-
messageId: data.reference.message,
|
|
2370
|
-
err,
|
|
2371
|
-
statusCode: err.statusCode || (err.oauthRequest && err.oauthRequest.status)
|
|
2372
|
-
});
|
|
2373
|
-
referencedMessage = null;
|
|
2374
|
-
} else if (!isTransient && err.statusCode === 404) {
|
|
2375
|
-
// Permanent 404 error - respect ignoreMissing
|
|
2376
|
-
if (!data.reference.ignoreMissing) {
|
|
2377
|
-
let error = new Error('Referenced message was not found');
|
|
2378
|
-
error.code = 'ReferenceNotFound';
|
|
2379
|
-
error.statusCode = 404;
|
|
2380
|
-
throw error;
|
|
2381
|
-
}
|
|
2382
|
-
referencedMessage = null;
|
|
2383
|
-
} else {
|
|
2384
|
-
// Either transient error with ignoreMissing=false, or other permanent error
|
|
2385
|
-
throw err;
|
|
2311
|
+
try {
|
|
2312
|
+
referencedMessage = await retryOnTransientError(
|
|
2313
|
+
async () => {
|
|
2314
|
+
return await this.getMessage(
|
|
2315
|
+
data.reference.message,
|
|
2316
|
+
{
|
|
2317
|
+
fields: !extendedData
|
|
2318
|
+
? {
|
|
2319
|
+
uid: true,
|
|
2320
|
+
flags: true,
|
|
2321
|
+
envelope: true,
|
|
2322
|
+
headers: ['references']
|
|
2323
|
+
}
|
|
2324
|
+
: false,
|
|
2325
|
+
textType: extendedData ? '*' : false
|
|
2326
|
+
},
|
|
2327
|
+
connectionOptions
|
|
2328
|
+
);
|
|
2329
|
+
},
|
|
2330
|
+
{
|
|
2331
|
+
maxAttempts: 3,
|
|
2332
|
+
baseDelay: 1000,
|
|
2333
|
+
logger: this.logger
|
|
2386
2334
|
}
|
|
2335
|
+
);
|
|
2336
|
+
} catch (err) {
|
|
2337
|
+
// Check if this is a transient error that exhausted retries
|
|
2338
|
+
const isTransient = isTransientError(err);
|
|
2339
|
+
|
|
2340
|
+
if (isTransient && data.reference.ignoreMissing) {
|
|
2341
|
+
// Transient error but ignoreMissing is true - log and continue without reference
|
|
2342
|
+
this.logger.warn({
|
|
2343
|
+
msg: 'Failed to fetch referenced message due to transient error, continuing without reference',
|
|
2344
|
+
messageId: data.reference.message,
|
|
2345
|
+
err,
|
|
2346
|
+
statusCode: err.statusCode || (err.oauthRequest && err.oauthRequest.status)
|
|
2347
|
+
});
|
|
2348
|
+
referencedMessage = null;
|
|
2349
|
+
} else if (!isTransient && err.statusCode === 404) {
|
|
2350
|
+
// Permanent 404 error - respect ignoreMissing
|
|
2351
|
+
if (!data.reference.ignoreMissing) {
|
|
2352
|
+
let error = new Error('Referenced message was not found');
|
|
2353
|
+
error.code = 'ReferenceNotFound';
|
|
2354
|
+
error.statusCode = 404;
|
|
2355
|
+
throw error;
|
|
2356
|
+
}
|
|
2357
|
+
referencedMessage = null;
|
|
2358
|
+
} else {
|
|
2359
|
+
// Either transient error with ignoreMissing=false, or other permanent error
|
|
2360
|
+
throw err;
|
|
2387
2361
|
}
|
|
2388
2362
|
}
|
|
2389
2363
|
|
|
@@ -2404,25 +2378,7 @@ class BaseClient {
|
|
|
2404
2378
|
}
|
|
2405
2379
|
|
|
2406
2380
|
// Build references header chain
|
|
2407
|
-
let references =
|
|
2408
|
-
.concat(referencedMessage.messageId || [])
|
|
2409
|
-
.concat(referencedMessage.inReplyTo || [])
|
|
2410
|
-
.concat((referencedMessage.headers && referencedMessage.headers.references) || [])
|
|
2411
|
-
.flatMap(line => line.split(/\s+/))
|
|
2412
|
-
.map(ref => ref.trim())
|
|
2413
|
-
.filter(ref => ref)
|
|
2414
|
-
.map(ref => {
|
|
2415
|
-
// Ensure proper Message-ID format
|
|
2416
|
-
if (!/^</.test(ref)) {
|
|
2417
|
-
ref = '<' + ref;
|
|
2418
|
-
}
|
|
2419
|
-
if (!/>$/.test(ref)) {
|
|
2420
|
-
ref = ref + '>';
|
|
2421
|
-
}
|
|
2422
|
-
return ref;
|
|
2423
|
-
});
|
|
2424
|
-
|
|
2425
|
-
references = Array.from(new Set(references));
|
|
2381
|
+
let references = buildReferences(referencedMessage);
|
|
2426
2382
|
if (references.length) {
|
|
2427
2383
|
if (!data.headers) {
|
|
2428
2384
|
data.headers = {};
|
|
@@ -2611,7 +2567,6 @@ class BaseClient {
|
|
|
2611
2567
|
response.reference = {};
|
|
2612
2568
|
if (data.reference.message) {
|
|
2613
2569
|
response.reference.message = data.reference.message;
|
|
2614
|
-
response.reference.documentStore = documentStoreUsed;
|
|
2615
2570
|
response.reference.success = referencedMessage ? true : false;
|
|
2616
2571
|
if (!referencedMessage) {
|
|
2617
2572
|
response.reference.error = 'Referenced message was not found';
|
|
@@ -2631,11 +2586,9 @@ class BaseClient {
|
|
|
2631
2586
|
let gatewayData;
|
|
2632
2587
|
if (gateway) {
|
|
2633
2588
|
let gatewayObject = new Gateway({ gateway, redis: this.redis, secret: this.secret });
|
|
2634
|
-
|
|
2635
|
-
|
|
2636
|
-
|
|
2637
|
-
this.logger.warn({ msg: 'Failed to load gateway data', envelope, messageId, gateway, err });
|
|
2638
|
-
}
|
|
2589
|
+
// never fall through to the account's own SMTP server when a gateway was requested:
|
|
2590
|
+
// a missing gateway throws GatewayNotFound (404)
|
|
2591
|
+
gatewayData = await gatewayObject.loadGatewayData();
|
|
2639
2592
|
}
|
|
2640
2593
|
|
|
2641
2594
|
// Verify SMTP capability
|
|
@@ -2682,7 +2635,6 @@ class BaseClient {
|
|
|
2682
2635
|
response.reference = {};
|
|
2683
2636
|
if (data.reference.message) {
|
|
2684
2637
|
response.reference.message = data.reference.message;
|
|
2685
|
-
response.reference.documentStore = documentStoreUsed;
|
|
2686
2638
|
response.reference.success = referencedMessage ? true : false;
|
|
2687
2639
|
if (!referencedMessage) {
|
|
2688
2640
|
response.reference.error = 'Referenced message was not found';
|
|
@@ -2856,11 +2808,9 @@ class BaseClient {
|
|
|
2856
2808
|
let gatewayData;
|
|
2857
2809
|
if (gateway) {
|
|
2858
2810
|
let gatewayObject = new Gateway({ gateway, redis: this.redis, secret: this.secret });
|
|
2859
|
-
|
|
2860
|
-
|
|
2861
|
-
|
|
2862
|
-
this.logger.warn({ msg: 'Failed to load gateway data', envelope, messageId, gateway, err });
|
|
2863
|
-
}
|
|
2811
|
+
// never fall through to the account's own SMTP server when a gateway was requested:
|
|
2812
|
+
// a missing gateway throws GatewayNotFound (404)
|
|
2813
|
+
gatewayData = await gatewayObject.loadGatewayData();
|
|
2864
2814
|
}
|
|
2865
2815
|
|
|
2866
2816
|
// Verify SMTP capability
|
|
@@ -2990,6 +2940,12 @@ class BaseClient {
|
|
|
2990
2940
|
* Gets message fetch options based on settings
|
|
2991
2941
|
* @returns {Object} Fetch options for IMAP message retrieval
|
|
2992
2942
|
*/
|
|
2943
|
+
// The provider behind this client, for the AI summary's authentication trust rule: the Gmail
|
|
2944
|
+
// and Outlook API clients each set their type, and nothing else is reachable through an API
|
|
2945
|
+
summaryProvider() {
|
|
2946
|
+
return summaryProvider({ isGmail: this.type === 'gmail', isOutlook: this.type === 'outlook' });
|
|
2947
|
+
}
|
|
2948
|
+
|
|
2993
2949
|
async getMessageFetchOptions() {
|
|
2994
2950
|
let messageFetchOptions = {};
|
|
2995
2951
|
|
|
@@ -3010,24 +2966,11 @@ class BaseClient {
|
|
|
3010
2966
|
messageFetchOptions.headers = notifyHeaders.includes('*') ? true : notifyHeaders.length ? notifyHeaders : false;
|
|
3011
2967
|
}
|
|
3012
2968
|
|
|
3013
|
-
//
|
|
2969
|
+
// Also request the headers this path needs for its own processing, and the summary's
|
|
2970
|
+
// while summaries are on. Skipped when the setting already asks for every header
|
|
3014
2971
|
if (messageFetchOptions.headers !== true) {
|
|
3015
|
-
|
|
3016
|
-
|
|
3017
|
-
// Headers for auto-reply detection
|
|
3018
|
-
fetchHeaders.add('x-autoreply');
|
|
3019
|
-
fetchHeaders.add('x-autorespond');
|
|
3020
|
-
fetchHeaders.add('auto-submitted');
|
|
3021
|
-
fetchHeaders.add('precedence');
|
|
3022
|
-
|
|
3023
|
-
// Headers for threading
|
|
3024
|
-
fetchHeaders.add('in-reply-to');
|
|
3025
|
-
fetchHeaders.add('references');
|
|
3026
|
-
|
|
3027
|
-
// Content type for detecting multipart/report
|
|
3028
|
-
fetchHeaders.add('content-type');
|
|
3029
|
-
|
|
3030
|
-
messageFetchOptions.fetchHeaders = Array.from(fetchHeaders);
|
|
2972
|
+
const summaryHeaders = (await llmPreProcess.getPreProcessHandler()) ? summaryFetchHeaders(this.summaryProvider()) : [];
|
|
2973
|
+
messageFetchOptions.fetchHeaders = notificationHeaderFields(messageFetchOptions.headers, summaryHeaders);
|
|
3031
2974
|
}
|
|
3032
2975
|
|
|
3033
2976
|
return messageFetchOptions;
|
|
@@ -3094,7 +3037,7 @@ class BaseClient {
|
|
|
3094
3037
|
try {
|
|
3095
3038
|
let raw = await this.getRawMessage(messageData.id);
|
|
3096
3039
|
|
|
3097
|
-
let parsed = await
|
|
3040
|
+
let parsed = await parseMessage(raw, DSN_PARSER_OPTIONS);
|
|
3098
3041
|
if (parsed) {
|
|
3099
3042
|
content = { parsed };
|
|
3100
3043
|
applyDeliveryReport(parsed, messageData, this.logger);
|
|
@@ -3152,7 +3095,7 @@ class BaseClient {
|
|
|
3152
3095
|
}
|
|
3153
3096
|
}
|
|
3154
3097
|
|
|
3155
|
-
if (bounce
|
|
3098
|
+
if (isFailedDelivery(bounce)) {
|
|
3156
3099
|
bounceNotifyInfo = Object.assign({ bounceMessage: messageData.id }, bounce);
|
|
3157
3100
|
|
|
3158
3101
|
messageData.isBounce = true;
|
|
@@ -3222,22 +3165,9 @@ class BaseClient {
|
|
|
3222
3165
|
if (notifyCalendarEvents && messageData.attachments && messageData.attachments.length) {
|
|
3223
3166
|
let calendarEventMap = new Map();
|
|
3224
3167
|
|
|
3225
|
-
//
|
|
3226
|
-
let sortCalendarAttachments = (a, b) => {
|
|
3227
|
-
if (a.contentType !== b.contentType) {
|
|
3228
|
-
if (a.contentType === 'text/calendar') {
|
|
3229
|
-
return -1;
|
|
3230
|
-
}
|
|
3231
|
-
if (b.contentType === 'text/calendar') {
|
|
3232
|
-
return 1;
|
|
3233
|
-
}
|
|
3234
|
-
}
|
|
3235
|
-
return a.contentType.localeCompare(b.contentType);
|
|
3236
|
-
};
|
|
3237
|
-
|
|
3238
|
-
// Process calendar attachments
|
|
3168
|
+
// Process calendar attachments, text/calendar before application/ics
|
|
3239
3169
|
for (let attachment of [...messageData.attachments].sort(sortCalendarAttachments)) {
|
|
3240
|
-
if (
|
|
3170
|
+
if (CALENDAR_CONTENT_TYPES.includes(attachment.contentType)) {
|
|
3241
3171
|
if (!attachment.content) {
|
|
3242
3172
|
try {
|
|
3243
3173
|
let calendarBuf = (await this.getAttachment(attachment.id))?.data;
|
|
@@ -3249,33 +3179,15 @@ class BaseClient {
|
|
|
3249
3179
|
if (attachment.content) {
|
|
3250
3180
|
let contentBuf = Buffer.from(attachment.content, 'base64');
|
|
3251
3181
|
try {
|
|
3252
|
-
|
|
3253
|
-
|
|
3254
|
-
|
|
3255
|
-
const comp = new ical.Component(jcalData);
|
|
3256
|
-
if (!comp) {
|
|
3257
|
-
continue;
|
|
3258
|
-
}
|
|
3259
|
-
|
|
3260
|
-
const vevent = comp.getFirstSubcomponent('vevent');
|
|
3261
|
-
if (!vevent) {
|
|
3262
|
-
continue;
|
|
3263
|
-
}
|
|
3264
|
-
|
|
3265
|
-
// Extract method (REQUEST, CANCEL, etc.)
|
|
3266
|
-
let eventMethodProp = comp.getFirstProperty('method');
|
|
3267
|
-
let eventMethodValue = eventMethodProp ? eventMethodProp.getFirstValue() : null;
|
|
3268
|
-
|
|
3269
|
-
const event = new ical.Event(vevent);
|
|
3270
|
-
|
|
3271
|
-
if (!event || !event.uid) {
|
|
3182
|
+
const event = parseCalendarEvent(contentBuf);
|
|
3183
|
+
if (!event) {
|
|
3272
3184
|
continue;
|
|
3273
3185
|
}
|
|
3274
3186
|
|
|
3275
3187
|
// Skip if we already processed this event
|
|
3276
|
-
if (calendarEventMap.has(event.
|
|
3188
|
+
if (calendarEventMap.has(event.eventId)) {
|
|
3277
3189
|
if (attachment.filename) {
|
|
3278
|
-
let existingEntry = calendarEventMap.get(event.
|
|
3190
|
+
let existingEntry = calendarEventMap.get(event.eventId);
|
|
3279
3191
|
if (!existingEntry.filename) {
|
|
3280
3192
|
// inject filename
|
|
3281
3193
|
existingEntry.filename = attachment.filename;
|
|
@@ -3284,31 +3196,20 @@ class BaseClient {
|
|
|
3284
3196
|
continue;
|
|
3285
3197
|
}
|
|
3286
3198
|
|
|
3287
|
-
// Extract timezone information
|
|
3288
|
-
let timezone;
|
|
3289
|
-
const vtz = comp.getFirstSubcomponent('vtimezone');
|
|
3290
|
-
if (vtz) {
|
|
3291
|
-
const tz = new ical.Timezone(vtz);
|
|
3292
|
-
timezone = tz && tz.tzid;
|
|
3293
|
-
}
|
|
3294
|
-
|
|
3295
|
-
let startDate = event.startDate && event.startDate.toJSDate();
|
|
3296
|
-
let endDate = event.endDate && event.endDate.toJSDate();
|
|
3297
|
-
|
|
3298
3199
|
// Store calendar event data
|
|
3299
3200
|
calendarEventMap.set(
|
|
3300
|
-
event.
|
|
3201
|
+
event.eventId,
|
|
3301
3202
|
filterEmptyObjectValues({
|
|
3302
|
-
eventId: event.
|
|
3203
|
+
eventId: event.eventId,
|
|
3303
3204
|
attachment: attachment.id,
|
|
3304
|
-
method: attachment.method ||
|
|
3205
|
+
method: attachment.method || event.method || null,
|
|
3305
3206
|
|
|
3306
|
-
summary: event.summary
|
|
3307
|
-
description: event.description
|
|
3308
|
-
timezone: timezone
|
|
3309
|
-
startDate: startDate
|
|
3310
|
-
endDate: endDate
|
|
3311
|
-
organizer: event.organizer
|
|
3207
|
+
summary: event.summary,
|
|
3208
|
+
description: event.description,
|
|
3209
|
+
timezone: event.timezone,
|
|
3210
|
+
startDate: event.startDate,
|
|
3211
|
+
endDate: event.endDate,
|
|
3212
|
+
organizer: event.organizer,
|
|
3312
3213
|
|
|
3313
3214
|
filename: attachment.filename,
|
|
3314
3215
|
contentType: attachment.contentType,
|
|
@@ -3346,96 +3247,19 @@ class BaseClient {
|
|
|
3346
3247
|
}
|
|
3347
3248
|
}
|
|
3348
3249
|
|
|
3349
|
-
// Process with AI features for inbox messages
|
|
3350
|
-
|
|
3250
|
+
// Process with AI features for inbox messages. A message with no text part has no text
|
|
3251
|
+
// object at all, and nothing to summarize
|
|
3252
|
+
if (messageData.messageSpecialUse === '\\Inbox' && (messageData.text?.plain || messageData.text?.html)) {
|
|
3351
3253
|
let llmMessageData = Object.assign({ account: this.account }, messageData);
|
|
3352
3254
|
|
|
3353
|
-
|
|
3354
|
-
|
|
3355
|
-
|
|
3356
|
-
|
|
3357
|
-
|
|
3358
|
-
|
|
3359
|
-
|
|
3360
|
-
|
|
3361
|
-
cmd: 'generateSummary',
|
|
3362
|
-
data: {
|
|
3363
|
-
message: {
|
|
3364
|
-
headers: Object.keys(messageData.headers || {}).map(key => ({ key, value: [].concat(messageData.headers[key] || []) })),
|
|
3365
|
-
attachments: messageData.attachments,
|
|
3366
|
-
from: messageData.from,
|
|
3367
|
-
subject: messageData.subject,
|
|
3368
|
-
text: messageData.text.plain,
|
|
3369
|
-
html: messageData.text.html
|
|
3370
|
-
},
|
|
3371
|
-
account: this.account
|
|
3372
|
-
},
|
|
3373
|
-
timeout: 2 * 60 * 1000
|
|
3374
|
-
});
|
|
3375
|
-
|
|
3376
|
-
if (messageData.summary) {
|
|
3377
|
-
// Clean up summary output
|
|
3378
|
-
for (let key of Object.keys(messageData.summary)) {
|
|
3379
|
-
// remove meta keys from output
|
|
3380
|
-
if (key.charAt(0) === '_' || messageData.summary[key] === '') {
|
|
3381
|
-
delete messageData.summary[key];
|
|
3382
|
-
}
|
|
3383
|
-
if (key === 'riskAssessment') {
|
|
3384
|
-
messageData.riskAssessment = messageData.summary.riskAssessment;
|
|
3385
|
-
delete messageData.summary.riskAssessment;
|
|
3386
|
-
}
|
|
3387
|
-
}
|
|
3388
|
-
|
|
3389
|
-
this.logger.trace({ msg: 'Fetched summary from OpenAI', summary: messageData.summary });
|
|
3390
|
-
}
|
|
3391
|
-
|
|
3392
|
-
await this.redis.del(`${REDIS_PREFIX}:openai:error`);
|
|
3393
|
-
} catch (err) {
|
|
3394
|
-
// Track OpenAI errors
|
|
3395
|
-
await this.redis.set(
|
|
3396
|
-
`${REDIS_PREFIX}:openai:error`,
|
|
3397
|
-
JSON.stringify({
|
|
3398
|
-
message: err.message,
|
|
3399
|
-
code: err.code,
|
|
3400
|
-
statusCode: err.statusCode,
|
|
3401
|
-
created: Date.now()
|
|
3402
|
-
})
|
|
3403
|
-
);
|
|
3404
|
-
this.logger.error({ msg: 'Failed to fetch summary from OpenAI', err });
|
|
3405
|
-
}
|
|
3406
|
-
}
|
|
3407
|
-
|
|
3408
|
-
// Generate embeddings for semantic search
|
|
3409
|
-
if (canUseLLM.generateEmbeddings) {
|
|
3410
|
-
try {
|
|
3411
|
-
messageData.embeddings = await this.call({
|
|
3412
|
-
cmd: 'generateEmbeddings',
|
|
3413
|
-
data: {
|
|
3414
|
-
message: {
|
|
3415
|
-
headers: Object.keys(messageData.headers || {}).map(key => ({ key, value: [].concat(messageData.headers[key] || []) })),
|
|
3416
|
-
attachments: messageData.attachments,
|
|
3417
|
-
from: messageData.from,
|
|
3418
|
-
subject: messageData.subject,
|
|
3419
|
-
text: messageData.text.plain,
|
|
3420
|
-
html: messageData.text.html
|
|
3421
|
-
},
|
|
3422
|
-
account: this.account
|
|
3423
|
-
},
|
|
3424
|
-
timeout: 2 * 60 * 1000
|
|
3425
|
-
});
|
|
3426
|
-
} catch (err) {
|
|
3427
|
-
await this.redis.set(
|
|
3428
|
-
`${REDIS_PREFIX}:openai:error`,
|
|
3429
|
-
JSON.stringify({
|
|
3430
|
-
message: err.message,
|
|
3431
|
-
code: err.code,
|
|
3432
|
-
statusCode: err.statusCode,
|
|
3433
|
-
time: Date.now()
|
|
3434
|
-
})
|
|
3435
|
-
);
|
|
3436
|
-
this.logger.error({ msg: 'Failed to fetch embeddings from OpenAI', err });
|
|
3437
|
-
}
|
|
3438
|
-
}
|
|
3255
|
+
if (await llmPreProcess.run(llmMessageData)) {
|
|
3256
|
+
await llmPreProcess.summarize(messageData, {
|
|
3257
|
+
call: this.call,
|
|
3258
|
+
redis: this.redis,
|
|
3259
|
+
account: this.account,
|
|
3260
|
+
provider: this.summaryProvider(),
|
|
3261
|
+
logger: this.logger
|
|
3262
|
+
});
|
|
3439
3263
|
}
|
|
3440
3264
|
}
|
|
3441
3265
|
|
|
@@ -3454,11 +3278,14 @@ class BaseClient {
|
|
|
3454
3278
|
for (let attachment of messageData.attachments) {
|
|
3455
3279
|
let contentId = attachment.contentId && attachment.contentId.replace(/^<|>$/g, '');
|
|
3456
3280
|
if (contentId && cidReferences.has(contentId)) {
|
|
3457
|
-
|
|
3458
|
-
|
|
3459
|
-
|
|
3460
|
-
|
|
3461
|
-
|
|
3281
|
+
// An attachment the earlier inline pass already loaded is embedded as is; the
|
|
3282
|
+
// same size cap applies to anything downloaded here
|
|
3283
|
+
if (!attachment.content && !(attachment.encodedSize && attachment.encodedSize > MAX_INLINE_ATTACHMENT_SIZE)) {
|
|
3284
|
+
try {
|
|
3285
|
+
attachment.content = (await this.getAttachment(attachment.id))?.data?.toString('base64');
|
|
3286
|
+
} catch (err) {
|
|
3287
|
+
this.logger.error({ msg: 'Failed to load attachment content', attachment, err });
|
|
3288
|
+
}
|
|
3462
3289
|
}
|
|
3463
3290
|
|
|
3464
3291
|
attachmentList.set(contentId, {
|
|
@@ -3468,35 +3295,32 @@ class BaseClient {
|
|
|
3468
3295
|
}
|
|
3469
3296
|
}
|
|
3470
3297
|
|
|
3471
|
-
// Replace CID references with data URIs
|
|
3298
|
+
// Replace CID references with data URIs. The web safe copy is what webhooks deliver
|
|
3299
|
+
// in place of the HTML, so it gets the same treatment
|
|
3472
3300
|
if (attachmentList.size) {
|
|
3473
|
-
|
|
3474
|
-
|
|
3475
|
-
let { attachment, content } = attachmentList.get(
|
|
3476
|
-
|
|
3477
|
-
|
|
3478
|
-
|
|
3479
|
-
|
|
3480
|
-
|
|
3481
|
-
|
|
3301
|
+
const embedImages = html =>
|
|
3302
|
+
embedCidImages(html, contentId => {
|
|
3303
|
+
let { attachment, content } = attachmentList.get(contentId) || {};
|
|
3304
|
+
return content && { contentType: attachment.contentType, content };
|
|
3305
|
+
});
|
|
3306
|
+
|
|
3307
|
+
messageData.text.html = embedImages(messageData.text.html);
|
|
3308
|
+
if (messageData.text._generatedHtml) {
|
|
3309
|
+
messageData.text._generatedHtml = embedImages(messageData.text._generatedHtml);
|
|
3310
|
+
}
|
|
3482
3311
|
}
|
|
3483
3312
|
}
|
|
3484
3313
|
|
|
3485
|
-
messageData.text.
|
|
3314
|
+
if (messageData.text._generatedHtml) {
|
|
3315
|
+
messageData.text.webSafe = true;
|
|
3316
|
+
}
|
|
3486
3317
|
}
|
|
3487
3318
|
|
|
3488
|
-
//
|
|
3489
|
-
//
|
|
3490
|
-
|
|
3491
|
-
|
|
3492
|
-
|
|
3493
|
-
if (requestedHeaders.includes(key)) {
|
|
3494
|
-
filteredHeaders[key] = messageData.headers[key];
|
|
3495
|
-
}
|
|
3496
|
-
}
|
|
3497
|
-
messageData.headers = filteredHeaders;
|
|
3498
|
-
} else if (options.headers && requestedHeaders === false) {
|
|
3499
|
-
delete messageData.headers;
|
|
3319
|
+
// Narrow the headers to what the caller asked for. This runs last: the checks and the AI
|
|
3320
|
+
// summary above read headers fetched for them (see notificationHeaderFields()) that the
|
|
3321
|
+
// published payload may not carry
|
|
3322
|
+
if (options.headers) {
|
|
3323
|
+
narrowHeaders(messageData, requestedHeaders);
|
|
3500
3324
|
}
|
|
3501
3325
|
|
|
3502
3326
|
let path = messageData.path || this.path;
|
|
@@ -3613,7 +3437,10 @@ class BaseClient {
|
|
|
3613
3437
|
parsedContentType &&
|
|
3614
3438
|
parsedContentType.value &&
|
|
3615
3439
|
parsedContentType.value.toLowerCase().trim() === 'multipart/report' &&
|
|
3616
|
-
|
|
3440
|
+
// parameter values are case-insensitive for this one (RFC 6522)
|
|
3441
|
+
String(parsedContentType.params['report-type'] || '')
|
|
3442
|
+
.toLowerCase()
|
|
3443
|
+
.trim() === 'delivery-status'
|
|
3617
3444
|
) {
|
|
3618
3445
|
return true;
|
|
3619
3446
|
}
|
|
@@ -3769,6 +3596,10 @@ class BaseClient {
|
|
|
3769
3596
|
// Get SMTP transport from pool
|
|
3770
3597
|
const transporter = getMailTransport(smtpSettings);
|
|
3771
3598
|
|
|
3599
|
+
// Only the send itself is inside this try. Once the server has accepted the message a
|
|
3600
|
+
// failure is a bookkeeping failure: rethrowing it made the submit worker retry (and so
|
|
3601
|
+
// deliver) an already delivered message, and reported a messageDeliveryError for it
|
|
3602
|
+
let info;
|
|
3772
3603
|
try {
|
|
3773
3604
|
// Update job progress
|
|
3774
3605
|
try {
|
|
@@ -3778,104 +3609,116 @@ class BaseClient {
|
|
|
3778
3609
|
}
|
|
3779
3610
|
|
|
3780
3611
|
// Send the message
|
|
3781
|
-
|
|
3612
|
+
info = await transporter.sendMail({
|
|
3782
3613
|
envelope,
|
|
3783
3614
|
messageId,
|
|
3784
3615
|
raw: !hasBcc ? raw : await removeBcc(raw),
|
|
3785
3616
|
dsn: data.dsn || null
|
|
3786
3617
|
});
|
|
3618
|
+
} catch (err) {
|
|
3619
|
+
await this.handleSubmitError(err, {
|
|
3620
|
+
smtpSettings,
|
|
3621
|
+
networkRouting,
|
|
3622
|
+
gatewayData,
|
|
3623
|
+
gatewayObject,
|
|
3624
|
+
data,
|
|
3625
|
+
jobData,
|
|
3626
|
+
queueId,
|
|
3627
|
+
envelope
|
|
3628
|
+
});
|
|
3629
|
+
throw err;
|
|
3630
|
+
}
|
|
3787
3631
|
|
|
3788
|
-
|
|
3789
|
-
|
|
3632
|
+
// Store EHLO response for debugging
|
|
3633
|
+
if (info.ehlo) {
|
|
3634
|
+
try {
|
|
3790
3635
|
await this.redis.hSetExists(this.getAccountKey(), 'smtpServerEhlo', JSON.stringify(info.ehlo));
|
|
3636
|
+
} catch (err) {
|
|
3637
|
+
this.logger.error({ msg: 'Failed to store the SMTP server EHLO response', queueId, messageId, err });
|
|
3791
3638
|
}
|
|
3639
|
+
}
|
|
3792
3640
|
|
|
3793
|
-
|
|
3794
|
-
|
|
3641
|
+
// Handle provider-specific message ID extraction
|
|
3642
|
+
const originalMessageId = ProviderMessageIdHandler.processResponse(info, smtpSettings.host);
|
|
3795
3643
|
|
|
3796
|
-
|
|
3797
|
-
|
|
3798
|
-
|
|
3799
|
-
|
|
3800
|
-
|
|
3801
|
-
|
|
3802
|
-
|
|
3803
|
-
|
|
3804
|
-
|
|
3805
|
-
|
|
3806
|
-
|
|
3644
|
+
// Update job progress
|
|
3645
|
+
try {
|
|
3646
|
+
await submitJobEntry.updateProgress({
|
|
3647
|
+
status: 'smtp-completed',
|
|
3648
|
+
response: info.response,
|
|
3649
|
+
messageId: info.messageId,
|
|
3650
|
+
originalMessageId
|
|
3651
|
+
});
|
|
3652
|
+
} catch (err) {
|
|
3653
|
+
// ignore
|
|
3654
|
+
}
|
|
3807
3655
|
|
|
3808
|
-
|
|
3656
|
+
// Send success notification
|
|
3657
|
+
try {
|
|
3809
3658
|
await this.notify(
|
|
3810
3659
|
false,
|
|
3811
3660
|
EMAIL_SENT_NOTIFY,
|
|
3812
3661
|
NotificationBuilder.buildSuccessPayload({ info, originalMessageId, queueId, envelope, networkRouting })
|
|
3813
3662
|
);
|
|
3663
|
+
} catch (err) {
|
|
3664
|
+
this.logger.error({ msg: 'Failed to queue the messageSent notification', queueId, messageId, err });
|
|
3665
|
+
}
|
|
3814
3666
|
|
|
3815
|
-
|
|
3816
|
-
|
|
3817
|
-
|
|
3818
|
-
|
|
3819
|
-
|
|
3820
|
-
|
|
3821
|
-
|
|
3822
|
-
// Handle sent mail copy to folder
|
|
3823
|
-
const copyDeciderInputs = {
|
|
3824
|
-
accountData,
|
|
3825
|
-
data,
|
|
3826
|
-
isGmail: this.isGmail,
|
|
3827
|
-
isOutlook: this.isOutlook,
|
|
3828
|
-
gatewayData
|
|
3829
|
-
};
|
|
3830
|
-
const shouldCopy = SentMailCopyDecider.shouldCopy(copyDeciderInputs);
|
|
3831
|
-
|
|
3832
|
-
const connectionOptions = { allowSecondary: true };
|
|
3833
|
-
|
|
3834
|
-
const sentCopySucceeded = shouldCopy ? await this.uploadToSentFolder(raw, data, queueId, messageId, connectionOptions) : false;
|
|
3835
|
-
|
|
3836
|
-
// Remove the submitted draft. Delete it permanently when a copy of the message
|
|
3837
|
-
// exists in the Sent Mail folder - either uploaded above, or filed by the provider
|
|
3838
|
-
// itself (Gmail and Outlook store SMTP-sent messages on their own, which is also
|
|
3839
|
-
// why the upload is suppressed for them). Otherwise move the draft to Trash so the
|
|
3840
|
-
// content is not lost
|
|
3841
|
-
if (data.draft) {
|
|
3842
|
-
await this.cleanupSubmittedDraft(data.draft, {
|
|
3843
|
-
permanent: sentCopySucceeded || SentMailCopyDecider.providerSavesSentCopy(copyDeciderInputs),
|
|
3844
|
-
queueId,
|
|
3845
|
-
messageId,
|
|
3846
|
-
connectionOptions
|
|
3847
|
-
});
|
|
3848
|
-
}
|
|
3667
|
+
// Clear cached SMTP error
|
|
3668
|
+
try {
|
|
3669
|
+
await this.redis.hset(this.getAccountKey(), 'smtpStatus', JSON.stringify({ created: Date.now(), status: 'ok', response: info.response }));
|
|
3670
|
+
} catch (err) {
|
|
3671
|
+
// ignore
|
|
3672
|
+
}
|
|
3849
3673
|
|
|
3850
|
-
|
|
3851
|
-
|
|
3852
|
-
|
|
3853
|
-
|
|
3674
|
+
// Handle sent mail copy to folder
|
|
3675
|
+
const copyDeciderInputs = {
|
|
3676
|
+
accountData,
|
|
3677
|
+
data,
|
|
3678
|
+
isGmail: this.isGmail,
|
|
3679
|
+
isOutlook: this.isOutlook,
|
|
3680
|
+
gatewayData
|
|
3681
|
+
};
|
|
3682
|
+
const shouldCopy = SentMailCopyDecider.shouldCopy(copyDeciderInputs);
|
|
3854
3683
|
|
|
3855
|
-
|
|
3856
|
-
if (data.feedbackKey) {
|
|
3857
|
-
await this.updateFeedbackKey(data.feedbackKey, true);
|
|
3858
|
-
}
|
|
3684
|
+
const connectionOptions = { allowSecondary: true };
|
|
3859
3685
|
|
|
3860
|
-
|
|
3861
|
-
if (gatewayData) {
|
|
3862
|
-
await this.updateGatewayStats(gatewayObject, queueId, messageId, reference);
|
|
3863
|
-
}
|
|
3686
|
+
const sentCopySucceeded = shouldCopy ? await this.uploadToSentFolder(raw, data, queueId, messageId, connectionOptions) : false;
|
|
3864
3687
|
|
|
3865
|
-
|
|
3866
|
-
|
|
3867
|
-
|
|
3868
|
-
|
|
3869
|
-
|
|
3870
|
-
|
|
3871
|
-
|
|
3872
|
-
|
|
3873
|
-
jobData,
|
|
3688
|
+
// Remove the submitted draft. Delete it permanently when a copy of the message
|
|
3689
|
+
// exists in the Sent Mail folder - either uploaded above, or filed by the provider
|
|
3690
|
+
// itself (Gmail and Outlook store SMTP-sent messages on their own, which is also
|
|
3691
|
+
// why the upload is suppressed for them). Otherwise move the draft to Trash so the
|
|
3692
|
+
// content is not lost
|
|
3693
|
+
if (data.draft) {
|
|
3694
|
+
await this.cleanupSubmittedDraft(data.draft, {
|
|
3695
|
+
permanent: sentCopySucceeded || SentMailCopyDecider.providerSavesSentCopy(copyDeciderInputs),
|
|
3874
3696
|
queueId,
|
|
3875
|
-
|
|
3697
|
+
messageId,
|
|
3698
|
+
connectionOptions
|
|
3876
3699
|
});
|
|
3877
|
-
throw err;
|
|
3878
3700
|
}
|
|
3701
|
+
|
|
3702
|
+
// Update reference flags if needed
|
|
3703
|
+
if (reference && reference.update) {
|
|
3704
|
+
await this.updateReferenceFlags(reference, queueId, messageId, connectionOptions);
|
|
3705
|
+
}
|
|
3706
|
+
|
|
3707
|
+
// Update feedback key if provided
|
|
3708
|
+
if (data.feedbackKey) {
|
|
3709
|
+
try {
|
|
3710
|
+
await this.updateFeedbackKey(data.feedbackKey, true);
|
|
3711
|
+
} catch (err) {
|
|
3712
|
+
this.logger.error({ msg: 'Failed to update the submission feedback key', queueId, messageId, err });
|
|
3713
|
+
}
|
|
3714
|
+
}
|
|
3715
|
+
|
|
3716
|
+
// Update gateway usage stats
|
|
3717
|
+
if (gatewayData) {
|
|
3718
|
+
await this.updateGatewayStats(gatewayObject, queueId, messageId, reference);
|
|
3719
|
+
}
|
|
3720
|
+
|
|
3721
|
+
return { response: info.response, messageId: info.messageId };
|
|
3879
3722
|
}
|
|
3880
3723
|
|
|
3881
3724
|
/**
|
|
@@ -4012,8 +3855,10 @@ class BaseClient {
|
|
|
4012
3855
|
async handleSubmitError(err, context) {
|
|
4013
3856
|
const { smtpSettings, networkRouting, gatewayData, gatewayObject, data, jobData, queueId, envelope } = context;
|
|
4014
3857
|
|
|
4015
|
-
// Build SMTP status from error
|
|
4016
|
-
|
|
3858
|
+
// Build SMTP status from error. Only an SMTP attempt has one to report: a provider API send
|
|
3859
|
+
// (Gmail, Graph) reaches here with no smtpSettings, and recording a status for it would
|
|
3860
|
+
// describe an SMTP server the message never touched
|
|
3861
|
+
const smtpStatus = smtpSettings ? SmtpErrorBuilder.buildStatus(err, smtpSettings, networkRouting) : null;
|
|
4017
3862
|
|
|
4018
3863
|
if (smtpStatus) {
|
|
4019
3864
|
// Store SMTP error for the account
|
|
@@ -4074,6 +3919,8 @@ module.exports = {
|
|
|
4074
3919
|
BaseClient,
|
|
4075
3920
|
metricsMeta: postMetrics,
|
|
4076
3921
|
applyReferenceRecipients,
|
|
3922
|
+
buildReferences,
|
|
3923
|
+
isFailedDelivery,
|
|
4077
3924
|
markRejectedAccessToken,
|
|
4078
3925
|
renewRejectedAccessToken
|
|
4079
3926
|
};
|