fix(editor): 🐛 Fix email step in viewer
This commit is contained in:
@@ -11,14 +11,7 @@ AUTH_EMAIL_SERVER_PORT=587
|
|||||||
AUTH_EMAIL_FROM_EMAIL=noreply@example.com
|
AUTH_EMAIL_FROM_EMAIL=noreply@example.com
|
||||||
AUTH_EMAIL_FROM_NAME="John Smith"
|
AUTH_EMAIL_FROM_NAME="John Smith"
|
||||||
|
|
||||||
# (Optional) Used for email notifications
|
NEXT_PUBLIC_EMAIL_NOTIFICATIONS_FROM_EMAIL=
|
||||||
EMAIL_NOTIFICATIONS_SERVER_USERNAME=username
|
|
||||||
EMAIL_NOTIFICATIONS_SERVER_PASSWORD=password
|
|
||||||
EMAIL_NOTIFICATIONS_SERVER_HOST=smtp.example.com
|
|
||||||
EMAIL_NOTIFICATIONS_SERVER_PORT=587
|
|
||||||
EMAIL_NOTIFICATIONS_FROM_EMAIL=noreply@example.com
|
|
||||||
EMAIL_NOTIFICATIONS_FROM_NAME="John Smith"
|
|
||||||
|
|
||||||
|
|
||||||
# Storage
|
# Storage
|
||||||
# Used for uploading images, videos, etc...
|
# Used for uploading images, videos, etc...
|
||||||
|
|||||||
@@ -2,3 +2,14 @@ ENCRYPTION_SECRET=
|
|||||||
NEXT_PUBLIC_VIEWER_HOST=http://localhost:3001
|
NEXT_PUBLIC_VIEWER_HOST=http://localhost:3001
|
||||||
DATABASE_URL=postgresql://postgres:@localhost:5432/typebot
|
DATABASE_URL=postgresql://postgres:@localhost:5432/typebot
|
||||||
|
|
||||||
|
GOOGLE_CLIENT_ID=
|
||||||
|
GOOGLE_CLIENT_SECRET=
|
||||||
|
|
||||||
|
# (Optional) Used for email notifications
|
||||||
|
EMAIL_NOTIFICATIONS_SERVER_USER=username
|
||||||
|
EMAIL_NOTIFICATIONS_SERVER_PASSWORD=password
|
||||||
|
EMAIL_NOTIFICATIONS_SERVER_HOST=smtp.example.com
|
||||||
|
EMAIL_NOTIFICATIONS_SERVER_PORT=587
|
||||||
|
NEXT_PUBLIC_EMAIL_NOTIFICATIONS_FROM_EMAIL=noreply@example.com
|
||||||
|
NEXT_PUBLIC_EMAIL_NOTIFICATIONS_FROM_NAME="John Smith"
|
||||||
|
|
||||||
|
|||||||
@@ -2,14 +2,13 @@ import prisma from 'libs/prisma'
|
|||||||
import { SendEmailOptions, SmtpCredentialsData } from 'models'
|
import { SendEmailOptions, SmtpCredentialsData } from 'models'
|
||||||
import { NextApiRequest, NextApiResponse } from 'next'
|
import { NextApiRequest, NextApiResponse } from 'next'
|
||||||
import { createTransport } from 'nodemailer'
|
import { createTransport } from 'nodemailer'
|
||||||
import { Options } from 'nodemailer/lib/smtp-transport'
|
|
||||||
import { decrypt, initMiddleware } from 'utils'
|
import { decrypt, initMiddleware } from 'utils'
|
||||||
|
|
||||||
import Cors from 'cors'
|
import Cors from 'cors'
|
||||||
|
|
||||||
const cors = initMiddleware(Cors())
|
const cors = initMiddleware(Cors())
|
||||||
|
|
||||||
const defaultTransportOptions: Options = {
|
const defaultTransportOptions = {
|
||||||
host: process.env.EMAIL_NOTIFICATIONS_SERVER_HOST,
|
host: process.env.EMAIL_NOTIFICATIONS_SERVER_HOST,
|
||||||
port: Number(process.env.EMAIL_NOTIFICATIONS_SERVER_PORT),
|
port: Number(process.env.EMAIL_NOTIFICATIONS_SERVER_PORT),
|
||||||
secure: false,
|
secure: false,
|
||||||
@@ -19,7 +18,10 @@ const defaultTransportOptions: Options = {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultFrom = `"${process.env.NEXT_PUBLIC_EMAIL_NOTIFICATIONS_FROM_NAME}" <${process.env.NEXT_PUBLIC_EMAIL_NOTIFICATIONS_FROM_EMAIL}>`
|
const defaultFrom = {
|
||||||
|
name: process.env.NEXT_PUBLIC_EMAIL_NOTIFICATIONS_FROM_NAME,
|
||||||
|
email: process.env.NEXT_PUBLIC_EMAIL_NOTIFICATIONS_FROM_EMAIL,
|
||||||
|
}
|
||||||
|
|
||||||
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||||
await cors(req, res)
|
await cors(req, res)
|
||||||
@@ -27,35 +29,23 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
const { credentialsId, recipients, body, subject } = JSON.parse(
|
const { credentialsId, recipients, body, subject } = JSON.parse(
|
||||||
req.body
|
req.body
|
||||||
) as SendEmailOptions
|
) as SendEmailOptions
|
||||||
const credentials = await prisma.credentials.findUnique({
|
|
||||||
where: { id: credentialsId },
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!credentials)
|
const { host, port, isTlsEnabled, username, password, from } =
|
||||||
|
(await getEmailInfo(credentialsId)) ?? {}
|
||||||
|
if (!from)
|
||||||
return res.status(404).send({ message: "Couldn't find credentials" })
|
return res.status(404).send({ message: "Couldn't find credentials" })
|
||||||
const { host, port, isTlsEnabled, username, password, from } = decrypt(
|
|
||||||
credentials.data,
|
|
||||||
credentials.iv
|
|
||||||
) as SmtpCredentialsData
|
|
||||||
|
|
||||||
const transporter = createTransport(
|
const transporter = createTransport({
|
||||||
credentialsId === 'default'
|
host,
|
||||||
? defaultTransportOptions
|
port,
|
||||||
: {
|
secure: isTlsEnabled ?? undefined,
|
||||||
host,
|
auth: {
|
||||||
port,
|
user: username,
|
||||||
secure: isTlsEnabled ?? undefined,
|
pass: password,
|
||||||
auth: {
|
},
|
||||||
user: username,
|
})
|
||||||
pass: password,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
)
|
|
||||||
const info = await transporter.sendMail({
|
const info = await transporter.sendMail({
|
||||||
from:
|
from: `"${from.name}" <${from.email}>`,
|
||||||
credentialsId === 'default'
|
|
||||||
? defaultFrom
|
|
||||||
: `"${from.name}" <${from.email}>`,
|
|
||||||
to: recipients.join(', '),
|
to: recipients.join(', '),
|
||||||
subject,
|
subject,
|
||||||
text: body,
|
text: body,
|
||||||
@@ -65,4 +55,23 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
export default handler
|
export default handler
|
||||||
|
|||||||
Reference in New Issue
Block a user