Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Email functionality to Contact Us Page #119

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"next": "14.2.5",
"next-client-cookies": "^1.1.1",
"next-sitemap": "^4.2.3",
"nodemailer": "^6.9.15",
"react": "^18",
"react-dom": "^18",
"react-icons": "^5.2.1",
Expand Down
56 changes: 56 additions & 0 deletions src/app/api/sendEmail/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { NextRequest, NextResponse } from 'next/server';
import nodemailer from 'nodemailer';

export async function POST(req) {
try {
const body = await req.json();
const { name, email, message, contact, organisation, messageType } = body;
console.log(messageType);

// Determine if it's a bug or a message
const typeTitle = messageType==="Message" ? 'Message from User': 'Bug Report from User';
const typeMessage = messageType==="Message" ? 'Message' : 'Message/Issue';

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

const htmlContent = `
<div style="background-color:#f4f4f4;padding:20px;">
<div style="max-width:600px;margin:auto;background-color:white;padding:20px;border-radius:10px;box-shadow:0 4px 8px rgba(0, 0, 0, 0.1);">
<p style="margin-top: 20px; font-size: 12px; color: #777;">
This message was sent via the <strong>IIITV Coding Club Website</strong>.
</p>
<h1 style="background-color:#f88474;color:white;padding:10px;border-top-left-radius:10px;border-top-right-radius:10px;">${typeTitle}</h1>
<p><strong>Name:</strong> ${name}</p>
<p><strong>Email:</strong> ${email}</p>
<p><strong>Contact No.:</strong> ${contact}</p>
<p><strong>Organisation:</strong> ${organisation}</p>
<div style="background-color:#f9f9f9;padding:10px;border-radius:8px;margin:10px 0;">
<p><strong>${typeMessage}:</strong></p>
<p>${message}</p>
</div>
<a href="mailto:${email}" style="display:inline-block;background-color:#f88474;color:white;padding:5px 20px;border-radius:8px;text-decoration:none;margin-top:20px;">Reply to User</a>
</div>
</div>
`;

const mailOptions = {
from: email,
to: '[email protected]',
subject: `${typeTitle}: ${name}`,
html: htmlContent,
};

await transporter.sendMail(mailOptions);

return NextResponse.json({ success: true, message: 'Email sent successfully' });
} catch (error) {
console.error('❌ Error sending email:', error);
return NextResponse.json({ success: false, message: 'Failed to send email', error: error.message }, { status: 500 });
}
}
Loading
Loading