-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
53 lines (45 loc) · 1.53 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
const bodyParser = require("body-parser");
require("dotenv").config(); // Load environment variables
const express = require("express");
const nodemailer = require("nodemailer");
const cors = require("cors");
const app = express();
const port = 3000;
const pass = process.env.pass; // Environment variables should be uppercase
const user = process.env.user;
const transporter = nodemailer.createTransport({
host: "mail.privateemail.com",
port: 465, // Port should be a number, not a string
secure: true, // Use SSL
auth: {
user: user,
pass: pass,
},
});
app.use(cors());
app.use(express.json());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/", function (req, res) {
res.send("Welcome to the email sending service");
});
app.post("/email", async function (req, res) { // Added async keyword here
const { text } = req.body; // Use 'text' to match your frontend data
const mailOptions = {
from: "[email protected]",
to: "[email protected]",
subject: "Nodemailer",
text: text, // Corrected to 'text'
};
try {
const info = await transporter.sendMail(mailOptions); // Await the sendMail function
console.log("Email sent: " + info.response);
res.status(200).json({ message: "Email sent successfully" });
} catch (error) {
console.error(error.message);
res.status(500).json({ message: error.message });
}
});
app.listen(port, function () {
console.log(`App listening on port ${port}`);
});