-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBulk_Email.js
More file actions
56 lines (46 loc) · 1.32 KB
/
Copy pathBulk_Email.js
File metadata and controls
56 lines (46 loc) · 1.32 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
const express = require('express');
const nodemailer = require('nodemailer');
const mongoose = require('mongoose');
require('dotenv').config();
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });
const emailSchema = new mongoose.Schema({
subject: String,
body: String,
recipients: [String]
});
const Email = mongoose.model('Email', emailSchema);
const app = express();
app.use(express.json());
const transporter = nodemailer.createTransport({
service: 'SendGrid',
auth: {
user: 'apikey',
pass: process.env.SENDGRID_API_KEY
}
});
app.post('/send', async (req, res) => {
const { subject, body, recipients } = req.body;
try {
const email = new Email({ subject, body, recipients });
await email.save();
const mailOptions = {
from: 'your-email@example.com',
to: recipients,
subject: subject,
text: body,
html: `<p>${body}</p>`
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return res.status(500).send(error.toString());
}
res.status(200).send('Emails sent: ' + info.response);
});
} catch (error) {
res.status(500).send(error.toString());
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});