yo-next-auth/lib/mailer.ts

22 lines
843 B
TypeScript

'use server'
import { from, transportOptions } from '@/config/mailer'
import nodemailer, { Transporter } from 'nodemailer'
import type { Options } from 'nodemailer/lib/mailer'
import { SentMessageInfo } from 'nodemailer/lib/smtp-transport'
const transporter: Transporter<SentMessageInfo> = nodemailer.createTransport(transportOptions)
type Return = {
isOk: boolean, code?: number, info?: SentMessageInfo, error?: any
}
export default async function mailer ({ to, subject, text, html }: Options): Promise<Return> {
try {
const info: SentMessageInfo = await transporter.sendMail({ from, to, subject, text, html })
return { isOk: true, code: parseInt((info?.response ?? '0').substring(0, 3), 10), info }
} catch (error: any) {
return { isOk: false, error }
}
//TODO: MAILER LOGGING >> error.response || info?.messageId
}