const express = require('express'); const cors = require('cors'); const path = require('path'); const nodemailer = require('nodemailer'); const app = express(); const PORT = process.env.PORT || 3000; app.use(cors()); app.use(express.json()); app.use(express.urlencoded({ extended: true })); // Serve a pasta frontend app.use(express.static(path.join(__dirname, '../frontend'), { extensions: ['html'] })); // Rotas das 4 páginas app.get('/', (req, res) => res.sendFile(path.join(__dirname, '../frontend/index.html'))); app.get('/about', (req, res) => res.sendFile(path.join(__dirname, '../frontend/about.html'))); app.get('/careers', (req, res) => res.sendFile(path.join(__dirname, '../frontend/careers.html'))); app.get('/contact', (req, res) => res.sendFile(path.join(__dirname, '../frontend/contact.html'))); // Configuração do transporte de E-mail const transporter = nodemailer.createTransport({ host: 'mail.limpezascleanandcare.com', port: 465, secure: true, auth: { user: 'teste@limpezascleanandcare.com', pass: 'HTC?D=}5&cHoTrH=' }, tls: { rejectUnauthorized: false // Ignora avisos de certificado no servidor SMTP } }); // Endpoint do Formulário de Contacto (Com Recetor Dinâmico) app.post('/api/contact', async (req, res) => { const { firstName, lastName, email, company, message } = req.body; if (!email || !message) { return res.status(400).json({ error: 'Email and message are required.' }); } try { // Construção do Email - Agora enviado para o e-mail inserido no formulário const mailOptions = { from: '"Apex Solutions" ', // Remetente fixo válido to: email, // RECETOR DINÂMICO (Vem diretamente do campo Email Address) subject: `Confirmation: We received your request, ${firstName}!`, html: `

Hello ${firstName} ${lastName},

Thank you for reaching out to Apex Solutions. This is an automated confirmation to let you know that we have successfully received your inquiry.

Summary of your submission:

Subject/Company: ${company || 'Not specified'}

"${message}"

Our advisory team will review your details and get back to you within 24 business hours.


Best regards,
Apex Solutions Corporate Team

` }; // Envia o e-mail real const info = await transporter.sendMail(mailOptions); console.log(`[Email Sent Automatically to ${email}] Message ID: ${info.messageId}`); res.status(200).json({ success: true, message: 'Thank you! A confirmation email has been dispatched to your address.' }); } catch (error) { console.error(`[Email Error]:`, error); res.status(500).json({ error: 'Internal server error. Could not send confirmation email.' }); } }); // Export the app for testing, but only listen if run directly if (require.main === module) { app.listen(PORT, () => { console.log(`[Apex Server] Running on port ${PORT}`); }); } module.exports = app;