-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
92 lines (80 loc) · 3.09 KB
/
index.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
const express = require('express');
const cors = require('cors');
const mongoose = require('./config/mongoose');
const nodemailer = require('nodemailer'); // Import nodemailer
const User = require('./models/model');
const app = express();
app.use(cors());
app.use(express.json());
// Nodemailer setup
const transporter = nodemailer.createTransport({
service: 'gmail', // Use any email service provider (e.g., Gmail)
auth: {
user: process.env.EMAIL_USER, // Add your email
pass: process.env.EMAIL_PASS // Add your email password
}
});
// Route to get all subscribers
app.get('/subscribers', async (req, res) => {
try {
const users = await User.find({});
if (users.length === 0) {
return res.status(404).json({ message: 'No subscribers found' });
}
res.status(200).json(users);
} catch (error) {
console.error('Error retrieving users:', error);
res.status(500).json({ message: 'Error retrieving users', error });
}
});
// Route to send email
// Route to send email
app.post('/send-email', async (req, res) => {
const { recipients, subject, body } = req.body; // Updated to accept an array of recipients
try {
// Ensure recipients is an array
if (!Array.isArray(recipients) || recipients.length === 0) {
return res.status(400).json({ message: 'Recipients must be provided' });
}
// Send email to each recipient
const emailPromises = recipients.map(async ({ name, email }) => {
const mailOptions = {
from: process.env.EMAIL_USER, // sender address
to: email, // individual recipient email
subject,
html: `<p>Dear ${name},</p>${body}` // Personalized greeting
};
return transporter.sendMail(mailOptions);
});
await Promise.all(emailPromises); // Wait for all emails to be sent
res.status(200).json({ message: 'Emails sent successfully!' });
} catch (error) {
console.error('Error sending email:', error);
res.status(500).json({ message: 'Error sending email', error });
}
});
app.post('/subscribe', async (req, res) => {
const { name, email } = req.body;
if (!name || !email) {
return res.status(400).json({ message: 'Name and email are required' });
}
try {
// Check if the email already exists
const existingUser = await User.findOne({ email });
if (existingUser) {
console.log('Same email used');
return res.status(400).json({ message: 'Email already subscribed' });
}
// Create new subscriber
const newUser = new User({ name, email });
await newUser.save();
res.status(201).json({ message: 'User subscribed successfully', user: newUser });
} catch (error) {
console.error('Error subscribing user:', error);
res.status(500).json({ message: 'Error subscribing user', error });
}
});
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});