-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
128 lines (110 loc) · 4.24 KB
/
server.js
File metadata and controls
128 lines (110 loc) · 4.24 KB
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
import express from 'express';
import nodemailer from 'nodemailer';
import cors from 'cors';
import dotenv from 'dotenv';
dotenv.config();
const app = express();
const PORT = process.env.PORT || 3001;
app.use(cors());
app.use(express.json());
// Email configuration using environment variables
// For development/testing, using Gmail with app-specific password
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL_USER || 'babinbid05@gmail.com',
pass: process.env.EMAIL_PASSWORD || 'your-app-specific-password'
}
});
// Subscribe endpoint
app.post('/api/subscribe', async (req, res) => {
try {
const { email, name = 'Subscriber' } = req.body;
if (!email) {
return res.status(400).json({ error: 'Email is required' });
}
// Email to admin
const adminMailOptions = {
from: process.env.EMAIL_USER || 'babinbid05@gmail.com',
to: 'babinbid05@gmail.com',
subject: 'New CargoConnect Subscription',
html: `<h2>New Subscription</h2><p><strong>${name}</strong> (${email}) has subscribed to CargoConnect</p>`
};
// Confirmation email to subscriber
const subscriberMailOptions = {
from: process.env.EMAIL_USER || 'babinbid05@gmail.com',
to: email,
subject: 'Welcome to CargoConnect Newsletter',
html: `<h2>Welcome to CargoConnect!</h2><p>Thank you for subscribing to our newsletter. You'll now receive exclusive offers, logistics tips, and service updates.</p><p>Best regards,<br>CargoConnect Team</p>`
};
// Send both emails
await transporter.sendMail(adminMailOptions);
await transporter.sendMail(subscriberMailOptions);
res.status(200).json({
success: true,
message: 'Subscription successful! Check your email for confirmation.'
});
} catch (error) {
console.error('Error sending email:', error);
res.status(500).json({
error: 'Failed to process subscription. Please try again later.'
});
}
});
// Contact form endpoint
app.post('/api/contact', async (req, res) => {
try {
const { name, email, phone, subject, message } = req.body;
if (!name || !email || !subject || !message) {
return res.status(400).json({ error: 'All fields are required' });
}
// Email to admin
const adminMailOptions = {
from: process.env.EMAIL_USER || 'babinbid05@gmail.com',
to: 'babinbid05@gmail.com',
subject: `New Contact Form Submission: ${subject}`,
html: `
<h2>New Contact Form Submission</h2>
<p><strong>Name:</strong> ${name}</p>
<p><strong>Email:</strong> ${email}</p>
<p><strong>Phone:</strong> ${phone || 'Not provided'}</p>
<p><strong>Subject:</strong> ${subject}</p>
<p><strong>Message:</strong></p>
<p>${message.replace(/\n/g, '<br>')}</p>
`
};
// Confirmation email to user
const userMailOptions = {
from: process.env.EMAIL_USER || 'babinbid05@gmail.com',
to: email,
subject: 'We received your message - CargoConnect',
html: `
<h2>Thank You for Contacting Us</h2>
<p>Hi ${name},</p>
<p>We've received your message and will get back to you within 2 hours.</p>
<p><strong>Your Message:</strong></p>
<p>${message.replace(/\n/g, '<br>')}</p>
<p>Best regards,<br>CargoConnect Team</p>
`
};
// Send both emails
await transporter.sendMail(adminMailOptions);
await transporter.sendMail(userMailOptions);
res.status(200).json({
success: true,
message: 'Message sent successfully! We will contact you soon.'
});
} catch (error) {
console.error('Error sending email:', error);
res.status(500).json({
error: 'Failed to send message. Please try again later.'
});
}
});
// Health check endpoint
app.get('/api/health', (req, res) => {
res.status(200).json({ status: 'Server is running' });
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});