Fix the existing contact page on my DeepSite site so the contact form's "Submit" button actually sends the submitted contact information to the site owner email: [email protected]. Do not expose any secrets in the repo; use environment variables for API keys and owner email where appropriate.
7bcbf5a
verified
| const fs = require('fs'); | |
| const path = require('path'); | |
| const sgMail = require('@sendgrid/mail'); | |
| // Configure email service | |
| sgMail.setApiKey(process.env.SENDGRID_API_KEY); | |
| // Ensure data directory exists | |
| const dataDir = path.join(__dirname, '../data'); | |
| if (!fs.existsSync(dataDir)) { | |
| fs.mkdirSync(dataDir, { recursive: true }); | |
| } | |
| const submissionsFile = path.join(dataDir, 'submissions.json'); | |
| // Simple email validation | |
| function isValidEmail(email) { | |
| return /^\S+@\S+\.\S+$/.test(email); | |
| } | |
| // Append submission to JSON file | |
| function logSubmission(data) { | |
| let submissions = []; | |
| if (fs.existsSync(submissionsFile)) { | |
| submissions = JSON.parse(fs.readFileSync(submissionsFile)); | |
| } | |
| submissions.push({ | |
| ...data, | |
| timestamp: new Date().toISOString(), | |
| ip: req.ip | |
| }); | |
| fs.writeFileSync(submissionsFile, JSON.stringify(submissions, null, 2)); | |
| } | |
| module.exports = async (req, res) => { | |
| if (req.method !== 'POST') { | |
| return res.status(405).json({ error: 'Method not allowed' }); | |
| } | |
| try { | |
| const { name, email, message, phone, service, website, sourcePage } = req.body; | |
| // Honeypot check | |
| if (website) { | |
| return res.status(200).json({ ok: true }); | |
| } | |
| // Validation | |
| if (!name || !name.trim()) { | |
| return res.status(400).json({ error: 'Please enter your name' }); | |
| } | |
| if (!email || !isValidEmail(email)) { | |
| return res.status(400).json({ error: 'Please enter a valid email' }); | |
| } | |
| if (!message || !message.trim()) { | |
| return res.status(400).json({ error: 'Please enter your message' }); | |
| } | |
| // Prepare email | |
| const emailContent = ` | |
| <h2>New Contact Form Submission</h2> | |
| <p><strong>Name:</strong> ${name}</p> | |
| <p><strong>Email:</strong> <a href="mailto:${email}">${email}</a></p> | |
| ${phone ? `<p><strong>Phone:</strong> ${phone}</p>` : ''} | |
| ${service ? `<p><strong>Service:</strong> ${service}</p>` : ''} | |
| <p><strong>Message:</strong></p> | |
| <p>${message}</p> | |
| <hr> | |
| <p>Submitted from: ${sourcePage || 'Unknown page'}</p> | |
| `; | |
| // Send email | |
| if (process.env.SENDGRID_API_KEY) { | |
| await sgMail.send({ | |
| to: process.env.OWNER_EMAIL || '[email protected]', | |
| from: process.env.FROM_EMAIL || '[email protected]', | |
| subject: `New contact form submission - ${name}`, | |
| html: emailContent | |
| }); | |
| } | |
| // Log submission | |
| logSubmission(req.body); | |
| return res.status(200).json({ ok: true }); | |
| } catch (error) { | |
| console.error('Error processing contact form:', error); | |
| return res.status(500).json({ error: 'Internal server error' }); | |
| } | |
| }; |