|
| 1 | +const express = require("express"); |
| 2 | +const cors = require("cors"); |
| 3 | +const nodemailer = require("nodemailer"); |
| 4 | +require("dotenv").config(); |
| 5 | + |
| 6 | +const app = express(); |
| 7 | +const port = Number(process.env.PORT || 8787); |
| 8 | +const defaultAllowedOrigins = [ |
| 9 | + "https://ziongospelministry.org", |
| 10 | + "http://127.0.0.1:4177", |
| 11 | + "http://localhost:4177" |
| 12 | +]; |
| 13 | + |
| 14 | +const allowedOrigins = String(process.env.ALLOWED_ORIGINS || "") |
| 15 | + .split(",") |
| 16 | + .map((x) => x.trim()) |
| 17 | + .filter(Boolean); |
| 18 | + |
| 19 | +const origins = allowedOrigins.length ? allowedOrigins : defaultAllowedOrigins; |
| 20 | + |
| 21 | +app.use(cors({ |
| 22 | + origin(origin, callback) { |
| 23 | + if (!origin || origins.includes(origin)) { |
| 24 | + callback(null, true); |
| 25 | + return; |
| 26 | + } |
| 27 | + callback(new Error("Origin not allowed")); |
| 28 | + } |
| 29 | +})); |
| 30 | + |
| 31 | +app.use(express.json({ limit: "100kb" })); |
| 32 | + |
| 33 | +function buildTransporter() { |
| 34 | + const host = process.env.SMTP_HOST; |
| 35 | + const portNumber = Number(process.env.SMTP_PORT || 587); |
| 36 | + const secure = String(process.env.SMTP_SECURE || "false").toLowerCase() === "true"; |
| 37 | + const user = process.env.SMTP_USER; |
| 38 | + const pass = process.env.SMTP_PASS; |
| 39 | + |
| 40 | + if (!host || !user || !pass) { |
| 41 | + throw new Error("Missing SMTP configuration."); |
| 42 | + } |
| 43 | + |
| 44 | + return nodemailer.createTransport({ |
| 45 | + host, |
| 46 | + port: portNumber, |
| 47 | + secure, |
| 48 | + auth: { user, pass } |
| 49 | + }); |
| 50 | +} |
| 51 | + |
| 52 | +function validatePayload(body) { |
| 53 | + const payload = { |
| 54 | + name: String(body.name || "").trim(), |
| 55 | + email: String(body.email || "").trim(), |
| 56 | + subject: String(body.subject || "").trim(), |
| 57 | + message: String(body.message || "").trim() |
| 58 | + }; |
| 59 | + |
| 60 | + if (!payload.name || !payload.email || !payload.message) { |
| 61 | + return { error: "Name, email, and message are required." }; |
| 62 | + } |
| 63 | + |
| 64 | + if (!/^\S+@\S+\.\S+$/.test(payload.email)) { |
| 65 | + return { error: "Invalid email address." }; |
| 66 | + } |
| 67 | + |
| 68 | + if (payload.message.length > 8000) { |
| 69 | + return { error: "Message is too long." }; |
| 70 | + } |
| 71 | + |
| 72 | + return { payload }; |
| 73 | +} |
| 74 | + |
| 75 | +app.get("/api/health", (_req, res) => { |
| 76 | + res.status(200).json({ ok: true }); |
| 77 | +}); |
| 78 | + |
| 79 | +app.post("/api/contact", async (req, res) => { |
| 80 | + const { payload, error } = validatePayload(req.body || {}); |
| 81 | + if (error) { |
| 82 | + res.status(400).json({ ok: false, error }); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + const to = process.env.CONTACT_TO || "vinodraj.j@gmail.com"; |
| 87 | + const from = process.env.FROM_EMAIL || process.env.SMTP_USER; |
| 88 | + const subject = payload.subject || "New message from ziongospelministry.org"; |
| 89 | + |
| 90 | + const text = [ |
| 91 | + `Name: ${payload.name}`, |
| 92 | + `Email: ${payload.email}`, |
| 93 | + "", |
| 94 | + payload.message |
| 95 | + ].join("\n"); |
| 96 | + |
| 97 | + const html = ` |
| 98 | + <h2>New Contact Message</h2> |
| 99 | + <p><strong>Name:</strong> ${payload.name}</p> |
| 100 | + <p><strong>Email:</strong> ${payload.email}</p> |
| 101 | + <p><strong>Message:</strong></p> |
| 102 | + <p>${payload.message.replace(/\n/g, "<br>")}</p> |
| 103 | + `; |
| 104 | + |
| 105 | + try { |
| 106 | + const transporter = buildTransporter(); |
| 107 | + await transporter.sendMail({ |
| 108 | + to, |
| 109 | + from, |
| 110 | + replyTo: payload.email, |
| 111 | + subject, |
| 112 | + text, |
| 113 | + html |
| 114 | + }); |
| 115 | + |
| 116 | + res.status(200).json({ ok: true }); |
| 117 | + } catch (err) { |
| 118 | + console.error("Email send failed:", err.message); |
| 119 | + res.status(500).json({ ok: false, error: "Unable to send message." }); |
| 120 | + } |
| 121 | +}); |
| 122 | + |
| 123 | +app.use((_req, res) => { |
| 124 | + res.status(404).json({ ok: false, error: "Not found" }); |
| 125 | +}); |
| 126 | + |
| 127 | +app.listen(port, () => { |
| 128 | + console.log(`Contact API running on port ${port}`); |
| 129 | +}); |
0 commit comments