-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.js
More file actions
180 lines (173 loc) · 6.01 KB
/
helpers.js
File metadata and controls
180 lines (173 loc) · 6.01 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const mailgunJS = require('mailgun-js')
const nodemailer = require("nodemailer")
module.exports = {
validateAndFetchEnvVariables,
getEmailHeader,
handleFormFields,
parseFormData,
sendMail
}
function checkAllowedDomains(config) {
if(process.env.ALLOWED_DOMAINS
&& process.env.ALLOWED_DOMAINS.length > 0) {
const allowedDomains = process.env.ALLOWED_DOMAINS.replace(/ /g,"").split(",")
if(allowedDomains.length > 0) {
config.allowedDomains = allowedDomains
}
else {
config.valid = false
config.reason = `ALLOW_ALL_DOMAINS doesn't exist or set to 'no' and ALLOWED_DOMAINS doesn't exist or is invalid.`
}
}
else {
config.valid = false
config.reason = `ALLOW_ALL_DOMAINS doesn't exist or set to 'no' and ALLOWED_DOMAINS doesn't exist or is invalid.`
}
return config
}
function validateAndFetchEnvVariables(config) {
config = {
valid: true
}
if(process.env.ALLOW_ALL_DOMAINS
&& process.env.ALLOW_ALL_DOMAINS.length > 0) {
if(process.env.ALLOW_ALL_DOMAINS == "yes") {
config.allowAllDomains = true
}
else if(process.env.ALLOW_ALL_DOMAINS == "no") {
config.allowAllDomains = false
config = checkAllowedDomains(config)
}
else {
config.valid = false
config.reason = `ALLOW_ALL_DOMAINS must be set to "yes" or "no".`
}
}
else {
config.allowAllDomains = false
config = checkAllowedDomains(config)
}
if(config.valid) {
if(process.env.MAILGUN_API_KEY
&& process.env.MAILGUN_API_KEY.length > 0
&& process.env.MAILGUN_FROM_EMAIL
&& process.env.MAILGUN_FROM_EMAIL.length > 0
&& process.env.MAILGUN_DOMAIN
&& process.env.MAILGUN_DOMAIN.length > 0) {
config.method = 1
config.mailgunAPIKey = process.env.MAILGUN_API_KEY
config.mailgunFromEmail = process.env.MAILGUN_FROM_EMAIL
config.mailgunDomain = process.env.MAILGUN_DOMAIN
}
else if(process.env.SMTP_SERVER && process.env.SMTP_SERVER.length > 0
&& process.env.SMTP_EMAIL && process.env.SMTP_EMAIL.length > 0
&& process.env.SMTP_USER && process.env.SMTP_USER.length > 0
&& process.env.SMTP_PASSWORD && process.env.SMTP_PASSWORD.length > 0
&& process.env.SMTP_PORT && process.env.SMTP_PORT.length > 0) {
config.method = 2
config.smtpServer = process.env.SMTP_SERVER
config.smtpEmail = process.env.SMTP_EMAIL
config.smtpUser = process.env.SMTP_USER
config.smtpPassword = process.env.SMTP_PASSWORD
config.smtpPort = parseInt(process.env.SMTP_PORT)
}
else {
config.valid = false
config.reason = `No valid email system configuration.`
}
}
return config
}
function getEmailHeader(formName, postURL) {
let html = `
<h2>New Form Submission ${(formName ? " for " + formName : (postURL ? " for " + postURL : ""))}</h2>
`
if(formName && postURL) {
html += `
<b>Form URL:</b> ${postURL}<br>
`
}
html += `
<b>Timestamp:</b> ${new Date().toLocaleString()} (UTC)<br><br>
`
return html
}
function handleFormFields(postData) {
let html = `
<table style="width: 100%; padding: 1opx; border: 1px solid #ddd; text-align: left; border-collapse: collapse;">
<tr>
<th style="padding: 10px; border: 1px solid #ddd; text-align: left;">Field</th>
<th style="padding: 10px; border: 1px solid #ddd; text-align: left;">Value</th>
</tr>
`
Object.keys(postData)
.filter(key => key[0] != "_")
.forEach(key => {
html += `
<tr>
<td style="padding: 10px; border: 1px solid #ddd; text-align: left;">${key}</td>
<td style="padding: 10px; border: 1px solid #ddd; text-align: left;">${postData[key]}</td>
</tr>
`
})
html += `
</table>
`
return html
}
function parseFormData(data) {
const postData = {}
for(const item of data.split("&")) {
const itemParts = item.split("=")
postData[itemParts[0]] = decodeURIComponent(itemParts[1].replace(/\+/g, '%20'))
}
console.log(`POST Data - ${postData}`)
return postData
}
function sendMail(toEmail, formName, replyEmail, messageHTML, config, callback) {
const subject = `A New Form Submission Has Arrived ${(formName ? " - " + formName : "")} (${new Date().toLocaleString()})`
if(config.method == 1) {
const mailgun = new mailgunJS({
apiKey: config.mailgunAPIKey,
domain: config.mailgunDomain
})
const emailConfig = {
to: toEmail,
from: config.mailgunFromEmail,
subject: subject,
html: messageHTML,
}
if(replyEmail) {
emailConfig["h:Reply-To"] = replyEmail
}
mailgun.messages().send(emailConfig, (err, body) => {
if(body) {
console.log(`Data returned from mailgun.messages().send - ${body}`)
}
return(callback(err))
})
}
else if(config.method == 2) {
const transporter = nodemailer.createTransport({
host: config.smtpServer,
port: config.smtpPort,
secure: true,
auth: {
user: config.smtpUser,
pass: config.smtpPassword
}
})
const emailConfig = {
from: config.smtpEmail,
to: toEmail,
subject,
html: messageHTML
}
transporter.sendMail(emailConfig, (err, info) => {
if(info) {
console.log(`Data returned from SMTP call - ${info}`)
}
return(callback(err))
})
}
}