43 lines
1.1 KiB
TypeScript
43 lines
1.1 KiB
TypeScript
'use server'
|
|
|
|
import nodemailer from 'nodemailer'
|
|
import { Attachment } from 'nodemailer/lib/mailer'
|
|
|
|
interface EmailParams {
|
|
from: string
|
|
to: string | string[]
|
|
cc?: string | string[]
|
|
subject: string
|
|
content: string
|
|
attachments?: Attachment[]
|
|
}
|
|
|
|
export async function sendEmail({ from, to, cc, subject, content, attachments }: 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,
|
|
tls: { rejectUnauthorized: false },
|
|
})
|
|
|
|
// Email options
|
|
const mailOptions = {
|
|
from,
|
|
to: Array.isArray(to) ? to.join(', ') : to,
|
|
cc: cc ? (Array.isArray(cc) ? cc.join(', ') : cc) : undefined,
|
|
subject,
|
|
html: content,
|
|
attachments: attachments || [],
|
|
}
|
|
|
|
try {
|
|
// Send email
|
|
await transporter.sendMail(mailOptions)
|
|
} catch (error) {
|
|
console.error('Error sending email:', error)
|
|
throw new Error('Failed to send email')
|
|
}
|
|
}
|