Switch postmark api to smtp

This commit is contained in:
mplorentz 2026-08-24 16:12:32 -04:00
parent 1b797b3759
commit 1865128f41
4 changed files with 34 additions and 22 deletions

View file

@ -20,8 +20,8 @@
"@types/express-ws": "^3.0.5",
"@types/mjml": "^4.7.4",
"@types/mustache": "^4.2.6",
"@types/nodemailer": "^8.0.1",
"@types/sanitize-html": "^2.16.0",
"@types/ws": "^8.18.1",
"@typescript-eslint/eslint-plugin": "^8.43.0",
"@typescript-eslint/parser": "^8.43.0",
@ -48,17 +48,15 @@
"express": "^4.21.2",
"express-rate-limit": "^7.5.1",
"express-ws": "^5.0.2",
"localstorage-polyfill": "^1.0.1",
"mjml": "^4.15.3",
"mustache": "^4.2.0",
"nodemailer": "^6.10.1",
"nostr-tools": "^2.16.2",
"postmark": "^4.0.5",
"sanitize-html": "^2.17.0",
"sqlite3": "^5.1.7",
"succinct-async": "^1.0.4",
"ts-node-dev": "^2.0.0",
"ws": "^8.18.3"
},
"pnpm": {

Binary file not shown.

View file

@ -8,8 +8,11 @@ import { routerContext } from '@welshman/router'
if (!process.env.MAILSHIP_URL) throw new Error('MAILSHIP_URL is not defined.')
if (!process.env.MAILSHIP_NAME) throw new Error('MAILSHIP_NAME is not defined.')
if (!process.env.MAILSHIP_SECRET) throw new Error('MAILSHIP_SECRET is not defined.')
if (!process.env.POSTMARK_API_KEY) throw new Error('POSTMARK_API_KEY is not defined.')
if (!process.env.POSTMARK_SENDER_ADDRESS) throw new Error('POSTMARK_SENDER_ADDRESS is not defined.')
if (!process.env.SMTP_HOST) throw new Error('SMTP_HOST is not defined.')
if (!process.env.SMTP_PORT) throw new Error('SMTP_PORT is not defined.')
if (!process.env.SMTP_USER) throw new Error('SMTP_USER is not defined.')
if (!process.env.SMTP_PASSWORD) throw new Error('SMTP_PASSWORD is not defined.')
if (!process.env.SMTP_FROM) throw new Error('SMTP_FROM is not defined.')
if (!process.env.DEFAULT_RELAYS) throw new Error('DEFAULT_RELAYS is not defined.')
if (!process.env.INDEXER_RELAYS) throw new Error('INDEXER_RELAYS is not defined.')
if (!process.env.SEARCH_RELAYS) throw new Error('SEARCH_RELAYS is not defined.')
@ -20,12 +23,15 @@ export const MAILSHIP_URL = process.env.MAILSHIP_URL
export const MAILSHIP_NAME = process.env.MAILSHIP_NAME
export const BASE_URL = process.env.BASE_URL
export const appSigner = Nip01Signer.fromSecret(process.env.MAILSHIP_SECRET)
export const POSTMARK_API_KEY = process.env.POSTMARK_API_KEY
export const POSTMARK_SENDER_ADDRESS = process.env.POSTMARK_SENDER_ADDRESS
export const DEFAULT_RELAYS = process.env.DEFAULT_RELAYS.split(',').map(normalizeRelayUrl)
export const INDEXER_RELAYS = process.env.INDEXER_RELAYS.split(',').map(normalizeRelayUrl)
export const SEARCH_RELAYS = process.env.SEARCH_RELAYS.split(',').map(normalizeRelayUrl)
export const PORT = process.env.PORT
export const SMTP_HOST = process.env.SMTP_HOST
export const SMTP_PORT = process.env.SMTP_PORT
export const SMTP_USER = process.env.SMTP_USER
export const SMTP_PASSWORD = process.env.SMTP_PASSWORD
export const SMTP_FROM = process.env.SMTP_FROM
appSigner.getPubkey().then(pubkey => {
console.log(`Running as ${pubkey}`)

View file

@ -1,32 +1,40 @@
import { ServerClient } from 'postmark'
import { POSTMARK_SENDER_ADDRESS, POSTMARK_API_KEY, MAILSHIP_NAME, BASE_URL } from './env.js'
import nodemailer from 'nodemailer'
import { SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASSWORD, SMTP_FROM, MAILSHIP_NAME, BASE_URL } from './env.js'
import type { Subscription } from './alert.js'
import { render } from './templates.js'
const client = new ServerClient(POSTMARK_API_KEY)
const transporter = nodemailer.createTransport({
host: SMTP_HOST,
port: Number(SMTP_PORT),
secure: true,
auth: {
user: SMTP_USER,
pass: SMTP_PASSWORD,
},
})
export const sendConfirm = (sub: Subscription) => {
const href = `${BASE_URL}/confirm?token=${sub.key}`
return client.sendEmail({
From: POSTMARK_SENDER_ADDRESS,
To: sub.email,
Subject: 'Confirm your email digest',
HtmlBody: `
return transporter.sendMail({
from: SMTP_FROM,
to: sub.email,
subject: 'Confirm your email digest',
html: `
<h3>Welcome to ${MAILSHIP_NAME}!</h3>
<p>Please confirm that you would like to receive ${sub.frequency} digests by clicking the link below:</p>
<p><a href="${href}">Confirm Digest</a></p>
`,
TextBody: `Please confirm that you would like to receive ${sub.frequency} digests by visiting: ${href}`,
text: `Please confirm that you would like to receive ${sub.frequency} digests by visiting: ${href}`,
})
}
export const sendDigest = async (sub: Subscription, variables: Record<string, any>) => {
return client.sendEmail({
From: POSTMARK_SENDER_ADDRESS,
To: sub.email,
Subject: 'New activity',
HtmlBody: await render('emails/digest.mjml', {
return transporter.sendMail({
from: SMTP_FROM,
to: sub.email,
subject: 'New activity',
html: await render('emails/digest.mjml', {
...variables,
name: sub.email.split('@')[0],
unsubscribeUrl: `${BASE_URL}/unsubscribe?token=${sub.key}`,