-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
145 lines (116 loc) · 3.96 KB
/
app.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const express = require('express');
const bodyParser = require('body-parser');
const nodemailer = require('nodemailer');
const cors = require('cors');
const path = require('path');
const { DateTime } = require('luxon');
const app = express();
const port = process.env.PORT || 3000;
const mongoose = require('mongoose');
const dotenv = require('dotenv');
const ical = require('ical-generator');
const { log } = require('console');
dotenv.config({ path: '/.env' });
require('dotenv').config();
console.log(typeof (ical));
app.use(cors())
app.use(bodyParser.urlencoded({ extended: false }));
app.use('/asset', express.static(path.join(__dirname, 'asset')))
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
const mongo_URI = 'mongodb+srv://SAC:[email protected]/microsite';
mongoose.connect(mongo_URI, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(result => {
console.log('Connected to the MongoDB database');
})
.catch(error => {
console.error('Error connecting to the MongoDB database:', error);
});
const dataSchema = new mongoose.Schema({
firstname: String,
lastname: String,
email: String,
phone: String,
company: String,
designation: String,
submissionTime: String,
});
const Data = mongoose.model('Data', dataSchema);
console.log('database data', Data);
app.post('/send-email', (req, res) => {
const { firstname,lastname, email, phone, company, designation } = req.body;
const submissionTime = DateTime.now().setZone('Asia/Kolkata').toISO();
const newData = new Data({
firstname: firstname,
lastname: lastname,
email: email,
phone: phone,
company: company,
designation: designation,
submissionTime: submissionTime,
});
// res.sendFile(path.join(__dirname, 'display.html'));
console.log('data going for mongodb', newData);
newData.save()
.then(() => {
console.log('Data saved to MongoDB');
})
.catch((err) => {
console.error('Error saving data to MongoDB:', err);
});
const cal = new ical.ICalCalendar();
cal.createEvent({
start: new Date(),
end: new Date(new Date().getTime() + 3600000),
summary: 'Meeting Invitation',
description: 'This is a meeting invitation.',
organizer: 'Qualcomm <[email protected]>',
location: 'Meeting Location',
});
const icsContent = cal.toString();
const icsBuffer = Buffer.from(icsContent, 'utf-8');
console.log('iCalendar Content as Buffer:', icsBuffer);
var transporter = nodemailer.createTransport({
host: 'smtpout.secureserver.net',
// service: 'Godaddy',
// secureConnection: false,
secure: true,
port: 465,
auth: {
user: '[email protected]',
pass: "xb/YQN.G5MC/Ya_"
}
});
console.log(email);
var mailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: 'Meeting Invitation',
text: `Hello,\n\nThank you for reaching out to us. We have received your contact information:\nEmail: ${email}\nPhone: ${phone}\n\nBest regards,\nYour Company`,
attachments: [
{
filename: 'invite.ics',
content: icsBuffer,
contentType: 'text/calendar; method=REQUEST',
},
],
};
// Send the thank you email
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.error(error);
res.send('Error sending email.');
} else {
console.log('Email sent: ' + info.response);
// res.send('Thank you for contacting us! Check your email for confirmation.');
res.sendFile(path.join(__dirname, 'display.html'));
}
});
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});