feat: add email notification channel
This commit is contained in:
+31
-8
@@ -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 }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user