|
| 1 | + |
| 2 | +const express = require('express'); |
| 3 | +const app = express(); |
| 4 | +const nodemailer = require('nodemailer'); |
| 5 | +const akun = require('./env.js') |
| 6 | + |
| 7 | +app.use(express.json()); |
| 8 | +app.use(express.urlencoded({ extended: false })); |
| 9 | +const port = process.env.PORT || 5000; |
| 10 | + |
| 11 | +//server mulai |
| 12 | +app.get("/", (req, res) => { |
| 13 | + |
| 14 | + res.status(200).send("Server ready, siap digunakan!"); |
| 15 | + |
| 16 | +}) |
| 17 | + |
| 18 | +//email file.... |
| 19 | + |
| 20 | +app.post("/api/mail", function (request, response) { |
| 21 | + const transporter = nodemailer.createTransport({ |
| 22 | + host: 'smtp.gmail.com', |
| 23 | + port: 587, |
| 24 | + auth: { |
| 25 | + user: akun.email, |
| 26 | + pass: akun.password |
| 27 | + } |
| 28 | + }); |
| 29 | + |
| 30 | + const admin = { |
| 31 | + to: akun.email, // list penerima (dapat multipel, dipisahkan koma) |
| 32 | + subject: `Pengiriman berhasil`, // Subject |
| 33 | + text: `${request.body.to} ${request.body.sub} ${request.body.text}` //text |
| 34 | + }; |
| 35 | + |
| 36 | + const mail = { |
| 37 | + //from: `${request.body.from}`, // email pengirim |
| 38 | + to: `${request.body.to}`, // list penerima (dapat multipel, dipisahkan koma) |
| 39 | + subject: `${request.body.sub}`, // Subject |
| 40 | + text: `${request.body.text}` //text |
| 41 | + // attachments: [{ // menyisipkan file |
| 42 | + // filename: 'test.docx', |
| 43 | + // content: './storage/test.docx' |
| 44 | + // }] |
| 45 | + }; |
| 46 | + |
| 47 | + // send mail with defined transport object |
| 48 | + const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); |
| 49 | + |
| 50 | + (async () => { |
| 51 | + |
| 52 | + if (`${request.headers.auth}` == akun.auth) { |
| 53 | + |
| 54 | + console.log(`Authorisasi berhasil`) |
| 55 | + |
| 56 | + //kirim ke admin |
| 57 | + transporter.sendMail(admin, function (err, info) { |
| 58 | + if (err) { |
| 59 | + console.log(err); |
| 60 | + response.json({ |
| 61 | + message: "message not sent: an error occured; check the server's console log" |
| 62 | + }); |
| 63 | + } else { |
| 64 | + console.log("Pengiriman ke admin berhasil") |
| 65 | + console.log("Pengiriman ke klien pending selama 6 jam") |
| 66 | + response.json({ |
| 67 | + message: `Email telah sukses terkirim!` |
| 68 | + }); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + await delay(360000); |
| 73 | + |
| 74 | + //kirim ke klien |
| 75 | + transporter.sendMail(mail, function (err, info) { |
| 76 | + if (err) { |
| 77 | + console.log(err); |
| 78 | + response.json({ |
| 79 | + message: "message not sent: an error occured; check the http request" |
| 80 | + }); |
| 81 | + } else { |
| 82 | + console.log("Pengiriman ke klien berhasil") |
| 83 | + response.json({ |
| 84 | + message: `Email telah sukses terkirim!` |
| 85 | + }); |
| 86 | + } |
| 87 | + }); |
| 88 | + } else { |
| 89 | + response.json({ |
| 90 | + message: `Anda tidak terautentikasi` |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + })(); |
| 95 | +}); |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +//express.... |
| 100 | +app.listen(port, () => { |
| 101 | + |
| 102 | + console.log(`Server berjalan pada ${port}.`); |
| 103 | + |
| 104 | +}) |
| 105 | + |
| 106 | +// catch 404, meneruskan ke error handler |
| 107 | +app.use(function(req, res, next) { |
| 108 | + next(createError(404)); |
| 109 | +}); |
| 110 | + |
| 111 | +// error handler |
| 112 | +app.use(function(err, req, res, next) { |
| 113 | + |
| 114 | + // render error page |
| 115 | + res.status(err.status || 500); |
| 116 | + res.json({ |
| 117 | + message: err.message, |
| 118 | + error: req.app.get('env') === 'development' ? err : {} |
| 119 | + |
| 120 | + }); |
| 121 | +}); |
| 122 | + |
| 123 | +module.exports = app; |
0 commit comments