-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
123 lines (106 loc) · 4.33 KB
/
index.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
// This example uses Express to receive webhooks
// https://stackoverflow.com/questions/27599614/var-express-requireexpress-var-app-express-what-is-express-is-it
const express = require('express')
const app = express()
// Instantiating formsg-sdk without parameters default to using the package's
// production public signing key.
const formsg = require('@opengovsg/formsg-sdk')()
// This is where your domain is hosted, and should match
// the URI supplied to FormSG in the form dashboard
const POST_URI = 'https://qx5aa8.deta.dev/submissions'
// Your form's secret key downloaded from FormSG upon form creation
const formSecretKey = process.env.FORM_SECRET_KEY
// Set to true if you need to download and decrypt attachments from submissions
const HAS_ATTACHMENTS = false
app.post(
'/submissions',
// Endpoint authentication by verifying signatures
function (req, res, next) {
try {
formsg.webhooks.authenticate(req.get('X-Formsg-Signature'), POST_URI)
// Continue processing the POST body
return next()
} catch (e) {
return next()
// return res.status(401).send({ message: 'Unauthorized' })
}
},
// Parse JSON from raw request body
express.json(),
// Decrypt the submission
async function (req, res, next) {
// If `verifiedContent` is provided in `req.body.data`, the return object
// will include a verified key.
const submission = HAS_ATTACHMENTS
? await formsg.crypto.decryptWithAttachments(formSecretKey, req.body.data)
: formsg.crypto.decrypt(formSecretKey, req.body.data)
// If the decryption failed, submission will be `null`.
if (submission) {
const nodemailer = require('nodemailer');
try {
console.log(require.resolve("nodemailer"));
} catch (e) {
console.error("nodemailer is not found");
process.exit(e.code);
}
console.log('Nodemailer is running')
const msg = {
from: "[email protected]",
to: "[email protected]",
subject: "Nodemailer Testing",
text: "Testing" + JSON.stringify(submission)
}
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: "[email protected]",
pass: "lhlkykbactdagjme"
},
port: 587,
host: 'smtp.gmail.com',
// secure: true
})
.sendMail(msg, (err) => {
if (err) {
return console.log('Error occurs', err);
} else {
return console.log('Email sent!');
}
})
console.log('This is submission' + JSON.stringify(submission))
return res.status(200).send({ message: 'See console for submission!' })
// Continue processing the submission
} else {
// console.log('This is submission' + JSON.stringify(submission))
return res.status(200).send({ message: 'Could not decrypt the submission' })
// Could not decrypt the submission
}
}
)
module.exports = app;
// app.get('/', async (req, res) => {
// res.send('Hello World'),
// const nodemailer = require('nodemailer');
// try {
// console.log(require.resolve("nodemailer"));
// } catch (e) {
// console.error("nodemailer is not found");
// process.exit(e.code);
// }
// console.log('Nodemailer is running')
// const msg = {
// from: "[email protected]",
// to: "[email protected]",
// subject: "Nodemailer Testing",
// text: "Testing" + JSON.stringify(submission)
// }
// const transporter = nodemailer.createTransport({
// service: "gmail",
// auth: {
// user: "[email protected]",
// pass: "lhlkykbactdagjme"
// },
// port: 587,
// host: 'smtp.gmail.com',
// // secure: true
// });