const express = require('express'); const nodemailer = require('nodemailer'); const cors = require('cors'); const app = express(); app.use(cors()); app.use(express.json()); // SMTP Configuration from environment variables const transporter = nodemailer.createTransport({ host: process.env.SMTP_ADDRESS || 'smtp.gmail.com', port: parseInt(process.env.SMTP_PORT || '587'), secure: false, // true for 465, false for other ports auth: { user: process.env.SMTP_USER_NAME, pass: process.env.SMTP_PASSWORD, }, }); // Verify SMTP connection on startup transporter.verify((error) => { if (error) { console.error('SMTP connection error:', error); } else { console.log('SMTP server is ready to send emails'); } }); // Health check endpoint app.get('/health', (req, res) => { res.json({ status: 'ok' }); }); // Send email endpoint app.post('/send', async (req, res) => { const { to, subject, html, text } = req.body; if (!to || !subject || (!html && !text)) { return res.status(400).json({ error: 'Missing required fields: to, subject, and html or text' }); } try { const info = await transporter.sendMail({ from: `"Traceability Dashboard" <${process.env.SMTP_USER_NAME}>`, to, subject, text, html, }); console.log('Email sent:', info.messageId); res.json({ success: true, messageId: info.messageId }); } catch (error) { console.error('Failed to send email:', error); res.status(500).json({ error: 'Failed to send email', details: error.message }); } }); // Predefined email templates app.post('/send/welcome', async (req, res) => { const { to, username } = req.body; if (!to || !username) { return res.status(400).json({ error: 'Missing required fields: to, username' }); } try { const info = await transporter.sendMail({ from: `"Traceability Dashboard" <${process.env.SMTP_USER_NAME}>`, to, subject: 'Welcome to Traceability Dashboard - Registration Pending', html: `

Welcome, ${username}!

Thank you for registering at the Traceability Dashboard.

Your registration is pending admin approval. You will receive another email once your account has been approved.


This is an automated message from NABD Solutions.

`, }); console.log('Welcome email sent:', info.messageId); res.json({ success: true, messageId: info.messageId }); } catch (error) { console.error('Failed to send welcome email:', error); res.status(500).json({ error: 'Failed to send email', details: error.message }); } }); app.post('/send/admin-notification', async (req, res) => { const { username, email } = req.body; if (!username || !email) { return res.status(400).json({ error: 'Missing required fields: username, email' }); } const adminEmail = process.env.ADMIN_EMAIL || 'support@nabd-co.com'; try { const info = await transporter.sendMail({ from: `"Traceability Dashboard" <${process.env.SMTP_USER_NAME}>`, to: adminEmail, subject: 'New User Registration - Pending Approval', html: `

New User Registration

A new user has registered and is awaiting approval:

Please log in to the admin dashboard to approve or reject this registration.


This is an automated message from NABD Solutions.

`, }); console.log('Admin notification sent:', info.messageId); res.json({ success: true, messageId: info.messageId }); } catch (error) { console.error('Failed to send admin notification:', error); res.status(500).json({ error: 'Failed to send email', details: error.message }); } }); app.post('/send/approval', async (req, res) => { const { to, username } = req.body; if (!to || !username) { return res.status(400).json({ error: 'Missing required fields: to, username' }); } try { const info = await transporter.sendMail({ from: `"Traceability Dashboard" <${process.env.SMTP_USER_NAME}>`, to, subject: 'Account Approved - Traceability Dashboard', html: `

Account Approved!

Congratulations, ${username}!

Your account has been approved. You can now log in to the Traceability Dashboard.


This is an automated message from NABD Solutions.

`, }); console.log('Approval email sent:', info.messageId); res.json({ success: true, messageId: info.messageId }); } catch (error) { console.error('Failed to send approval email:', error); res.status(500).json({ error: 'Failed to send email', details: error.message }); } }); app.post('/send/rejection', async (req, res) => { const { to, username, reason } = req.body; if (!to || !username) { return res.status(400).json({ error: 'Missing required fields: to, username' }); } try { const info = await transporter.sendMail({ from: `"Traceability Dashboard" <${process.env.SMTP_USER_NAME}>`, to, subject: 'Registration Update - Traceability Dashboard', html: `

Registration Update

Hello ${username},

Unfortunately, your registration request was not approved.

${reason ? `

Reason: ${reason}

` : ''}

If you believe this was a mistake, please contact the administrator.


This is an automated message from NABD Solutions.

`, }); console.log('Rejection email sent:', info.messageId); res.json({ success: true, messageId: info.messageId }); } catch (error) { console.error('Failed to send rejection email:', error); res.status(500).json({ error: 'Failed to send email', details: error.message }); } }); app.post('/send/member-added', async (req, res) => { const { to, username, addedBy } = req.body; if (!to || !username) { return res.status(400).json({ error: 'Missing required fields: to, username' }); } try { const info = await transporter.sendMail({ from: `"Traceability Dashboard" <${process.env.SMTP_USER_NAME}>`, to, subject: 'You have been added to Traceability Dashboard', html: `

Welcome to Traceability Dashboard!

Hello ${username},

You have been added to the Traceability Dashboard${addedBy ? ` by ${addedBy}` : ''}.

You can now log in using your credentials.


This is an automated message from NABD Solutions.

`, }); console.log('Member added email sent:', info.messageId); res.json({ success: true, messageId: info.messageId }); } catch (error) { console.error('Failed to send member added email:', error); res.status(500).json({ error: 'Failed to send email', details: error.message }); } }); app.post('/send/member-removed', async (req, res) => { const { to, username } = req.body; if (!to || !username) { return res.status(400).json({ error: 'Missing required fields: to, username' }); } try { const info = await transporter.sendMail({ from: `"Traceability Dashboard" <${process.env.SMTP_USER_NAME}>`, to, subject: 'Account Update - Traceability Dashboard', html: `

Account Update

Hello ${username},

Your access to the Traceability Dashboard has been removed.

If you believe this was a mistake, please contact the administrator.


This is an automated message from NABD Solutions.

`, }); console.log('Member removed email sent:', info.messageId); res.json({ success: true, messageId: info.messageId }); } catch (error) { console.error('Failed to send member removed email:', error); res.status(500).json({ error: 'Failed to send email', details: error.message }); } }); const PORT = process.env.PORT || 3001; app.listen(PORT, () => { console.log(`Email service running on port ${PORT}`); });