Spaces:
Sleeping
Sleeping
| const express = require('express'); | |
| const nodemailer = require('nodemailer'); | |
| const cors = require('cors'); | |
| require('dotenv').config(); | |
| const app = express(); | |
| const PORT = process.env.PORT || 7860; | |
| // Middleware | |
| app.use(cors()); | |
| app.use(express.json()); | |
| // Transporter setup | |
| const transporter = nodemailer.createTransport({ | |
| service: 'gmail', | |
| auth: { | |
| user: process.env.EMAIL_USER, | |
| pass: process.env.EMAIL_PASS | |
| } | |
| }); | |
| // Verify transporter connection | |
| transporter.verify((error) => { | |
| if (error) { | |
| console.log('Error connecting to email service:', error); | |
| } else { | |
| console.log('Server is ready to take messages'); | |
| } | |
| }); | |
| // Route to send email | |
| app.post('/send-email', async (req, res) => { | |
| const { name, email, phone, message } = req.body; | |
| if (!name || !email || !message) { | |
| return res.status(400).send('Missing required fields'); | |
| } | |
| const mailOptions = { | |
| from: process.env.EMAIL_USER, | |
| to: process.env.EMAIL_USER, // or another email | |
| replyTo: email, | |
| subject: 'New Message from Portfolio', | |
| text: ` | |
| Name: ${name} | |
| Email: ${email} | |
| Phone: ${phone} | |
| Message: | |
| ${message} | |
| `, | |
| html: ` | |
| <h3>New Message from Portfolio</h3> | |
| <p><strong>Name:</strong> ${name}</p> | |
| <p><strong>Email:</strong> ${email}</p> | |
| <p><strong>Phone:</strong> ${phone}</p> | |
| <p><strong>Message:</strong></p> | |
| <p>${message}</p> | |
| ` | |
| }; | |
| try { | |
| await transporter.sendMail(mailOptions); | |
| res.status(200).send('Message sent successfully!'); | |
| } catch (error) { | |
| console.error('Error sending email:', error); | |
| res.status(500).send('Failed to send message.'); | |
| } | |
| }); | |
| app.listen(PORT, () => { | |
| console.log(`Server running on port ${PORT}`); | |
| }); |