feat: add email notification channel

This commit is contained in:
Kirill
2026-05-18 11:28:46 +05:00
parent 79c85b0a88
commit 8f3d1ae5ef
2 changed files with 68 additions and 8 deletions
+31 -8
View File
@@ -4,14 +4,8 @@ function hasSmtpEnv() {
return Boolean(process.env.SMTP_HOST && process.env.SMTP_PORT && process.env.SMTP_USER && process.env.SMTP_PASS)
}
export async function sendLoginCodeEmail({ to, code }) {
if (!hasSmtpEnv()) {
// dev fallback
console.log(`[DEV] login code for ${to}: ${code}`)
return
}
const transporter = nodemailer.createTransport({
function createTransporter() {
return nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: Number(process.env.SMTP_PORT),
secure: process.env.SMTP_SECURE === 'true',
@@ -20,7 +14,15 @@ export async function sendLoginCodeEmail({ to, code }) {
pass: process.env.SMTP_PASS,
},
})
}
export async function sendLoginCodeEmail({ to, code }) {
if (!hasSmtpEnv()) {
console.log(`[DEV] login code for ${to}: ${code}`)
return
}
const transporter = createTransporter()
const from = process.env.MAIL_FROM || process.env.SMTP_USER
await transporter.sendMail({
@@ -31,3 +33,24 @@ export async function sendLoginCodeEmail({ to, code }) {
})
}
export async function sendNotificationEmail({ to, subject, html }) {
if (!hasSmtpEnv()) {
console.log(`[DEV] notification email to ${to}: ${subject}`)
return { success: true }
}
try {
const transporter = createTransporter()
const from = process.env.MAIL_FROM || process.env.SMTP_USER
await transporter.sendMail({
from,
to,
subject,
html,
})
return { success: true }
} catch (err) {
return { success: false, error: err.message }
}
}