From 7f586332a034990e048f2cf6bea6969864337ef1 Mon Sep 17 00:00:00 2001 From: Adithya KP Date: Sun, 7 Jun 2026 18:38:02 +0530 Subject: [PATCH] feat: implement email notifications for contact form using nodemailer --- server.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/server.js b/server.js index de73440..0652b0f 100644 --- a/server.js +++ b/server.js @@ -5,6 +5,24 @@ const rateLimit = require('express-rate-limit'); const fs = require('fs-extra'); const morgan = require('morgan'); const logger = require('./utils/logger'); +const nodemailer = require('nodemailer'); + +// Configure Nodemailer Transporter using environment variables +const transporter = nodemailer.createTransport({ + host: process.env.SMTP_HOST || 'smtp.ethereal.email', + port: parseInt(process.env.SMTP_PORT, 10) || 587, + secure: process.env.SMTP_SECURE === 'true', // true for 465, false for other ports + auth: { + user: process.env.SMTP_USER || null, + pass: process.env.SMTP_PASS || null + } +}); + +const isMailConfigured = !!(process.env.SMTP_USER && process.env.SMTP_PASS); +if (!isMailConfigured) { + logger.warn('SMTP credentials not configured. Email notifications will be logged instead of sent.'); +} + const app = express(); const PORT = process.env.PORT || 3000; @@ -87,6 +105,36 @@ app.post('/contact', contactLimiter, [ // Write back to file await fs.writeJson(filePath, contacts, { spaces: 2 }); + // Send email notification + const mailOptions = { + from: process.env.CONTACT_SENDER || 'no-reply@nexuscore.io', + to: process.env.CONTACT_RECEIVER || 'admin@nexuscore.io', + subject: `New Contact Form Submission: ${subject}`, + text: `Name: ${name}\nEmail: ${email}\nSubject: ${subject}\n\nMessage:\n${message}`, + html: ` +
+

New Contact Form Submission

+

Name: ${name}

+

Email: ${email}

+

Subject: ${subject}

+
+

Message:

+
${message}
+
+ ` + }; + + if (isMailConfigured) { + try { + await transporter.sendMail(mailOptions); + logger.info('Email notification sent successfully.'); + } catch (mailErr) { + logger.error('Failed to send email notification', { error: mailErr.message, stack: mailErr.stack }); + } + } else { + logger.info(`SMTP not configured. Logged notification details: ${JSON.stringify(mailOptions, null, 2)}`); + } + res.json({ success: true, message: 'Message received successfully!' }); } catch (err) { logger.error('Error saving contact submission', { error: err.message, stack: err.stack });