diff --git a/README.md b/README.md index c918086..1927b4e 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,10 @@ Everything in `public/` is published by `npm run build`; delete `standalone.html `server/` is a small Express + SQL service that receives the webhook events (`POST /api/webhook`). Point `VITE_CRM_WEBHOOK_URL` at it and set `VITE_CRM_PROVIDER=webhook`. +It also emails a notification to `NOTIFY_EMAIL` (see `server/.env.example`) every time a +visitor registers or downloads an ROI report. Set `SMTP_HOST`/`SMTP_USER`/`SMTP_PASSWORD` +to enable it; leave `SMTP_HOST` blank to disable. + ## Project map ``` diff --git a/server/.env.example b/server/.env.example index 5c6ca30..5a8273f 100644 --- a/server/.env.example +++ b/server/.env.example @@ -12,3 +12,12 @@ DB_CA_PATH= ALLOWED_ORIGIN=https://toolkit.blackdice.ai PORT=8080 + +# SMTP settings used to notify the team whenever a visitor submits data +# (registration or ROI report download). Leave blank to disable notifications. +SMTP_HOST= +SMTP_PORT=587 +SMTP_USER= +SMTP_PASSWORD= +SMTP_FROM= +NOTIFY_EMAIL=mark.mullings@blackdice.ai diff --git a/server/index.js b/server/index.js index b161031..0b3389b 100644 --- a/server/index.js +++ b/server/index.js @@ -2,6 +2,7 @@ import 'dotenv/config'; import cors from 'cors'; import express from 'express'; import { pool } from './db.js'; +import { notifySubmission } from './mailer.js'; const app = express(); @@ -35,6 +36,7 @@ app.post('/api/webhook', async (req, res) => { } else { return res.status(400).json({ ok: false, error: `Unknown event: ${event}` }); } + notifySubmission({ event, lead, roi, submittedAt }).catch((err) => console.error('[mail] notify failed:', err)); res.json({ ok: true }); } catch (err) { console.error('[db] write failed:', err); diff --git a/server/mailer.js b/server/mailer.js new file mode 100644 index 0000000..53a9c40 --- /dev/null +++ b/server/mailer.js @@ -0,0 +1,61 @@ +import nodemailer from 'nodemailer'; + +const { SMTP_HOST, SMTP_PORT = '587', SMTP_USER, SMTP_PASSWORD, SMTP_FROM, NOTIFY_EMAIL } = process.env; + +const transporter = SMTP_HOST + ? nodemailer.createTransport({ + host: SMTP_HOST, + port: Number(SMTP_PORT), + secure: Number(SMTP_PORT) === 465, + auth: SMTP_USER ? { user: SMTP_USER, pass: SMTP_PASSWORD } : undefined, + }) + : null; + +/** Fire-and-forget notification sent whenever a visitor submits data. Never blocks the request. */ +export async function notifySubmission({ event, lead, roi, submittedAt }) { + if (!transporter || !NOTIFY_EMAIL) { + console.info('[mail] SMTP not configured — skipping submission notification'); + return; + } + + const subject = + event === 'roi_report_downloaded' + ? `ROI report downloaded — ${lead.email}` + : `New toolkit registration — ${lead.email}`; + + const lines = [ + `Event: ${event}`, + `Submitted at: ${submittedAt || new Date().toISOString()}`, + '', + `Name: ${[lead.first, lead.last].filter(Boolean).join(' ') || '—'}`, + `Email: ${lead.email}`, + `Company: ${lead.company || '—'}`, + `Title: ${lead.title || '—'}`, + `Region: ${lead.region || '—'}`, + `Subscriber band: ${lead.subs || '—'}`, + ]; + + if (roi?.results) { + lines.push( + '', + 'ROI results:', + ` Total: ${roi.formatted?.total ?? '—'}`, + ` Bandwidth savings: ${roi.formatted?.bw ?? '—'}`, + ` Support calls savings: ${roi.formatted?.calls ?? '—'}`, + ` Churn savings: ${roi.formatted?.churn ?? '—'}`, + ` Equipment savings: ${roi.formatted?.equip ?? '—'}`, + ` DDoS savings: ${roi.formatted?.ddos ?? '—'}` + ); + } + + try { + await transporter.sendMail({ + from: SMTP_FROM || SMTP_USER, + to: NOTIFY_EMAIL, + subject, + text: lines.join('\n'), + }); + } catch (err) { + console.error('[mail] failed to send submission notification:', err); + } +} diff --git a/server/package-lock.json b/server/package-lock.json index 49bfa17..22ceb13 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -11,7 +11,8 @@ "cors": "^2.8.5", "dotenv": "^16.4.5", "express": "^4.21.2", - "mysql2": "^3.11.5" + "mysql2": "^3.11.5", + "nodemailer": "^10.1.7" } }, "node_modules/@types/node": { @@ -661,6 +662,15 @@ "node": ">= 0.6" } }, + "node_modules/nodemailer": { + "version": "10.1.7", + "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-10.1.7.tgz", + "integrity": "sha512-OQa6FDln5fLttmqSbjDiRYvFDOFNrBqfiOwHPlEbURil3b+vPB1v8xpi+Sp46drzNDlsTnNsS502vthmz56r4w==", + "license": "MIT-0", + "engines": { + "node": ">=20.0.0" + } + }, "node_modules/object-assign": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", diff --git a/server/package.json b/server/package.json index 3266b5a..43ca9aa 100644 --- a/server/package.json +++ b/server/package.json @@ -11,6 +11,7 @@ "cors": "^2.8.5", "dotenv": "^16.4.5", "express": "^4.21.2", - "mysql2": "^3.11.5" + "mysql2": "^3.11.5", + "nodemailer": "^10.1.7" } }