-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
63 lines (54 loc) · 1.79 KB
/
index.js
File metadata and controls
63 lines (54 loc) · 1.79 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
import express from "express";
import dotenv from "dotenv";
import nodemailer from "nodemailer";
dotenv.config();
const app = express();
app.use(express.json()); // To parse JSON request bodies
// Create transporter for Brevo
const transporter = nodemailer.createTransport({
host: "smtp-relay.brevo.com",
port: 587,
secure: false,
auth: {
user: process.env.SMTP_USER, // Brevo SMTP login
pass: process.env.SMTP_PASS, // Brevo SMTP password
},
});
// Function to send email
const sendEmail = async ({ to, subject, html }) => {
try {
if (!to || !subject || !html) {
throw new Error("Missing required email parameters");
}
const response = await transporter.sendMail({
from: `"Authify Support" <${process.env.SMTP_USER}>`, // Sender email
to,
subject,
html,
});
console.log(`Email sent successfully to ${to}:`, response);
return response;
} catch (error) {
console.error(`Error sending email: ${error.message}`, { error });
throw error;
}
};
// API endpoint for sending email
app.post("/send-email", async (req, res) => {
const { email, subject, html, apiKey } = req.body;
// Validate API Key
if (apiKey !== process.env.API_KEY) {
return res.status(401).json({ success: false, message: "Unauthorized" });
}
try {
const response = await sendEmail({ to: email, subject, html });
res.status(200).json({ success: true, message: "Email sent successfully", response });
} catch (error) {
res.status(500).json({ success: false, message: error.message });
}
});
// Start the server
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`App is listening on port ${port}`);
});