@@ -2,30 +2,31 @@ import { NextRequest, NextResponse } from 'next/server';
22import { Resend } from 'resend' ;
33import { z } from 'zod' ;
44import EmailTemplate from '@/components/emails/EmailTemplate' ;
5- import { render } from '@react-email/render' ; // Usaremos render de @react -email/render
6- import React from 'react' ; // Importar React para el tipado
5+ import { render } from '@react-email/render' ;
6+ import React from 'react' ;
77
8- const resend = new Resend ( process . env . RESEND_API_KEY ) ;
8+ // NO inicializamos Resend aquí
9+ // const resend = new Resend(process.env.RESEND_API_KEY);
910
1011// Esquema de validación con Zod
1112const contactSchema = z . object ( {
1213 name : z . string ( ) . min ( 2 , 'El nombre debe tener al menos 2 caracteres.' ) ,
1314 email : z . string ( ) . email ( 'El correo electrónico no es válido.' ) ,
1415 message : z . string ( ) . min ( 10 , 'El mensaje debe tener al menos 10 caracteres.' ) ,
15- // Campo Honeypot para detectar bots
1616 honeypot : z . string ( ) . optional ( ) ,
1717} ) ;
1818
1919export async function POST ( req : NextRequest ) {
2020 try {
21+ // Inicializamos Resend DENTRO de la función POST
22+ const resend = new Resend ( process . env . RESEND_API_KEY ) ;
23+
2124 const body = await req . json ( ) ;
2225
23- // 1. Validar el campo Honeypot
2426 if ( body . honeypot ) {
2527 return NextResponse . json ( { message : 'Message received!' } , { status : 200 } ) ;
2628 }
2729
28- // 2. Validar los datos con Zod
2930 const parsed = contactSchema . safeParse ( body ) ;
3031
3132 if ( ! parsed . success ) {
@@ -35,16 +36,14 @@ export async function POST(req: NextRequest) {
3536
3637 const { name, email, message } = parsed . data ;
3738
38- // 3. Renderizar el componente de React a un string de HTML (ahora con await)
3939 const emailHtml = await render ( EmailTemplate ( { name, email, message } ) as React . ReactElement ) ;
4040
41- // 4. Enviar el correo con Resend usando la propiedad 'html'
4241 const { data, error } = await resend . emails . send ( {
43- from : 'Portfolio Contact <onboarding@resend.dev>' , // Dominio verificado en Resend
42+ from : 'Portfolio Contact <onboarding@resend.dev>' ,
4443 to : [ 'ing.javiernuma@gmail.com' ] ,
4544 subject : `Nuevo mensaje de ${ name } desde tu portafolio` ,
46- html : emailHtml , // Ahora emailHtml es un string resuelto
47- text : `Nombre: ${ name } \nEmail: ${ email } \nMensaje: ${ message } ` , // Versión de texto plano
45+ html : emailHtml ,
46+ text : `Nombre: ${ name } \nEmail: ${ email } \nMensaje: ${ message } ` ,
4847 } ) ;
4948
5049 if ( error ) {
0 commit comments