website/server/index.js
2025-09-29 08:24:59 +00:00

63 lines
2.2 KiB
JavaScript

import express from 'express';
import helmet from 'helmet';
import nodemailer from 'nodemailer';
import process from 'process';
import 'dotenv/config';
const app = express();
const port = process.env.PORT ? Number(process.env.PORT) : (process.env.SERVER_PORT ? Number(process.env.SERVER_PORT) : 3001);
app.disable('x-powered-by');
app.use(helmet({ contentSecurityPolicy: false }));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
const smtpHost = process.env.SMTP_HOST || '127.0.0.1';
const smtpPort = process.env.SMTP_PORT ? Number(process.env.SMTP_PORT) : 25;
const smtpSecure = String(process.env.SMTP_SECURE || 'false').toLowerCase() === 'true';
const smtpRejectUnauthorized = String(process.env.SMTP_TLS_REJECT_UNAUTHORIZED || 'false').toLowerCase() === 'true';
const smtpUser = process.env.SMTP_USER;
const smtpPassword = process.env.SMTP_PASSWORD;
const mailTransport = nodemailer.createTransport({
host: smtpHost,
port: smtpPort,
secure: smtpSecure,
auth: smtpUser && smtpPassword ? {
user: smtpUser,
pass: smtpPassword
} : undefined,
tls: { rejectUnauthorized: smtpRejectUnauthorized }
});
app.post('/contact', async (req, res) => {
const name = String(req.body.name || '').trim();
const email = String(req.body.email || '').trim();
const message = String(req.body.message || '').trim();
if (!name || !email || !message) {
return res.status(400).json({ ok: false, error: 'Missing fields' });
}
try {
await mailTransport.verify();
await mailTransport.sendMail({
from: process.env.SMTP_FROM || 'no-reply@4nk.network',
to: process.env.SMTP_TO || 'nicolas.cantu@4nk.network',
subject: `[4NK Contact] ${name}`,
replyTo: email,
text: `From: ${name} <${email}>\n\n${message}`,
});
return res.json({ ok: true });
} catch (err) {
console.error('Mail send failed:', err);
return res.status(502).json({ ok: false, error: 'Mail delivery failed' });
}
});
app.get('/health', (req, res) => res.json({ ok: true }));
app.listen(port, '127.0.0.1', () => {
console.log(`Mailer listening on http://127.0.0.1:${port}`);
});