-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservesmail.js
61 lines (49 loc) · 1.47 KB
/
servesmail.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
51
52
53
54
55
56
57
58
59
const express = require('express');
const bodyParser = require('body-parser');
require('dotenv').config();
// Parse JSON bodies
const nodemailer = require('nodemailer');
const app = express();
app.use(express.static('public'));
app.use(bodyParser.json());
const path = require('path');
// Configuring Nodemailer with email service provider details
const transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: process.env.EMAIL,
pass: process.env.APP_PASSWORD
}
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname+"/public/contact.html"));
});
// Endpoint for handling the form submission
app.post('/send-email', (req, res) => {
const { email, subject, message } = req.body;
console.log(email);
console.log(req.body)
// Create the email content
const mailOptions = {
from: 'akshayne911@gmail.com',
to: 'akshayne911@gmail.com', // Replacing own email address
subject: subject,
text: `Email: ${req.body.email}\n\n${req.body.message}`
};
// Send the email
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.error(error);
res.status(500).json({ error: 'Error sending email' });
} else {
console.log('Email sent:', info.response);
res.json({ message: 'Email sent successfully' });
}
});
});
// Start the server
app.listen(3000, () => {
console.log('Server running on port 3000');
});