2022-02-07 18:06:37 +01:00
|
|
|
import prisma from 'libs/prisma'
|
|
|
|
import { SendEmailOptions, SmtpCredentialsData } from 'models'
|
|
|
|
import { NextApiRequest, NextApiResponse } from 'next'
|
|
|
|
import { createTransport } from 'nodemailer'
|
|
|
|
import { decrypt, initMiddleware } from 'utils'
|
|
|
|
|
|
|
|
import Cors from 'cors'
|
2022-02-15 08:43:11 +01:00
|
|
|
import { withSentry } from '@sentry/nextjs'
|
2022-02-07 18:06:37 +01:00
|
|
|
|
|
|
|
const cors = initMiddleware(Cors())
|
|
|
|
|
2022-02-11 19:09:29 +01:00
|
|
|
const defaultTransportOptions = {
|
2022-03-13 08:56:10 +01:00
|
|
|
host: process.env.SMTP_HOST,
|
|
|
|
port: Number(process.env.SMTP_PORT),
|
2022-02-07 18:06:37 +01:00
|
|
|
secure: false,
|
|
|
|
auth: {
|
2022-03-13 08:56:10 +01:00
|
|
|
user: process.env.SMTP_USERNAME,
|
|
|
|
pass: process.env.SMTP_PASSWORD,
|
2022-02-07 18:06:37 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-02-11 19:09:29 +01:00
|
|
|
const defaultFrom = {
|
2022-03-13 08:56:10 +01:00
|
|
|
name: process.env.NEXT_PUBLIC_SMTP_FROM?.split(' <')[0].replace(/"/g, ''),
|
|
|
|
email: process.env.NEXT_PUBLIC_SMTP_FROM?.match(/\<(.*)\>/)?.pop(),
|
2022-02-11 19:09:29 +01:00
|
|
|
}
|
2022-02-07 18:06:37 +01:00
|
|
|
|
|
|
|
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|
|
|
await cors(req, res)
|
|
|
|
if (req.method === 'POST') {
|
2022-02-22 10:16:35 +01:00
|
|
|
const { credentialsId, recipients, body, subject, cc, bcc } = (
|
|
|
|
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
|
2022-02-07 18:06:37 +01:00
|
|
|
) as SendEmailOptions
|
|
|
|
|
2022-02-11 19:09:29 +01:00
|
|
|
const { host, port, isTlsEnabled, username, password, from } =
|
|
|
|
(await getEmailInfo(credentialsId)) ?? {}
|
|
|
|
if (!from)
|
2022-02-07 18:06:37 +01:00
|
|
|
return res.status(404).send({ message: "Couldn't find credentials" })
|
|
|
|
|
2022-02-11 19:09:29 +01:00
|
|
|
const transporter = createTransport({
|
|
|
|
host,
|
|
|
|
port,
|
|
|
|
secure: isTlsEnabled ?? undefined,
|
|
|
|
auth: {
|
|
|
|
user: username,
|
|
|
|
pass: password,
|
|
|
|
},
|
|
|
|
})
|
2022-02-07 18:06:37 +01:00
|
|
|
const info = await transporter.sendMail({
|
2022-02-11 19:09:29 +01:00
|
|
|
from: `"${from.name}" <${from.email}>`,
|
2022-02-19 10:58:56 +01:00
|
|
|
cc: cc?.join(''),
|
|
|
|
bcc: bcc?.join(''),
|
2022-02-07 18:06:37 +01:00
|
|
|
to: recipients.join(', '),
|
|
|
|
subject,
|
|
|
|
text: body,
|
|
|
|
})
|
|
|
|
|
|
|
|
res.status(200).send({ message: 'Email sent!', info })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-11 19:09:29 +01:00
|
|
|
const getEmailInfo = async (
|
|
|
|
credentialsId: string
|
|
|
|
): Promise<SmtpCredentialsData | undefined> => {
|
|
|
|
if (credentialsId === 'default')
|
|
|
|
return {
|
|
|
|
host: defaultTransportOptions.host,
|
|
|
|
port: defaultTransportOptions.port,
|
|
|
|
username: defaultTransportOptions.auth.user,
|
|
|
|
password: defaultTransportOptions.auth.pass,
|
|
|
|
isTlsEnabled: undefined,
|
|
|
|
from: defaultFrom,
|
|
|
|
}
|
|
|
|
const credentials = await prisma.credentials.findUnique({
|
|
|
|
where: { id: credentialsId },
|
|
|
|
})
|
|
|
|
if (!credentials) return
|
|
|
|
return decrypt(credentials.data, credentials.iv) as SmtpCredentialsData
|
|
|
|
}
|
|
|
|
|
2022-02-15 08:43:11 +01:00
|
|
|
export default withSentry(handler)
|