NEXT JS: ERHALTENJavaScript

Javascript-Forum
Anonymous
 NEXT JS: ERHALTEN

Post by Anonymous »

Ich habe Nutzung in der nächsten JS -App zum Senden der Mails, während es in der lokalen Umgebung arbeitet. Ich kann auch nicht debuggen. Es ist eine Post -API. < /P>

Code: Select all

import nodemailer from "nodemailer";

export async function POST(req) {
const { name, email, subject, message } = await req.json();

const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_APP_PASS,
},
});

try {
await transporter.sendMail(mailOptions);
return new Response("Email sent successfully", { status: 200 });
} catch (error) {
console.error("Failed to send email:", error.message);
return new Response("Failed to send email", { status: 500 });
}
}
< /code>
und ich nenne diese Post -API in einem Ereignisklick auf < /p>
const handleSubmit = async (e) => {
e.preventDefault();

const response = await fetch("/sendMail", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(connect),
});

if (response.ok) {
alert("Message sent successfully!");
setConnect({ name: "", email: "", subject: "", message: "" });
} else {
alert("Failed to send the message, please try again.");
}
};
Geben Sie hier die Bildbeschreibung ein

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post