-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated contact form to work with office 365 mail server
- Loading branch information
1 parent
c49a5c4
commit a31ae22
Showing
2 changed files
with
8 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
|