Skip to content

Commit

Permalink
fix(editor): 🐛 Fix email step in viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Feb 11, 2022
1 parent 7c164e2 commit d19b26e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 36 deletions.
9 changes: 1 addition & 8 deletions apps/builder/.env.local.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,7 @@ AUTH_EMAIL_SERVER_PORT=587
AUTH_EMAIL_FROM_EMAIL=noreply@example.com
AUTH_EMAIL_FROM_NAME="John Smith"

# (Optional) Used for email notifications
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"

NEXT_PUBLIC_EMAIL_NOTIFICATIONS_FROM_EMAIL=

# Storage
# Used for uploading images, videos, etc...
Expand Down
11 changes: 11 additions & 0 deletions apps/viewer/.env.local.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,14 @@ ENCRYPTION_SECRET=
NEXT_PUBLIC_VIEWER_HOST=http://localhost:3001
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"

65 changes: 37 additions & 28 deletions apps/viewer/pages/api/integrations/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import prisma from 'libs/prisma'
import { SendEmailOptions, SmtpCredentialsData } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
import { createTransport } from 'nodemailer'
import { Options } from 'nodemailer/lib/smtp-transport'
import { decrypt, initMiddleware } from 'utils'

import Cors from 'cors'

const cors = initMiddleware(Cors())

const defaultTransportOptions: Options = {
const defaultTransportOptions = {
host: process.env.EMAIL_NOTIFICATIONS_SERVER_HOST,
port: Number(process.env.EMAIL_NOTIFICATIONS_SERVER_PORT),
secure: false,
Expand All @@ -19,43 +18,34 @@ 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) => {
await cors(req, res)
if (req.method === 'POST') {
const { credentialsId, recipients, body, subject } = JSON.parse(
req.body
) 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" })
const { host, port, isTlsEnabled, username, password, from } = decrypt(
credentials.data,
credentials.iv
) as SmtpCredentialsData

const transporter = createTransport(
credentialsId === 'default'
? defaultTransportOptions
: {
host,
port,
secure: isTlsEnabled ?? undefined,
auth: {
user: username,
pass: password,
},
}
)
const transporter = createTransport({
host,
port,
secure: isTlsEnabled ?? undefined,
auth: {
user: username,
pass: password,
},
})
const info = await transporter.sendMail({
from:
credentialsId === 'default'
? defaultFrom
: `"${from.name}" <${from.email}>`,
from: `"${from.name}" <${from.email}>`,
to: recipients.join(', '),
subject,
text: body,
Expand All @@ -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

3 comments on commit d19b26e

@vercel
Copy link

@vercel vercel bot commented on d19b26e Feb 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

viewer-v2 – ./apps/viewer

viewer-v2-typebot-io.vercel.app
viewer-v2-git-main-typebot-io.vercel.app
typebot-viewer.vercel.app

@vercel
Copy link

@vercel vercel bot commented on d19b26e Feb 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

landing-page-v2 – ./apps/landing-page

landing-page-v2-jade.vercel.app
landing-page-v2-typebot-io.vercel.app
landing-page-v2-git-main-typebot-io.vercel.app

@vercel
Copy link

@vercel vercel bot commented on d19b26e Feb 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

builder-v2 – ./apps/builder

builder-v2-git-main-typebot-io.vercel.app
next.typebot.io
builder-v2-typebot-io.vercel.app

Please sign in to comment.