63 lines
1.5 KiB
JavaScript
63 lines
1.5 KiB
JavaScript
const express = require('express');
|
|
const nodemailer = require('nodemailer');
|
|
const cors = require('cors');
|
|
require('dotenv').config();
|
|
|
|
const app = express();
|
|
const port = process.env.PORT || 3000;
|
|
|
|
// Middleware
|
|
app.use(cors());
|
|
app.use(express.json());
|
|
app.use(express.static('public'));
|
|
|
|
// Email transporter
|
|
const transporter = nodemailer.createTransport({
|
|
service: 'gmail',
|
|
auth: {
|
|
user: process.env.EMAIL_USER,
|
|
pass: process.env.EMAIL_PASS
|
|
}
|
|
});
|
|
|
|
// Contact form endpoint
|
|
app.post('/api/contact', async (req, res) => {
|
|
try {
|
|
const { name, email, subject, message } = req.body;
|
|
|
|
// Email options
|
|
const mailOptions = {
|
|
from: process.env.EMAIL_USER,
|
|
to: 'daniel.laforce@argobox.com',
|
|
subject: `Contact Form: ${subject}`,
|
|
text: `
|
|
Name: ${name}
|
|
Email: ${email}
|
|
Subject: ${subject}
|
|
|
|
Message:
|
|
${message}
|
|
`,
|
|
html: `
|
|
<h3>New Contact Form Submission</h3>
|
|
<p><strong>Name:</strong> ${name}</p>
|
|
<p><strong>Email:</strong> ${email}</p>
|
|
<p><strong>Subject:</strong> ${subject}</p>
|
|
<h4>Message:</h4>
|
|
<p>${message}</p>
|
|
`
|
|
};
|
|
|
|
// Send email
|
|
await transporter.sendMail(mailOptions);
|
|
|
|
res.status(200).json({ message: 'Message sent successfully!' });
|
|
} catch (error) {
|
|
console.error('Error sending email:', error);
|
|
res.status(500).json({ message: 'Failed to send message. Please try again.' });
|
|
}
|
|
});
|
|
|
|
app.listen(port, () => {
|
|
console.log(`Server running on port ${port}`);
|
|
});
|