Files
2026-02-03 20:48:09 +01:00

260 lines
9.4 KiB
JavaScript

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: `
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
<h1 style="color: #333;">Welcome, ${username}!</h1>
<p>Thank you for registering at the Traceability Dashboard.</p>
<p>Your registration is pending admin approval. You will receive another email once your account has been approved.</p>
<hr style="border: none; border-top: 1px solid #eee; margin: 20px 0;" />
<p style="color: #666; font-size: 12px;">This is an automated message from NABD Solutions.</p>
</div>
`,
});
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: `
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
<h1 style="color: #333;">New User Registration</h1>
<p>A new user has registered and is awaiting approval:</p>
<ul>
<li><strong>Username:</strong> ${username}</li>
<li><strong>Email:</strong> ${email}</li>
</ul>
<p>Please log in to the admin dashboard to approve or reject this registration.</p>
<hr style="border: none; border-top: 1px solid #eee; margin: 20px 0;" />
<p style="color: #666; font-size: 12px;">This is an automated message from NABD Solutions.</p>
</div>
`,
});
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: `
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
<h1 style="color: #333;">Account Approved!</h1>
<p>Congratulations, ${username}!</p>
<p>Your account has been approved. You can now log in to the Traceability Dashboard.</p>
<hr style="border: none; border-top: 1px solid #eee; margin: 20px 0;" />
<p style="color: #666; font-size: 12px;">This is an automated message from NABD Solutions.</p>
</div>
`,
});
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: `
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
<h1 style="color: #333;">Registration Update</h1>
<p>Hello ${username},</p>
<p>Unfortunately, your registration request was not approved.</p>
${reason ? `<p><strong>Reason:</strong> ${reason}</p>` : ''}
<p>If you believe this was a mistake, please contact the administrator.</p>
<hr style="border: none; border-top: 1px solid #eee; margin: 20px 0;" />
<p style="color: #666; font-size: 12px;">This is an automated message from NABD Solutions.</p>
</div>
`,
});
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: `
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
<h1 style="color: #333;">Welcome to Traceability Dashboard!</h1>
<p>Hello ${username},</p>
<p>You have been added to the Traceability Dashboard${addedBy ? ` by ${addedBy}` : ''}.</p>
<p>You can now log in using your credentials.</p>
<hr style="border: none; border-top: 1px solid #eee; margin: 20px 0;" />
<p style="color: #666; font-size: 12px;">This is an automated message from NABD Solutions.</p>
</div>
`,
});
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: `
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
<h1 style="color: #333;">Account Update</h1>
<p>Hello ${username},</p>
<p>Your access to the Traceability Dashboard has been removed.</p>
<p>If you believe this was a mistake, please contact the administrator.</p>
<hr style="border: none; border-top: 1px solid #eee; margin: 20px 0;" />
<p style="color: #666; font-size: 12px;">This is an automated message from NABD Solutions.</p>
</div>
`,
});
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}`);
});