var express = require('express'); var router = express.Router(); const nodemailer = require('nodemailer'); let transporter = nodemailer.createTransport({ host: 'smtp.ionos.com', port: 587, secure: false, auth:{ user: 'mail@ofbbutte.com', pass: '@2014OfbPwd' } }); router.get("/",function(req,res){ res.status(200).json({"message":"Hello World"}); return; }); router.post("/", function(req,res){ console.log(req.body); //This is the honeypot field //If it has something in it then we know it was filled out by a bot if (!req.body.hp || req.body.hp != '.'){ res.status(200).json({"status":200,"message":"Success!"}); return; } let mailOptions = { from: 'donotreply@ofbbutte.com', to: 'djabsher@gmail.com', subject: 'OFB - VBS Registration' + req.body.firstName, html: generateRegistrationEmailHTML(req.body) }; transporter.sendMail(mailOptions,(error, info) =>{ if (error){ console.log(error); res.status(400).json({"status":400,"message":"There was an error","error":error.response}); } else { res.status(200).json({"status":200,"message":"Success"}); } }); }); /** * Generates a sectioned HTML email string for the registration form. * Displays all fields even if they are empty. * @param {Object} data - The value object from this.form.value * @returns {string} Ready-to-send HTML string */ function generateRegistrationEmailHTML(data) { // Helper to cleanly format boolean true/false fields const formatBool = (val) => { if (val === true || val === 'true') return 'Yes'; if (val === false || val === 'false') return 'No'; return val; }; // Helper to ensure we don't render literal 'null' or 'undefined' text const sanitizeValue = (val) => { if (val === null || val === undefined) return ''; return String(val).trim(); }; // Define the layout sections and human-readable labels const sections = [ { title: "Student Information", fields: [ { label: "Student Name", value: `${sanitizeValue(data.firstName)} ${sanitizeValue(data.lastName)}`.trim() }, { label: "Grade", value: sanitizeValue(data.grade) }, { label: "Date of Birth", value: sanitizeValue(data.dob) }, { label: "Has Allergies?", value: sanitizeValue(formatBool(data.hasAllergies)) }, { label: "Allergies", value: sanitizeValue(data.allergies) }, { label: "Has Dietary Restrictions?", value: sanitizeValue(formatBool(data.hasDietaryRestrictions)) }, { label: "Dietary Restrictions", value: sanitizeValue(data.dietaryRestrictions) }, { label: "Attended in Past?", value: sanitizeValue(formatBool(data.attendedInPast)) }, { label: "Rides Bus?", value: sanitizeValue(formatBool(data.ridesBus)) } ] }, { title: "Parent / Guardian Contact", fields: [ { label: "Parent Name", value: `${sanitizeValue(data.parentFirstName)} ${sanitizeValue(data.parentLastName)}`.trim() }, { label: "Email", value: sanitizeValue(data.parentEmail) }, { label: "Mobile Phone", value: sanitizeValue(data.parentMobilePhone) }, { label: "Home Phone", value: sanitizeValue(data.parentHomePhone) }, { label: "Work Phone", value: sanitizeValue(data.parentWorkPhone) }, { label: "Address", value: `${sanitizeValue(data.address1)}${data.address2 ? ', ' + sanitizeValue(data.address2) : ''}, ${sanitizeValue(data.city)}, ${sanitizeValue(data.state)} ${sanitizeValue(data.zip)}`.trim().replace(/^,|,$/g, '') } ] }, { title: "Emergency Contact", fields: [ { label: "Same as Parent?", value: sanitizeValue(formatBool(data.emergencySameAsParent)) }, { label: "Emergency Name", value: `${sanitizeValue(data.emergencyFirstName)} ${sanitizeValue(data.emergencyLastName)}`.trim() }, { label: "Emergency Phone", value: sanitizeValue(data.emergencyPhone) } ] } ]; // Map the sections array into HTML tables (No filtering out fields!) const htmlContent = sections.map(section => { const rows = section.fields.map(field => `