import nodemailer from 'nodemailer'
import {
SMTP_HOST,
SMTP_PORT,
SMTP_USER,
SMTP_PASSWORD,
SMTP_FROM,
BASE_URL,
EVENT_VIEWER_URL,
BRAND_ACCENT,
BRAND_NAME,
BRAND_LOGO,
} from './env.js'
import type { Subscription } from './alert.js'
import { render } from './templates.js'
const secure = Number(SMTP_PORT) === 465
const transporter = nodemailer.createTransport({
host: SMTP_HOST,
port: Number(SMTP_PORT),
secure,
requireTLS: !secure,
auth: {
user: SMTP_USER,
pass: SMTP_PASSWORD,
},
})
export const sendConfirm = (sub: Subscription) => {
const href = `${BASE_URL}/confirm?token=${sub.key}`
const settingsUrl = `${EVENT_VIEWER_URL}/settings/alerts`
return transporter
.sendMail({
from: SMTP_FROM,
to: sub.email,
subject: `Confirm your email for ${BRAND_NAME} notifications`,
html: `
`,
text: `Please click below to receive emails for updates from your communities on ${BRAND_NAME}.\n\nConfirm: ${href}\n\nTo disable notifications, ignore this email. Or update your settings at: ${settingsUrl}`,
})
.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) => {
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}`,
brandName: BRAND_NAME,
brandAccent: BRAND_ACCENT,
brandLogo: BRAND_LOGO,
settingsUrl: `${EVENT_VIEWER_URL}/settings/alerts`,
}),
})
.catch(error => {
console.error('mailer: digest email failed', {
to: sub.email,
smtp: { host: SMTP_HOST, port: SMTP_PORT },
error: error?.message || error,
})
throw error
})
}