Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use Postmark's spam score API #18743

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/emails/templates/_base-email.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import axios from "axios";
import { decodeHTML } from "entities";
import { createTransport } from "nodemailer";
import { z } from "zod";
Expand Down Expand Up @@ -53,6 +54,11 @@ export default class BaseEmail {
const from = "from" in payload ? (payload.from as string) : "";
const to = "to" in payload ? (payload.to as string) : "";

if (!(await this.checkSpamScore(payload))) {
console.log(`Skipped Sending Email email due to spam score: ${to}`);
return new Promise((r) => r(`Skipped Sending Email email due to spam score: ${to}`));
}

if (isSmsCalEmail(to)) {
console.log(`Skipped Sending Email to faux email: ${to}`);
return new Promise((r) => r(`Skipped Sending Email to faux email: ${to}`));
Expand Down Expand Up @@ -94,6 +100,25 @@ export default class BaseEmail {
);
return new Promise((resolve) => resolve("send mail async"));
}

protected async checkSpamScore(payload: Record<string, unknown>) {
try {
const result = await axios.post("https://spamcheck.postmarkapp.com./filter", {
email: payload.toString(),
options: "long",
});

const spamScore = parseFloat(result.data.score);

if (!isNaN(spamScore) && result.data.score < 8) {
return false;
}
} catch (error) {
console.error(error);
}

return true;
}
protected getMailerOptions() {
return {
transport: serverConfig.transport,
Expand Down
Loading