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)
};
let mailOptions2 = {
from: 'donotreply@ofbbutte.com',
to: req.body.parentEmail,
subject: 'OFB - Vaction Bible School Registration',
html: generateVbsConfirmationEmail(req.body.firstName, req.body.parentFirstName)
}
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 {
transporter.sendMail(mailOptions2, (error, into) => {
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();
};
const formatDate = (dateString) => {
try {
if (!dateString || typeof dateString !== "string") {
throw new Error("Invalid date string.");
}
const [date] = dateString.split("T");
const [year, month, day] = date.split("-");
if (!year || !month || !day) {
throw new Error("Invalid date format.");
}
return `${month}/${day}/${year}`;
} catch (error) {
console.error("Error formatting date:", error.message);
return dateString;
}
}
// 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: formatDate(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 Information",
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) },
{ label: "Notes", value: sanitizeValue(data.notes) }
]
}
];
// Map the sections array into HTML tables (No filtering out fields!)
const htmlContent = sections.map(section => {
const rows = section.fields.map(field => `
|
${field.label}
|
${field.value}
|
`).join('');
return `
${section.title}
`;
}).join('');
return `
Vacation Bible School Registration
Vacation Bible School Registration
|
|
${htmlContent}
|
|
Old Fashion Baptist Church Vacation Bible School Registration
|
|
`;
}
function generateVbsConfirmationEmail(studentName, parentName) {
return `
${studentName}
has been registered for
Vacation Bible School!
We look forward to having ${studentName} join us for Bible lessons, games, snacks, and prizes!
Vacation Bible School Information
| Ages |
4 - 12 years old |
| Dates |
August 10 - 13 |
| Time |
6:30 PM - 8:00 PM |
| Location |
Old Fashion Baptist Church
5003 Wynne Ave
Butte, MT
|
| Contact |
(406) 494-5028 |
If you have any questions, please don't hesitate to contact us.
`;
}
module.exports = router;