-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
123 lines (96 loc) · 3.02 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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const express = require('express');
const app = express();
const nodemailer = require('nodemailer');
const akun = require('./env.js')
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
const port = process.env.PORT || 5000;
//server mulai
app.get("/", (req, res) => {
res.status(200).send("Server ready, siap digunakan!");
})
//email file....
app.post("/api/mail", function (request, response) {
const transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
auth: {
user: akun.email,
pass: akun.password
}
});
const admin = {
to: akun.email, // list penerima (dapat multipel, dipisahkan koma)
subject: `Pengiriman berhasil`, // Subject
text: `${request.body.to} ${request.body.sub} ${request.body.text}` //text
};
const mail = {
//from: `${request.body.from}`, // email pengirim
to: `${request.body.to}`, // list penerima (dapat multipel, dipisahkan koma)
subject: `${request.body.sub}`, // Subject
text: `${request.body.text}` //text
// attachments: [{ // menyisipkan file
// filename: 'test.docx',
// content: './storage/test.docx'
// }]
};
// send mail with defined transport object
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
(async () => {
if (`${request.headers.auth}` == akun.auth) {
console.log(`Authorisasi berhasil`)
//kirim ke admin
transporter.sendMail(admin, function (err, info) {
if (err) {
console.log(err);
response.json({
message: "message not sent: an error occured; check the server's console log"
});
} else {
console.log("Pengiriman ke admin berhasil")
console.log("Pengiriman ke klien pending selama 6 jam")
response.json({
message: `Email telah sukses terkirim!`
});
}
});
await delay(360000);
//kirim ke klien
transporter.sendMail(mail, function (err, info) {
if (err) {
console.log(err);
response.json({
message: "message not sent: an error occured; check the http request"
});
} else {
console.log("Pengiriman ke klien berhasil")
response.json({
message: `Email telah sukses terkirim!`
});
}
});
} else {
response.json({
message: `Anda tidak terautentikasi`
});
}
})();
});
//express....
app.listen(port, () => {
console.log(`Server berjalan pada ${port}.`);
})
// catch 404, meneruskan ke error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// render error page
res.status(err.status || 500);
res.json({
message: err.message,
error: req.app.get('env') === 'development' ? err : {}
});
});
module.exports = app;