From a31ae22acc19ff54cf4fb0a18b43ba71c65d0151 Mon Sep 17 00:00:00 2001 From: evildevill Date: Mon, 18 Nov 2024 11:32:05 +0500 Subject: [PATCH] updated contact form to work with office 365 mail server --- .env.example | 3 ++- app/api/contact/route.js | 15 ++++++--------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/.env.example b/.env.example index d7c1bba..63a631c 100644 --- a/.env.example +++ b/.env.example @@ -1,8 +1,9 @@ #.env.local MONGO_URI=************************************** -EMAIL_SERVER=************************************** +EMAIL_SERVER=smtp.office365.com EMAIL_PORT=************************************** EMAIL_USER=************************************** +EMAIL_FROM=************************************** EMAIL_PASS=************************************** KINDE_CLIENT_ID=************************************** KINDE_CLIENT_SECRET=************************************** diff --git a/app/api/contact/route.js b/app/api/contact/route.js index 0776a71..1e45b1a 100644 --- a/app/api/contact/route.js +++ b/app/api/contact/route.js @@ -1,10 +1,9 @@ -// app/api/contact/route.js import nodemailer from 'nodemailer'; import { NextResponse } from 'next/server'; export async function POST(req) { try { - const body = await req.json(); // Parse JSON body + const body = await req.json(); const { name, email, message } = body; @@ -12,21 +11,20 @@ export async function POST(req) { return NextResponse.json({ message: 'Please fill in all the fields.' }, { status: 400 }); } - // Create a transporter const transporter = nodemailer.createTransport({ host: process.env.EMAIL_SERVER, port: process.env.EMAIL_PORT, - secure: process.env.EMAIL_USE_SSL === 'true', // true for 465, false for other ports + secure: process.env.EMAIL_USE_SSL === 'true', auth: { - user: process.env.EMAIL_USER, // Stored in .env - pass: process.env.EMAIL_PASS, // Stored in .env + user: process.env.EMAIL_USER, + pass: process.env.EMAIL_PASS, }, }); // Email content const mailOptions = { - from: `"Contact Form" <${process.env.EMAIL_USER}>`, // Sender address - to: process.env.EMAIL_USER, // Your email + from: `"Contact Form" `, + to: process.env.EMAIL_USER, subject: `New Contact Form Submission from ${name}`, text: `Name: ${name}\nEmail: ${email}\nMessage: ${message}`, html: `

Name: ${name}

@@ -34,7 +32,6 @@ export async function POST(req) {

Message:
${message}

`, }; - // Send the email await transporter.sendMail(mailOptions); return NextResponse.json({ message: 'Your message has been sent!' }, { status: 200 });