From e242737f522023c04e67e206aef7dfb9452e8d3c Mon Sep 17 00:00:00 2001 From: ludavidca Date: Thu, 26 Dec 2024 17:08:28 -0700 Subject: [PATCH] Swapped sendEmail to app/api --- Dockerfile | 3 ++ .../api/sendEmail/route.ts | 45 ++++++++++--------- 2 files changed, 28 insertions(+), 20 deletions(-) rename src/pages/api/sendEmail.ts => app/api/sendEmail/route.ts (63%) diff --git a/Dockerfile b/Dockerfile index 885298c..cd564c6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,9 @@ # Use the official Node.js image FROM node:20-alpine +#Add OpenSSL +RUN apk add --no-cache openssl openssl-dev + # Set the working directory WORKDIR /sistema diff --git a/src/pages/api/sendEmail.ts b/app/api/sendEmail/route.ts similarity index 63% rename from src/pages/api/sendEmail.ts rename to app/api/sendEmail/route.ts index 226cbe3..e9f8e55 100644 --- a/src/pages/api/sendEmail.ts +++ b/app/api/sendEmail/route.ts @@ -1,25 +1,26 @@ -import { NextApiRequest, NextApiResponse } from 'next'; +import { NextRequest, NextResponse } from 'next/server'; import nodemailer from 'nodemailer'; -type Data = { - message?: string; - error?: string; - details?: string; +type EmailBody = { + to: string; + subject: string; + text: string; }; -export default async function handler( - req: NextApiRequest, - res: NextApiResponse -) { - const { to, subject, text } = req.body; +export async function POST(request: NextRequest) { + try { + const body: EmailBody = await request.json(); + const { to, subject, text } = body; - // Validate the input - if (!to || !subject || !text) { - return res.status(400).json({ error: 'Missing required fields' }); - } + // Validate the input + if (!to || !subject || !text) { + return NextResponse.json( + { error: 'Missing required fields' }, + { status: 400 } + ); + } - // Create a transporter object - try { + // Create a transporter object const transporter = nodemailer.createTransport({ host: 'smtp.gmail.com', port: 465, @@ -64,10 +65,14 @@ export default async function handler( }); }); - res.status(200).json({ message: 'Email sent successfully' }); + return NextResponse.json( + { message: 'Email sent successfully' }, + { status: 200 } + ); } catch (error: any) { - res - .status(500) - .json({ error: 'Error sending email', details: error.message }); + return NextResponse.json( + { error: 'Error sending email', details: error.message }, + { status: 500 } + ); } }