-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
50 lines (39 loc) · 1.27 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const nodemailer = require('nodemailer');
const app = express();
const PORT = 5000;
app.use(bodyParser.json());
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
next();
});
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.GMAIL_USER,
pass: process.env.GMAIL_PASS,
},
});
app.post('/send-email', async (req, res) => {
const { name, email, message } = req.body;
if (!name || !email || !message) {
return res.status(400).send({ error: 'All fields are required!' });
}
try {
await transporter.sendMail({
from: `"${name}" <${email}>`,
to: process.env.MY_EMAIL,
subject: `Contact Form Submission from ${name}`,
text: message,
});
res.status(200).send({ success: 'Email sent successfully!' });
} catch (error) {
console.error('Error sending email:', error);
res.status(500).send({ error: 'Failed to send email.' });
}
});
app.listen(PORT, () => console.log(`Server running at http://localhost:${PORT}`));