-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
84 lines (71 loc) · 2.06 KB
/
server.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
const nodemailer = require("nodemailer");
const fs = require("fs");
const path = require("path");
const express = require("express");
const bodyParser = require("body-parser");
require("dotenv").config();
const htmlPath = path.join(__dirname, "index.html");
// sending from index.html
fs.readFile(htmlPath, "utf8", (err, data) => {
if (err) {
console.error("Error reading HTML file:", err);
return;
}
let tp = nodemailer.createTransport({
// host: "smtp.gmail.com",
// port: 587,
// secure: false,
service: "gmail",
auth: {
user: "[email protected]",
pass: process.env.pass,
},
});
let mailOptions = {
from: "'Bes'<[email protected]>",
to: ["[email protected]", "[email protected]"],
subject: "HTML Email from index.html",
html: data,
};
tp.sendMail(mailOptions, (error, info) => {
if (error) {
console.log("Error occurred: " + error.message);
return;
}
console.log("Email sent successfully!");
console.log("Message ID: " + info.messageId);
});
});
// sending from a server
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.post("/send-mail", (req, res) => {
const { subject, message } = req.body;
let tp = nodemailer.createTransport({
service: "gmail",
auth: {
user: "[email protected]",
pass: process.env.pass,
},
});
let mailOptions = {
from: "'Bes'<[email protected]>",
to: ["[email protected]", "[email protected]"],
subject: subject,
html: message,
};
tp.sendMail(mailOptions, (error, info) => {
if (error) {
console.log("Error occurred: " + error.message);
res.status(500).json({ message: "An error occurred" });
} else {
console.log("Email sent successfully!");
console.log("Message ID: " + info.messageId);
res.status(200).json({ message: "Email sent successfully" });
}
})
});
app.listen(3000, () => {
console.log("Server started at port:3000")
})