log errors

This commit is contained in:
mplorentz 2026-08-25 15:15:33 -04:00
parent 291f92c37e
commit cdabec2a92
2 changed files with 43 additions and 19 deletions

View file

@ -3,10 +3,13 @@ import { SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASSWORD, SMTP_FROM, MAILSHIP_NAM
import type { Subscription } from './alert.js' import type { Subscription } from './alert.js'
import { render } from './templates.js' import { render } from './templates.js'
const secure = Number(SMTP_PORT) === 465
const transporter = nodemailer.createTransport({ const transporter = nodemailer.createTransport({
host: SMTP_HOST, host: SMTP_HOST,
port: Number(SMTP_PORT), port: Number(SMTP_PORT),
secure: true, secure,
requireTLS: !secure,
auth: { auth: {
user: SMTP_USER, user: SMTP_USER,
pass: SMTP_PASSWORD, pass: SMTP_PASSWORD,
@ -16,28 +19,46 @@ const transporter = nodemailer.createTransport({
export const sendConfirm = (sub: Subscription) => { export const sendConfirm = (sub: Subscription) => {
const href = `${BASE_URL}/confirm?token=${sub.key}` const href = `${BASE_URL}/confirm?token=${sub.key}`
return transporter.sendMail({ return transporter
from: SMTP_FROM, .sendMail({
to: sub.email, from: SMTP_FROM,
subject: 'Confirm your email digest', to: sub.email,
html: ` subject: 'Confirm your email digest',
html: `
<h3>Welcome to ${MAILSHIP_NAME}!</h3> <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>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> <p><a href="${href}">Confirm Digest</a></p>
`, `,
text: `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}`,
}) })
.catch(error => {
console.error('mailer: confirmation email failed', {
to: sub.email,
smtp: { host: SMTP_HOST, port: SMTP_PORT },
error: error?.message || error,
})
throw error
})
} }
export const sendDigest = async (sub: Subscription, variables: Record<string, any>) => { export const sendDigest = async (sub: Subscription, variables: Record<string, any>) => {
return transporter.sendMail({ return transporter
from: SMTP_FROM, .sendMail({
to: sub.email, from: SMTP_FROM,
subject: 'New activity', to: sub.email,
html: await render('emails/digest.mjml', { subject: 'New activity',
...variables, html: await render('emails/digest.mjml', {
name: sub.email.split('@')[0], ...variables,
unsubscribeUrl: `${BASE_URL}/unsubscribe?token=${sub.key}`, name: sub.email.split('@')[0],
}), unsubscribeUrl: `${BASE_URL}/unsubscribe?token=${sub.key}`,
}) }),
})
.catch(error => {
console.error('mailer: digest email failed', {
to: sub.email,
smtp: { host: SMTP_HOST, port: SMTP_PORT },
error: error?.message || error,
})
throw error
})
} }

View file

@ -113,7 +113,10 @@ addRoute('post', '/subscription/email', async (req: Request, res: Response) => {
const result = await registerSubscription({ pubkey, email, frequency }) const result = await registerSubscription({ pubkey, email, frequency })
res.json(result) res.json(result)
} catch (error: any) { } catch (error: any) {
// If the error is just Postmark failing, the subscription was still created // The subscription was still created, but sending the confirmation email
// may have failed. Always log it so SMTP issues are visible.
console.error('Failed to send confirmation email for', pubkey, error?.message || error)
// Look up the actual subscription key from the DB // Look up the actual subscription key from the DB
const sub = await getSubscriptionByPubkey(pubkey) const sub = await getSubscriptionByPubkey(pubkey)
if (sub) { if (sub) {