onsitesurvey/src/libs/mailer.ts
2025-05-28 17:07:05 +09:00

51 lines
1.2 KiB
TypeScript

'use server'
import nodemailer from 'nodemailer'
interface EmailParams {
to: string | string[]
cc?: string | string[]
subject: string
content: string
}
export async function sendEmail({ to, cc, subject, content }: EmailParams): Promise<void> {
// Create a transporter using SMTP
const transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: Number(process.env.SMTP_PORT),
secure: process.env.SMTP_SECURE === 'true',
requireTLS: true,
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASSWORD,
},
})
// Email options
const mailOptions = {
from: process.env.SMTP_USER,
to: Array.isArray(to) ? to.join(', ') : to,
cc: cc ? (Array.isArray(cc) ? cc.join(', ') : cc) : undefined,
subject,
html: content,
}
try {
// Send email
await transporter.sendMail(mailOptions)
} catch (error) {
console.error('Error sending email:', error)
throw new Error('Failed to send email')
}
}
async function sendEmailTest() {
await sendEmail({
to: 'test@test.com',
cc: 'test2@test.com',
subject: 'Test Email',
content: '<h1>Hello</h1><p>This is a test email.</p>',
})
}