Skip to content

Commit

Permalink
updated contact form to work with office 365 mail server
Browse files Browse the repository at this point in the history
  • Loading branch information
evildevill committed Nov 18, 2024
1 parent c49a5c4 commit a31ae22
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -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=**************************************
Expand Down
15 changes: 6 additions & 9 deletions app/api/contact/route.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
// 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;

if (!name || !email || !message) {
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" <[email protected]>`,
to: process.env.EMAIL_USER,
subject: `New Contact Form Submission from ${name}`,
text: `Name: ${name}\nEmail: ${email}\nMessage: ${message}`,
html: `<p><strong>Name:</strong> ${name}</p>
<p><strong>Email:</strong> ${email}</p>
<p><strong>Message:</strong><br>${message}</p>`,
};

// Send the email
await transporter.sendMail(mailOptions);

return NextResponse.json({ message: 'Your message has been sent!' }, { status: 200 });
Expand Down

0 comments on commit a31ae22

Please sign in to comment.