Skip to content

Commit

Permalink
Swapped sendEmail to app/api
Browse files Browse the repository at this point in the history
  • Loading branch information
ludavidca committed Dec 27, 2024
1 parent 5f22cd1 commit e242737
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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

Expand Down
45 changes: 25 additions & 20 deletions src/pages/api/sendEmail.ts → app/api/sendEmail/route.ts
Original file line number Diff line number Diff line change
@@ -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<Data>
) {
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,
Expand Down Expand Up @@ -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 }
);
}
}

0 comments on commit e242737

Please sign in to comment.