@softure-ai/mailing 0.1.6 → 0.1.7
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/CHANGELOG.md +19 -0
- package/README.md +83 -18
- package/dist/cli/run.d.ts.map +1 -1
- package/dist/cli/run.js +46 -15
- package/dist/cli/run.js.map +1 -1
- package/dist/contract.d.ts +48 -8
- package/dist/contract.d.ts.map +1 -1
- package/dist/contract.js +2 -0
- package/dist/contract.js.map +1 -1
- package/dist/index.d.ts +12 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/messages/en.d.ts +2 -0
- package/dist/messages/en.d.ts.map +1 -1
- package/dist/messages/en.js +2 -0
- package/dist/messages/en.js.map +1 -1
- package/dist/messages/index.d.ts +4 -0
- package/dist/messages/index.d.ts.map +1 -1
- package/dist/messages/pl.d.ts.map +1 -1
- package/dist/messages/pl.js +2 -0
- package/dist/messages/pl.js.map +1 -1
- package/dist/next/actions.d.ts.map +1 -1
- package/dist/next/actions.js +6 -6
- package/dist/next/actions.js.map +1 -1
- package/dist/next/pages.d.ts.map +1 -1
- package/dist/next/pages.js +8 -6
- package/dist/next/pages.js.map +1 -1
- package/dist/next/route.d.ts +2 -1
- package/dist/next/route.d.ts.map +1 -1
- package/dist/next/route.js +6 -4
- package/dist/next/route.js.map +1 -1
- package/dist/options.d.ts +15 -1
- package/dist/options.d.ts.map +1 -1
- package/dist/options.js +32 -1
- package/dist/options.js.map +1 -1
- package/dist/providers/resend.d.ts.map +1 -1
- package/dist/providers/resend.js +12 -6
- package/dist/providers/resend.js.map +1 -1
- package/dist/schema.d.ts +17 -0
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +2 -0
- package/dist/schema.js.map +1 -1
- package/dist/server/campaigns.d.ts +30 -4
- package/dist/server/campaigns.d.ts.map +1 -1
- package/dist/server/campaigns.js +67 -12
- package/dist/server/campaigns.js.map +1 -1
- package/dist/server/deliveries.d.ts +30 -2
- package/dist/server/deliveries.d.ts.map +1 -1
- package/dist/server/deliveries.js +58 -23
- package/dist/server/deliveries.js.map +1 -1
- package/dist/server/index.d.ts +3 -3
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +2 -2
- package/dist/server/index.js.map +1 -1
- package/dist/server/send-mail.d.ts +3 -1
- package/dist/server/send-mail.d.ts.map +1 -1
- package/dist/server/send-mail.js +18 -6
- package/dist/server/send-mail.js.map +1 -1
- package/dist/server/suppressions.d.ts +8 -6
- package/dist/server/suppressions.d.ts.map +1 -1
- package/dist/server/suppressions.js +23 -7
- package/dist/server/suppressions.js.map +1 -1
- package/dist/server/unsubscribe-link.d.ts +23 -0
- package/dist/server/unsubscribe-link.d.ts.map +1 -1
- package/dist/server/unsubscribe-link.js +30 -1
- package/dist/server/unsubscribe-link.js.map +1 -1
- package/migrations/0003_add_delivery_provider_status.sql +13 -0
- package/module.json +1 -1
- package/package.json +1 -1
- package/src/cli/run.ts +61 -18
- package/src/contract.ts +49 -6
- package/src/index.ts +8 -3
- package/src/messages/en.ts +2 -0
- package/src/messages/pl.ts +2 -0
- package/src/next/actions.ts +5 -6
- package/src/next/pages.tsx +10 -7
- package/src/next/route.ts +6 -4
- package/src/options.ts +36 -2
- package/src/providers/resend.ts +11 -7
- package/src/schema.ts +2 -0
- package/src/server/campaigns.ts +95 -17
- package/src/server/deliveries.ts +89 -29
- package/src/server/index.ts +8 -0
- package/src/server/send-mail.ts +30 -6
- package/src/server/suppressions.ts +23 -8
- package/src/server/unsubscribe-link.ts +41 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type ModuleContext, type Result } from "@softure-ai/core";
|
|
2
2
|
import type { Queryable } from "@softure-ai/db";
|
|
3
3
|
import type { SuppressionSource, UnsubscribeErrorCode } from "../contract.js";
|
|
4
|
-
import { type Env, type UnsubscribeToken } from "./unsubscribe-link.js";
|
|
4
|
+
import { type Env, type UnsubscribeLink, type UnsubscribeToken } from "./unsubscribe-link.js";
|
|
5
5
|
export type SuppressionContext = ModuleContext<Queryable>;
|
|
6
6
|
/** Whether `address` unsubscribed. Throws on a database failure: callers decide how to fail. */
|
|
7
7
|
export declare function isSuppressed(ctx: Pick<SuppressionContext, "db">, address: string): Promise<boolean>;
|
|
@@ -18,10 +18,12 @@ export declare function suppressRecipient(ctx: SuppressionContext, address: stri
|
|
|
18
18
|
*/
|
|
19
19
|
export declare function liftSuppression(ctx: Pick<SuppressionContext, "db">, address: string): Promise<boolean>;
|
|
20
20
|
/**
|
|
21
|
-
* Unsubscribes the recipient of a
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
21
|
+
* Unsubscribes the recipient of a link and runs `onUnsubscribed` in the same transaction, also when
|
|
22
|
+
* the recipient had already unsubscribed (a retried one-click heals a missed hook). A signed link
|
|
23
|
+
* (or a bare token) is checked against the secrets before the database is touched; a legacy link
|
|
24
|
+
* goes to the app's `legacyUnsubscribe.verify`, which names the address. A link that does not
|
|
25
|
+
* verify is `mailing.invalid_link`. Throws on a database failure, or when `verify` or the hook
|
|
26
|
+
* throws (nothing is stored).
|
|
25
27
|
*/
|
|
26
|
-
export declare function unsubscribe(ctx: SuppressionContext,
|
|
28
|
+
export declare function unsubscribe(ctx: SuppressionContext, link: UnsubscribeLink | UnsubscribeToken | null, source: Exclude<SuppressionSource, "operator">, env?: Env): Promise<Result<undefined, UnsubscribeErrorCode>>;
|
|
27
29
|
//# sourceMappingURL=suppressions.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"suppressions.d.ts","sourceRoot":"","sources":["../../src/server/suppressions.ts"],"names":[],"mappings":"AAKA,OAAO,EAAW,KAAK,aAAa,EAAE,KAAK,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC5E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAEhD,OAAO,KAAK,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,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EAAE,oBAAoB,CAAC,CAAC,CAUlD"}
|
|
@@ -44,16 +44,18 @@ export async function liftSuppression(ctx, address) {
|
|
|
44
44
|
return lifted.length > 0;
|
|
45
45
|
}
|
|
46
46
|
/**
|
|
47
|
-
* Unsubscribes the recipient of a
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
47
|
+
* Unsubscribes the recipient of a link and runs `onUnsubscribed` in the same transaction, also when
|
|
48
|
+
* the recipient had already unsubscribed (a retried one-click heals a missed hook). A signed link
|
|
49
|
+
* (or a bare token) is checked against the secrets before the database is touched; a legacy link
|
|
50
|
+
* goes to the app's `legacyUnsubscribe.verify`, which names the address. A link that does not
|
|
51
|
+
* verify is `mailing.invalid_link`. Throws on a database failure, or when `verify` or the hook
|
|
52
|
+
* throws (nothing is stored).
|
|
51
53
|
*/
|
|
52
|
-
export async function unsubscribe(ctx,
|
|
53
|
-
|
|
54
|
+
export async function unsubscribe(ctx, link, source, env = process.env) {
|
|
55
|
+
const recipientKey = await verifyLink(ctx, link, env);
|
|
56
|
+
if (recipientKey === null)
|
|
54
57
|
return err("mailing.invalid_link");
|
|
55
58
|
const { onUnsubscribed } = getMailingOptions(ctx.config);
|
|
56
|
-
const { recipientKey } = token;
|
|
57
59
|
await ctx.db.transaction(async (tx) => {
|
|
58
60
|
const txCtx = { ...ctx, db: tx };
|
|
59
61
|
await recordSuppression(txCtx, recipientKey, source);
|
|
@@ -61,4 +63,18 @@ export async function unsubscribe(ctx, token, source, env = process.env) {
|
|
|
61
63
|
});
|
|
62
64
|
return ok();
|
|
63
65
|
}
|
|
66
|
+
/** The recipient key a link proves, or `null` when it proves none. */
|
|
67
|
+
async function verifyLink(ctx, link, env) {
|
|
68
|
+
if (link === null)
|
|
69
|
+
return null;
|
|
70
|
+
if (!("scheme" in link) || link.scheme === "signed") {
|
|
71
|
+
const token = "scheme" in link ? link.token : link;
|
|
72
|
+
return verifyUnsubscribeToken(token, readUnsubscribeSecrets(env)) ? token.recipientKey : null;
|
|
73
|
+
}
|
|
74
|
+
const legacy = getMailingOptions(ctx.config).legacyUnsubscribe;
|
|
75
|
+
if (legacy === undefined)
|
|
76
|
+
return null;
|
|
77
|
+
const address = await legacy.verify(link.values, ctx);
|
|
78
|
+
return typeof address === "string" && address.trim() !== "" ? getRecipientKey(address) : null;
|
|
79
|
+
}
|
|
64
80
|
//# sourceMappingURL=suppressions.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"suppressions.js","sourceRoot":"","sources":["../../src/server/suppressions.ts"],"names":[],"mappings":"AAAA,gGAAgG;AAChG,+FAA+F;AAC/F,oGAAoG;AACpG,kGAAkG;AAClG,mDAAmD;AACnD,OAAO,EAAE,GAAG,EAAE,EAAE,EAAmC,MAAM,kBAAkB,CAAC;AAE5E,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAE/C,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,sBAAsB,EAAE,sBAAsB,
|
|
1
|
+
{"version":3,"file":"suppressions.js","sourceRoot":"","sources":["../../src/server/suppressions.ts"],"names":[],"mappings":"AAAA,gGAAgG;AAChG,+FAA+F;AAC/F,oGAAoG;AACpG,kGAAkG;AAClG,mDAAmD;AACnD,OAAO,EAAE,GAAG,EAAE,EAAE,EAAmC,MAAM,kBAAkB,CAAC;AAE5E,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAE/C,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,sBAAsB,EAAE,sBAAsB,EAAyD,MAAM,uBAAuB,CAAC;AAI/J,gGAAgG;AAChG,MAAM,CAAC,KAAK,UA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E,sBAAsB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC,IAAI,CAAC;IAChG,CAAC;IACD,MAAM,MAAM,GAAG,iBAAiB,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,iBAAiB,CAAC;IAC/D,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,IAAI,CAAC;IACtC,MAAM,OAAO,GAAY,MAAM,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC/D,OAAO,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,CAAC,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;AAChG,CAAC"}
|
|
@@ -44,6 +44,29 @@ export declare function readUnsubscribeSecrets(env?: Env): UnsubscribeSecrets;
|
|
|
44
44
|
export declare function verifyUnsubscribeToken(token: UnsubscribeToken, secrets: UnsubscribeSecrets): boolean;
|
|
45
45
|
/** The two links for `address`, on the app's origin and the module's routes. */
|
|
46
46
|
export declare function buildUnsubscribeLinks(config: SoftureConfig, address: string, secret: string): UnsubscribeLinks;
|
|
47
|
+
/** A legacy link's value longer than this makes the link invalid before the app's `verify` sees it. */
|
|
48
|
+
export declare const MAX_LEGACY_VALUE_LENGTH = 512;
|
|
49
|
+
/** The link a person followed: the module's signed one, or one the app sent before it adopted the module. */
|
|
50
|
+
export type UnsubscribeLink = {
|
|
51
|
+
readonly scheme: "signed";
|
|
52
|
+
readonly token: UnsubscribeToken;
|
|
53
|
+
} | {
|
|
54
|
+
readonly scheme: "legacy";
|
|
55
|
+
readonly values: Readonly<Record<string, string>>;
|
|
56
|
+
};
|
|
57
|
+
/** Reads one query or form value; `URLSearchParams` and `FormData` both fit. */
|
|
58
|
+
export interface LinkParams {
|
|
59
|
+
get(name: string): unknown;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The link in a query or a form. A link with the recipient parameter (`r`) is a signed link, whatever else it
|
|
63
|
+
* carries, so a crafted URL cannot route a signed link to the app's legacy check. Without it, and with
|
|
64
|
+
* `mailing({ legacyUnsubscribe })`, a link that has every legacy parameter (non-empty, at most 512 characters) is
|
|
65
|
+
* a legacy link. Anything else is `null`. Checks no signature: `unsubscribe` does.
|
|
66
|
+
*/
|
|
67
|
+
export declare function readUnsubscribeLink(params: LinkParams, config: SoftureConfig): UnsubscribeLink | null;
|
|
68
|
+
/** The query (or form) parameters that carry `link`, to send it back to the page. */
|
|
69
|
+
export declare function getUnsubscribeLinkParams(link: UnsubscribeLink): Record<string, string>;
|
|
47
70
|
/** The token in a link's query (or a form), when both parameters are present once. */
|
|
48
71
|
export declare function readUnsubscribeToken(params: {
|
|
49
72
|
get(name: string): unknown;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"unsubscribe-link.d.ts","sourceRoot":"","sources":["../../src/server/unsubscribe-link.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAGtD,yCAAyC;AACzC,eAAO,MAAM,sBAAsB,+BAA+B,CAAC;AACnE,sEAAsE;AACtE,eAAO,MAAM,+BAA+B,wCAAwC,CAAC;AACrF,kFAAkF;AAClF,eAAO,MAAM,6BAA6B,KAAK,CAAC;AAEhD,0CAA0C;AAC1C,eAAO,MAAM,eAAe,MAAM,CAAC;AACnC,eAAO,MAAM,eAAe,MAAM,CAAC;AAWnC,MAAM,MAAM,GAAG,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,CAAC;AAE/D,wEAAwE;AACxE,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B;AAED,2DAA2D;AAC3D,MAAM,WAAW,gBAAgB;IAC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E3C,6GAA6G;AAC7G,MAAM,MAAM,eAAe,GACvB;IAAE,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,gBAAgB,CAAA;CAAE,GAC/D;IAAE,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAA;CAAE,CAAC;AAErF,gFAAgF;AAChF,MAAM,WAAW,UAAU;IACzB,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;CAC5B;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,aAAa,GAAG,eAAe,GAAG,IAAI,CAcrG;AAED,qFAAqF;AACrF,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,eAAe,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAEtF;AAED,sFAAsF;AACtF,wBAAgB,oBAAoB,CAAC,MAAM,EAAE;IAAE,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,GAAG,gBAAgB,GAAG,IAAI,CAIpG"}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// during a rotation. The whole link is a credential: whoever has it can unsubscribe that address,
|
|
5
5
|
// so it never reaches a log, an error or a result.
|
|
6
6
|
import { createHash, createHmac, timingSafeEqual } from "node:crypto";
|
|
7
|
-
import { getMailingRoutes } from "./options.js";
|
|
7
|
+
import { getMailingOptions, getMailingRoutes } from "./options.js";
|
|
8
8
|
/** Signs every new link and verifies. */
|
|
9
9
|
export const UNSUBSCRIBE_SECRET_ENV = "MAILING_UNSUBSCRIBE_SECRET";
|
|
10
10
|
/** Optional: verifies links signed before a rotation, never signs. */
|
|
@@ -69,6 +69,35 @@ export function buildUnsubscribeLinks(config, address, secret) {
|
|
|
69
69
|
oneClick: `${config.appOrigin}${routes.oneClick}?${query.toString()}`,
|
|
70
70
|
};
|
|
71
71
|
}
|
|
72
|
+
/** A legacy link's value longer than this makes the link invalid before the app's `verify` sees it. */
|
|
73
|
+
export const MAX_LEGACY_VALUE_LENGTH = 512;
|
|
74
|
+
/**
|
|
75
|
+
* The link in a query or a form. A link with the recipient parameter (`r`) is a signed link, whatever else it
|
|
76
|
+
* carries, so a crafted URL cannot route a signed link to the app's legacy check. Without it, and with
|
|
77
|
+
* `mailing({ legacyUnsubscribe })`, a link that has every legacy parameter (non-empty, at most 512 characters) is
|
|
78
|
+
* a legacy link. Anything else is `null`. Checks no signature: `unsubscribe` does.
|
|
79
|
+
*/
|
|
80
|
+
export function readUnsubscribeLink(params, config) {
|
|
81
|
+
if (params.get(RECIPIENT_PARAM) !== null && params.get(RECIPIENT_PARAM) !== undefined) {
|
|
82
|
+
const token = readUnsubscribeToken(params);
|
|
83
|
+
return token === null ? null : { scheme: "signed", token };
|
|
84
|
+
}
|
|
85
|
+
const legacy = getMailingOptions(config).legacyUnsubscribe;
|
|
86
|
+
if (legacy === undefined)
|
|
87
|
+
return null;
|
|
88
|
+
const values = {};
|
|
89
|
+
for (const name of legacy.params) {
|
|
90
|
+
const value = params.get(name);
|
|
91
|
+
if (typeof value !== "string" || value === "" || value.length > MAX_LEGACY_VALUE_LENGTH)
|
|
92
|
+
return null;
|
|
93
|
+
values[name] = value;
|
|
94
|
+
}
|
|
95
|
+
return { scheme: "legacy", values };
|
|
96
|
+
}
|
|
97
|
+
/** The query (or form) parameters that carry `link`, to send it back to the page. */
|
|
98
|
+
export function getUnsubscribeLinkParams(link) {
|
|
99
|
+
return link.scheme === "signed" ? { [RECIPIENT_PARAM]: link.token.recipientKey, [SIGNATURE_PARAM]: link.token.signature } : { ...link.values };
|
|
100
|
+
}
|
|
72
101
|
/** The token in a link's query (or a form), when both parameters are present once. */
|
|
73
102
|
export function readUnsubscribeToken(params) {
|
|
74
103
|
const recipientKey = params.get(RECIPIENT_PARAM);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"unsubscribe-link.js","sourceRoot":"","sources":["../../src/server/unsubscribe-link.ts"],"names":[],"mappings":"AAAA,6FAA6F;AAC7F,kGAAkG;AAClG,kGAAkG;AAClG,kGAAkG;AAClG,mDAAmD;AACnD,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAEtE,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"unsubscribe-link.js","sourceRoot":"","sources":["../../src/server/unsubscribe-link.ts"],"names":[],"mappings":"AAAA,6FAA6F;AAC7F,kGAAkG;AAClG,kGAAkG;AAClG,kGAAkG;AAClG,mDAAmD;AACnD,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAEtE,OAAO,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAEnE,yCAAyC;AACzC,MAAM,CAAC,MAAM,sBAAsB,GAAG,4BAA4B,CAAC;AACnE,sEAAsE;AACtE,MAAM,CAAC,MAAM,+BAA+B,GAAG,qCAAqC,CAAC;AACrF,kFAAkF;AAClF,MAAM,CAAC,MAAM,6BAA6B,GAAG,EAAE,CAAC;AAEhD,0CAA0C;AAC1C,MAAM,CAAC,MAAM,eAAe,GAAG,GAAG,CAAC;AACnC,MAAM,CAAC,MAAM,eAAe,GAAG,GAAG,CAAC;AAEnC;;;GAGG;AACH,MAAM,aAAa,GAAG,iCAAiC,CAAC;AAExD,sCAAsC;AACtC,MAAM,aAAa,GAAG,qBAAqB,CAAC;AAwB5C;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAAC,OAAe;IAC7C,OAAO,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;AACvF,CAA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ed answer, also on a released claim), and lets a
|
|
2
|
+
-- released claim give its attempt back: a halt (refused key, spent quota) says nothing about the mail, so a
|
|
3
|
+
-- `pending` row may hold 0 attempts until it is claimed again.
|
|
4
|
+
-- Rollback: ALTER TABLE mailing.deliveries DROP COLUMN provider_status;
|
|
5
|
+
-- UPDATE mailing.deliveries SET attempts = 1 WHERE attempts = 0;
|
|
6
|
+
-- ALTER TABLE mailing.deliveries DROP CONSTRAINT deliveries_attempts_check;
|
|
7
|
+
-- ALTER TABLE mailing.deliveries ADD CONSTRAINT deliveries_attempts_check CHECK (attempts >= 1);
|
|
8
|
+
-- DELETE FROM softure.migrations WHERE module = 'mailing' AND version = 3;
|
|
9
|
+
|
|
10
|
+
ALTER TABLE deliveries ADD COLUMN provider_status integer CHECK (provider_status BETWEEN 100 AND 599);
|
|
11
|
+
|
|
12
|
+
ALTER TABLE deliveries DROP CONSTRAINT deliveries_attempts_check;
|
|
13
|
+
ALTER TABLE deliveries ADD CONSTRAINT deliveries_attempts_check CHECK (attempts >= 1 OR (attempts = 0 AND status = 'pending'));
|
package/module.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@softure-ai/mailing",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"description": "SOFTURE AI mailing module: sendMail over a provider adapter (Resend first) with plain-text and HTML bodies, sender and reply-to from config, idempotency keys, a timeout and a typed result; signed one-click unsubscribe (RFC 8058) with a suppression list for list mail; an exactly-once delivery ledger, campaigns sent from a content file by the softure-mail command, and a sender DNS check; a fake provider for tests.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
package/src/cli/run.ts
CHANGED
|
@@ -13,7 +13,7 @@ import { systemClock, type SoftureConfig, type SoftureDatabaseConfig } from "@so
|
|
|
13
13
|
import { openCommandDatabase, type CommandDatabase, type DatabaseHandle } from "@softure-ai/db";
|
|
14
14
|
import { parseCampaignFile, parseRecipientList, type CampaignContent } from "../server/campaign-file.js";
|
|
15
15
|
import { planCampaign, sendCampaign, type CampaignSummary } from "../server/campaigns.js";
|
|
16
|
-
import type { DeliveryContext } from "../server/deliveries.js";
|
|
16
|
+
import type { DeliveryContext, HaltingErrorCode } from "../server/deliveries.js";
|
|
17
17
|
import { checkSenderDns, getSenderDomain, type DnsCheck, type ResolveTxt } from "../server/dns.js";
|
|
18
18
|
import { getMailingOptions } from "../server/options.js";
|
|
19
19
|
import { MIN_UNSUBSCRIBE_SECRET_LENGTH, readUnsubscribeSecrets, UNSUBSCRIBE_SECRET_ENV, type Env } from "../server/unsubscribe-link.js";
|
|
@@ -46,14 +46,17 @@ export const EXIT_USAGE = 2;
|
|
|
46
46
|
export const DEFAULT_PAUSE_MS = 500;
|
|
47
47
|
|
|
48
48
|
export const MAIL_USAGE = `Usage:
|
|
49
|
-
softure-mail campaign <content-file> --recipients <file> [--dry-run] [--pause-ms <ms>]
|
|
49
|
+
softure-mail campaign <content-file> --recipients <file> [--dry-run] [--pause-ms <ms>] [--resend-uncertain]
|
|
50
50
|
softure-mail dns [--domain <domain>] [--dkim-selector <name>]... [--spf-host <host>]...
|
|
51
51
|
|
|
52
52
|
campaign Sends the campaign in <content-file> (frontmatter id, kind, subject, optional html,
|
|
53
53
|
then the text body) to every address in the recipients file (one per line), at most
|
|
54
|
-
once each. Re-running sends only to recipients without an outcome yet.
|
|
55
|
-
|
|
56
|
-
--
|
|
54
|
+
once each. Re-running sends only to recipients without an outcome yet. A refused
|
|
55
|
+
API key or a spent quota stops the run; the rest go out on the next one.
|
|
56
|
+
--dry-run count what would be sent and change nothing
|
|
57
|
+
--pause-ms <ms> pause after every send (default ${String(DEFAULT_PAUSE_MS)})
|
|
58
|
+
--resend-uncertain also send to recipients whose send was interrupted long ago
|
|
59
|
+
(it may have gone out: they may get the mail twice)
|
|
57
60
|
|
|
58
61
|
dns Checks SPF, DKIM and DMARC for the sender domain (default: the domain of "from").
|
|
59
62
|
--domain <domain> check this domain instead
|
|
@@ -72,7 +75,14 @@ const consoleOutput: CliOutput = {
|
|
|
72
75
|
|
|
73
76
|
type Command =
|
|
74
77
|
| { readonly kind: "help" }
|
|
75
|
-
| {
|
|
78
|
+
| {
|
|
79
|
+
readonly kind: "campaign";
|
|
80
|
+
readonly contentFile: string;
|
|
81
|
+
readonly recipientsFile: string;
|
|
82
|
+
readonly dryRun: boolean;
|
|
83
|
+
readonly pauseMs: number;
|
|
84
|
+
readonly resendUncertain: boolean;
|
|
85
|
+
}
|
|
76
86
|
| { readonly kind: "dns"; readonly domain: string | undefined; readonly dkimSelectors: readonly string[] | undefined; readonly spfHosts: readonly string[] | undefined };
|
|
77
87
|
|
|
78
88
|
/** Runs the command and returns the process exit code: 0 done, 1 failed or incomplete, 2 usage error. */
|
|
@@ -111,7 +121,7 @@ function parseCommand(argv: readonly string[]): Command | string {
|
|
|
111
121
|
if (values.help === true) return { kind: "help" };
|
|
112
122
|
|
|
113
123
|
if (name === "dns") {
|
|
114
|
-
const misplaced = (["recipients", "dry-run", "pause-ms"] as const).filter((option) => values[option] !== undefined);
|
|
124
|
+
const misplaced = (["recipients", "dry-run", "pause-ms", "resend-uncertain"] as const).filter((option) => values[option] !== undefined);
|
|
115
125
|
if (misplaced.length > 0) return `dns does not take --${misplaced.join(", --")}`;
|
|
116
126
|
if (positionals.length > 0) return `dns takes no file, got "${positionals.join(" ")}"`;
|
|
117
127
|
return { kind: "dns", domain: values.domain, dkimSelectors: values["dkim-selector"], spfHosts: values["spf-host"] };
|
|
@@ -125,7 +135,7 @@ function parseCommand(argv: readonly string[]): Command | string {
|
|
|
125
135
|
if (values.recipients === undefined) return "campaign needs --recipients <file>";
|
|
126
136
|
const pauseMs = values["pause-ms"] === undefined ? DEFAULT_PAUSE_MS : Number(values["pause-ms"]);
|
|
127
137
|
if (!Number.isInteger(pauseMs) || pauseMs < 0 || pauseMs > 60_000) return `--pause-ms expects whole milliseconds from 0 to 60000, got "${values["pause-ms"] ?? ""}"`;
|
|
128
|
-
return { kind: "campaign", contentFile, recipientsFile: values.recipients, dryRun: values["dry-run"] === true, pauseMs };
|
|
138
|
+
return { kind: "campaign", contentFile, recipientsFile: values.recipients, dryRun: values["dry-run"] === true, pauseMs, resendUncertain: values["resend-uncertain"] === true };
|
|
129
139
|
}
|
|
130
140
|
|
|
131
141
|
function parseCommandArgs(args: readonly string[]) {
|
|
@@ -135,6 +145,7 @@ function parseCommandArgs(args: readonly string[]) {
|
|
|
135
145
|
recipients: { type: "string" },
|
|
136
146
|
"dry-run": { type: "boolean" },
|
|
137
147
|
"pause-ms": { type: "string" },
|
|
148
|
+
"resend-uncertain": { type: "boolean" },
|
|
138
149
|
domain: { type: "string" },
|
|
139
150
|
"dkim-selector": { type: "string", multiple: true },
|
|
140
151
|
"spf-host": { type: "string", multiple: true },
|
|
@@ -205,7 +216,9 @@ async function runCampaign(command: Extract<Command, { kind: "campaign" }>, opti
|
|
|
205
216
|
}
|
|
206
217
|
const ctx: DeliveryContext = { db: opened.handle.db, clock: systemClock, config };
|
|
207
218
|
try {
|
|
208
|
-
return command.dryRun
|
|
219
|
+
return command.dryRun
|
|
220
|
+
? await reportPlan(ctx, loaded, command.resendUncertain, output)
|
|
221
|
+
: await reportSend(ctx, loaded, { pauseMs: command.pauseMs, sleep: options.sleep, retakeUncertain: command.resendUncertain }, output);
|
|
209
222
|
} catch (error) {
|
|
210
223
|
// Driver errors: the message only, never a stack, an address or the database URL.
|
|
211
224
|
output.error(`softure-mail campaign: ${describeError(error)} (did softure migrate run?)`);
|
|
@@ -238,10 +251,19 @@ async function loadCampaign(contentPath: string, recipientsPath: string): Promis
|
|
|
238
251
|
return { campaign: { ...content, html }, recipients: parseRecipientList(list) };
|
|
239
252
|
}
|
|
240
253
|
|
|
241
|
-
async function reportPlan(ctx: DeliveryContext, loaded: LoadedCampaign, output: CliOutput): Promise<number> {
|
|
242
|
-
const plan = await planCampaign(ctx, loaded);
|
|
254
|
+
async function reportPlan(ctx: DeliveryContext, loaded: LoadedCampaign, retakeUncertain: boolean, output: CliOutput): Promise<number> {
|
|
255
|
+
const plan = await planCampaign(ctx, loaded, { retakeUncertain });
|
|
243
256
|
output.log(`campaign ${loaded.campaign.id} (${loaded.campaign.kind}), dry run: nothing sent or written`);
|
|
244
|
-
output.log(
|
|
257
|
+
output.log(
|
|
258
|
+
[
|
|
259
|
+
`recipients ${String(plan.recipients)}`,
|
|
260
|
+
`already done ${String(plan.done)}`,
|
|
261
|
+
`unsubscribed ${String(plan.suppressed)}`,
|
|
262
|
+
`filtered out ${String(plan.filtered)}`,
|
|
263
|
+
`uncertain ${String(plan.uncertain)}`,
|
|
264
|
+
`to send ${String(plan.toSend)}`,
|
|
265
|
+
].join(", "),
|
|
266
|
+
);
|
|
245
267
|
if (plan.contentChanged) {
|
|
246
268
|
output.error(`campaign ${loaded.campaign.id} was sent with other content; give this content a new id`);
|
|
247
269
|
return EXIT_FAILED;
|
|
@@ -252,13 +274,14 @@ async function reportPlan(ctx: DeliveryContext, loaded: LoadedCampaign, output:
|
|
|
252
274
|
async function reportSend(
|
|
253
275
|
ctx: DeliveryContext,
|
|
254
276
|
loaded: LoadedCampaign,
|
|
255
|
-
pace: { readonly pauseMs: number; readonly sleep: ((ms: number) => Promise<void>) | undefined },
|
|
277
|
+
pace: { readonly pauseMs: number; readonly sleep: ((ms: number) => Promise<void>) | undefined; readonly retakeUncertain: boolean },
|
|
256
278
|
output: CliOutput,
|
|
257
279
|
): Promise<number> {
|
|
258
280
|
let handled = 0;
|
|
259
281
|
const total = loaded.recipients.length;
|
|
260
282
|
const result = await sendCampaign(ctx, loaded, {
|
|
261
283
|
pauseMs: pace.pauseMs,
|
|
284
|
+
retakeUncertain: pace.retakeUncertain,
|
|
262
285
|
...(pace.sleep === undefined ? {} : { sleep: pace.sleep }),
|
|
263
286
|
onDelivery: () => {
|
|
264
287
|
handled += 1;
|
|
@@ -269,15 +292,33 @@ async function reportSend(
|
|
|
269
292
|
output.error(`campaign ${loaded.campaign.id} was sent with other content; give this content a new id`);
|
|
270
293
|
return EXIT_FAILED;
|
|
271
294
|
}
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
295
|
+
const summary = result.value;
|
|
296
|
+
output.log(`campaign ${loaded.campaign.id} (${loaded.campaign.kind}): ${formatSummary(summary)}`);
|
|
297
|
+
let code = EXIT_OK;
|
|
298
|
+
if (summary.halted !== null) {
|
|
299
|
+
const status = summary.halted.httpStatus === undefined ? "" : ` (HTTP ${String(summary.halted.httpStatus)})`;
|
|
300
|
+
output.error(`stopped: ${HALT_EXPLANATIONS[summary.halted.reason]}${status}; the remaining recipients were not touched, run the same command again once it is fixed`);
|
|
301
|
+
code = EXIT_FAILED;
|
|
302
|
+
}
|
|
303
|
+
const open = summary.inFlight + summary.retryLater;
|
|
304
|
+
if (open > 0 && summary.halted === null) {
|
|
275
305
|
output.error(`${String(open)} recipient(s) have no outcome yet; run the same command again later`);
|
|
276
|
-
|
|
306
|
+
code = EXIT_FAILED;
|
|
277
307
|
}
|
|
278
|
-
|
|
308
|
+
if (summary.uncertain > 0) {
|
|
309
|
+
output.error(
|
|
310
|
+
`${String(summary.uncertain)} recipient(s) are uncertain: a send was interrupted long ago and may have gone out; check the provider's log, then run again with --resend-uncertain to send them anyway`,
|
|
311
|
+
);
|
|
312
|
+
code = EXIT_FAILED;
|
|
313
|
+
}
|
|
314
|
+
return code;
|
|
279
315
|
}
|
|
280
316
|
|
|
317
|
+
const HALT_EXPLANATIONS: Readonly<Record<HaltingErrorCode, string>> = {
|
|
318
|
+
"mailing.provider_refused": "the provider refused the account (check the API key and the account)",
|
|
319
|
+
"mailing.quota_exceeded": "the account's sending quota is spent",
|
|
320
|
+
};
|
|
321
|
+
|
|
281
322
|
function formatSummary(summary: CampaignSummary): string {
|
|
282
323
|
const rejected = Object.entries(summary.rejected).filter(([, count]) => count > 0);
|
|
283
324
|
const rejectedTotal = rejected.reduce((sum, [, count]) => sum + count, 0);
|
|
@@ -289,6 +330,8 @@ function formatSummary(summary: CampaignSummary): string {
|
|
|
289
330
|
`already done ${String(summary.done)}`,
|
|
290
331
|
`in flight ${String(summary.inFlight)}`,
|
|
291
332
|
`retry later ${String(summary.retryLater)}`,
|
|
333
|
+
`filtered out ${String(summary.filtered)}`,
|
|
334
|
+
`uncertain ${String(summary.uncertain)}`,
|
|
292
335
|
].join(", ");
|
|
293
336
|
}
|
|
294
337
|
|
package/src/contract.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Result types, error codes and the provider contract of the mailing module. No user-facing copy
|
|
2
2
|
// here: the UI translates codes through `messages` (docs/02-module-standard.md §6).
|
|
3
|
-
import type { ModuleContext,
|
|
3
|
+
import type { Err, ModuleContext, Ok } from "@softure-ai/core";
|
|
4
4
|
import type { Queryable } from "@softure-ai/db";
|
|
5
5
|
|
|
6
6
|
export type MailingErrorCode =
|
|
@@ -11,7 +11,17 @@ export type MailingErrorCode =
|
|
|
11
11
|
/** The provider could not be reached or did not answer in time. Retry later, with the same idempotency key. */
|
|
12
12
|
| "mailing.unavailable"
|
|
13
13
|
/** A list mail to a recipient who unsubscribed. Nothing was sent; do not retry. */
|
|
14
|
-
| "mailing.suppressed"
|
|
14
|
+
| "mailing.suppressed"
|
|
15
|
+
/**
|
|
16
|
+
* The provider refuses the sender, not the mail: a wrong or revoked API key, a suspended or restricted account
|
|
17
|
+
* (HTTP 401/403). Every other mail would fail the same way: stop sending, fix the account, then retry.
|
|
18
|
+
*/
|
|
19
|
+
| "mailing.provider_refused"
|
|
20
|
+
/** The account's sending quota is spent (HTTP 429 that is not a rate limit). Stop sending; retry when it renews. */
|
|
21
|
+
| "mailing.quota_exceeded";
|
|
22
|
+
|
|
23
|
+
/** The codes that are about the sending account, not the mail: a run of sends stops at the first one. */
|
|
24
|
+
export const HALTING_ERROR_CODES: ReadonlySet<MailingErrorCode> = new Set(["mailing.provider_refused", "mailing.quota_exceeded"]);
|
|
15
25
|
|
|
16
26
|
/** Why an unsubscribe link was refused: missing, malformed, or not signed by a current secret. */
|
|
17
27
|
export type UnsubscribeErrorCode = "mailing.invalid_link";
|
|
@@ -36,6 +46,32 @@ export interface UnsubscribeEvent {
|
|
|
36
46
|
*/
|
|
37
47
|
export type OnUnsubscribedHook = (event: UnsubscribeEvent, ctx: ModuleContext<Queryable>) => Promise<void>;
|
|
38
48
|
|
|
49
|
+
/** What `filterCampaignRecipient` receives for each recipient of a campaign. */
|
|
50
|
+
export interface CampaignRecipient {
|
|
51
|
+
readonly address: string;
|
|
52
|
+
/** The recipient key of the address (`getRecipientKey`). */
|
|
53
|
+
readonly recipientKey: string;
|
|
54
|
+
readonly campaign: { readonly id: string; readonly kind: string };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Decides whether a campaign goes to a recipient, e.g. by the consent scope the app stored for the address. A
|
|
59
|
+
* recipient it refuses is skipped before the ledger is touched, so a later re-run sends to them once they qualify.
|
|
60
|
+
* A throw stops the campaign, like a database failure. Set in `mailing({ filterCampaignRecipient })`.
|
|
61
|
+
*/
|
|
62
|
+
export type CampaignRecipientFilter = (recipient: CampaignRecipient, ctx: ModuleContext<Queryable>) => Promise<boolean>;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Unsubscribe links an app sent before it adopted the module, in its own scheme. `params` are the query names of
|
|
66
|
+
* such a link; `verify` receives their values (all present once, each at most 512 characters) and returns the
|
|
67
|
+
* recipient's address when the link is genuine, else `null`. It must check the link's signature itself: whatever
|
|
68
|
+
* address it returns is unsubscribed. A throw reads as a failure (the person may try again), not as a bad link.
|
|
69
|
+
*/
|
|
70
|
+
export interface LegacyUnsubscribe {
|
|
71
|
+
readonly params: readonly string[];
|
|
72
|
+
readonly verify: (values: Readonly<Record<string, string>>, ctx: ModuleContext<Queryable>) => Promise<string | null>;
|
|
73
|
+
}
|
|
74
|
+
|
|
39
75
|
/** One mail to one recipient. A mail to a list is a loop over recipients, never a list in `to`. */
|
|
40
76
|
export interface OutgoingMail {
|
|
41
77
|
/** Exactly one address: `ada@example.com`. */
|
|
@@ -77,7 +113,10 @@ export interface SentMail {
|
|
|
77
113
|
readonly provider: string;
|
|
78
114
|
}
|
|
79
115
|
|
|
80
|
-
|
|
116
|
+
/** A failed send: the code, and the provider's HTTP status when it answered with one. */
|
|
117
|
+
export type SendMailFailure = Err<MailingErrorCode> & { readonly httpStatus?: number };
|
|
118
|
+
|
|
119
|
+
export type SendMailResult = Ok<SentMail> | SendMailFailure;
|
|
81
120
|
|
|
82
121
|
/** What a provider receives: a validated mail with the sender and reply-to from the configuration. */
|
|
83
122
|
export interface ProviderMessage {
|
|
@@ -91,11 +130,15 @@ export interface ProviderMessage {
|
|
|
91
130
|
readonly idempotencyKey: string | null;
|
|
92
131
|
}
|
|
93
132
|
|
|
94
|
-
/**
|
|
133
|
+
/**
|
|
134
|
+
* What a provider answers. `httpStatus` is logged and returned with the failure; the mail never is. `refused` is the
|
|
135
|
+
* provider refusing the sender (key, account), `quota_exceeded` a spent sending quota; `rejected` is about this mail.
|
|
136
|
+
*/
|
|
95
137
|
export type ProviderOutcome =
|
|
96
138
|
| { readonly status: "sent"; readonly id: string }
|
|
97
|
-
| { readonly status:
|
|
98
|
-
|
|
139
|
+
| { readonly status: ProviderFailureStatus; readonly httpStatus?: number };
|
|
140
|
+
|
|
141
|
+
export type ProviderFailureStatus = "rejected" | "unavailable" | "refused" | "quota_exceeded";
|
|
99
142
|
|
|
100
143
|
/**
|
|
101
144
|
* A mail service adapter. `send` should resolve with an outcome for every failure it knows; a
|
package/src/index.ts
CHANGED
|
@@ -17,7 +17,7 @@ export const MODULE_ID = "mailing";
|
|
|
17
17
|
export const mailing = defineModule({
|
|
18
18
|
manifest: {
|
|
19
19
|
id: MODULE_ID,
|
|
20
|
-
version: "0.1.
|
|
20
|
+
version: "0.1.7",
|
|
21
21
|
dependsOn: {},
|
|
22
22
|
dbSchema: "mailing",
|
|
23
23
|
tables: ["suppressions", "campaigns", "deliveries"],
|
|
@@ -64,14 +64,19 @@ export {
|
|
|
64
64
|
MAX_SUBJECT_LENGTH,
|
|
65
65
|
RESERVED_HEADERS,
|
|
66
66
|
} from "./address.js";
|
|
67
|
-
export { TRANSACTIONAL_KIND } from "./contract.js";
|
|
67
|
+
export { HALTING_ERROR_CODES, TRANSACTIONAL_KIND } from "./contract.js";
|
|
68
68
|
export type {
|
|
69
|
+
CampaignRecipient,
|
|
70
|
+
CampaignRecipientFilter,
|
|
71
|
+
LegacyUnsubscribe,
|
|
69
72
|
MailingErrorCode,
|
|
70
73
|
MailProvider,
|
|
71
74
|
OnUnsubscribedHook,
|
|
72
75
|
OutgoingMail,
|
|
76
|
+
ProviderFailureStatus,
|
|
73
77
|
ProviderMessage,
|
|
74
78
|
ProviderOutcome,
|
|
79
|
+
SendMailFailure,
|
|
75
80
|
SendMailOptions,
|
|
76
81
|
SendMailResult,
|
|
77
82
|
SentMail,
|
|
@@ -80,6 +85,6 @@ export type {
|
|
|
80
85
|
UnsubscribeEvent,
|
|
81
86
|
} from "./contract.js";
|
|
82
87
|
export { getMailingErrorMessage, mailingMessages, type MailingMessages } from "./messages/index.js";
|
|
83
|
-
export { DEFAULT_TIMEOUT_MS, type MailingOptions, type MailingOptionsInput } from "./options.js";
|
|
88
|
+
export { DEFAULT_STALE_CLAIM_MS, DEFAULT_TIMEOUT_MS, DEFAULT_UNCERTAIN_CLAIM_MS, type MailingOptions, type MailingOptionsInput } from "./options.js";
|
|
84
89
|
export { resend, RESEND_API_KEY_ENV, RESEND_ENDPOINT, type ResendOptions } from "./providers/resend.js";
|
|
85
90
|
export { campaigns, deliveries, mailingSchema, suppressions, type DeliveryStatus } from "./schema.js";
|
package/src/messages/en.ts
CHANGED
|
@@ -5,6 +5,8 @@ export const en = {
|
|
|
5
5
|
rejected: "The mail service refused this message.",
|
|
6
6
|
unavailable: "The mail service is not answering. Try again in a moment.",
|
|
7
7
|
suppressed: "This address unsubscribed from these emails, so nothing was sent.",
|
|
8
|
+
provider_refused: "The mail service refused our account, so nothing was sent. We need to fix it on our side.",
|
|
9
|
+
quota_exceeded: "We reached our sending limit for now. Try again later.",
|
|
8
10
|
},
|
|
9
11
|
},
|
|
10
12
|
footer: {
|
package/src/messages/pl.ts
CHANGED
|
@@ -8,6 +8,8 @@ export const pl: typeof en = {
|
|
|
8
8
|
rejected: "Usługa pocztowa odrzuciła tę wiadomość.",
|
|
9
9
|
unavailable: "Usługa pocztowa nie odpowiada. Spróbuj ponownie za chwilę.",
|
|
10
10
|
suppressed: "Ten adres wypisał się z takich wiadomości, więc nic nie wysłano.",
|
|
11
|
+
provider_refused: "Usługa pocztowa odrzuciła nasze konto, więc nic nie wysłano. Musimy to naprawić po naszej stronie.",
|
|
12
|
+
quota_exceeded: "Wyczerpaliśmy na razie limit wysyłki. Spróbuj ponownie później.",
|
|
11
13
|
},
|
|
12
14
|
},
|
|
13
15
|
footer: {
|
package/src/next/actions.ts
CHANGED
|
@@ -9,16 +9,16 @@ import { getSoftureConfig } from "@softure-ai/core/next";
|
|
|
9
9
|
import { redirect } from "next/navigation";
|
|
10
10
|
import { getMailingRoutes } from "../server/options.js";
|
|
11
11
|
import { unsubscribe } from "../server/suppressions.js";
|
|
12
|
-
import {
|
|
12
|
+
import { getUnsubscribeLinkParams, readUnsubscribeLink } from "../server/unsubscribe-link.js";
|
|
13
13
|
import { getMailingContext } from "./context.js";
|
|
14
14
|
import { UNSUBSCRIBE_STATUS_PARAM, type UnsubscribeStatus } from "./params.js";
|
|
15
15
|
|
|
16
16
|
export async function unsubscribeAction(formData: FormData): Promise<void> {
|
|
17
17
|
const config = getSoftureConfig();
|
|
18
|
-
const
|
|
18
|
+
const link = readUnsubscribeLink(formData, config);
|
|
19
19
|
let status: UnsubscribeStatus;
|
|
20
20
|
try {
|
|
21
|
-
const result = await unsubscribe(await getMailingContext(config),
|
|
21
|
+
const result = await unsubscribe(await getMailingContext(config), link, "page");
|
|
22
22
|
status = result.ok ? "done" : "invalid";
|
|
23
23
|
} catch (error) {
|
|
24
24
|
// The label names the kind of failure, never its text: that can carry the link's values.
|
|
@@ -26,10 +26,9 @@ export async function unsubscribeAction(formData: FormData): Promise<void> {
|
|
|
26
26
|
status = "failed";
|
|
27
27
|
}
|
|
28
28
|
const query = new URLSearchParams({ [UNSUBSCRIBE_STATUS_PARAM]: status });
|
|
29
|
-
if (status === "failed" &&
|
|
29
|
+
if (status === "failed" && link !== null) {
|
|
30
30
|
// The form comes back with the same link, so the person can try again.
|
|
31
|
-
query.set(
|
|
32
|
-
query.set(SIGNATURE_PARAM, token.signature);
|
|
31
|
+
for (const [name, value] of Object.entries(getUnsubscribeLinkParams(link))) query.set(name, value);
|
|
33
32
|
}
|
|
34
33
|
redirect(`${getMailingRoutes(config).unsubscribe}?${query.toString()}`);
|
|
35
34
|
}
|