-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
206 lines (191 loc) · 6.37 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* MIT License
*
* Copyright (c) 2018 Adab1ts
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
'use strict';
const cors = require('cors');
const nodemailer = require('nodemailer');
const bunyan = require('bunyan');
const config = require('./config.json');
/**
* Check Google Cloud Functions status
*
* Trigger this function by making a GET request to:
* https://[YOUR_REGION].[YOUR_PROJECT_ID].cloudfunctions.net/ping
*
* @example
* curl -X GET "https://us-central1.your-project-id.cloudfunctions.net/ping"
*
* @param {object} request Cloud Function request context.
* @param {object} response Cloud Function response context.
*/
exports.ping = (request, response) => {
// Everything is ok
response.status(200).send('Pong!');
};
/**
* Returns a nodemailer transporter.
*
* @returns {object} Transporter object.
*/
function getTransporter() {
const logger = bunyan.createLogger({ name: 'nodemailer' });
logger.level('trace');
return nodemailer.createTransport({
service: config.transport.service,
auth: config.transport.auth,
logger,
debug: true
});
}
/**
* Check your email provider status
*
* Trigger this function by making a GET request to:
* https://[YOUR_REGION].[YOUR_PROJECT_ID].cloudfunctions.net/check
*
* @example
* curl -X GET "https://us-central1.your-project-id.cloudfunctions.net/check"
*
* @param {object} request Cloud Function request context.
* @param {object} response Cloud Function response context.
*/
exports.check = (request, response) => {
return Promise.resolve()
.then(_ => {
const transporter = getTransporter();
return transporter.verify();
})
.then(_ => {
response.status(200).send('Success: Server ready to take our messages');
})
.catch(err => {
console.log(`[Error] { code: ${err.code}, message: ${err.message} }`);
response.status(400).send('Error: Server is not ready to accept messages');
});
}
/**
* Gets the email body data from the HTTP request body.
*
* @param {object} requestBody The request payload.
* @param {string} requestBody.botTrap Field to prevent spam.
* @param {string} requestBody.name Name of the sender.
* @param {string} requestBody.email Email address of the sender.
* @param {string} requestBody.message Body of the email message.
* @returns {object} Payload object.
*/
function getPayload(requestBody) {
if (requestBody.botTrap) {
const error = new Error('Spam not allowed!');
error.code = 400;
throw error;
} else if (!requestBody.name) {
const error = new Error('Name not provided. Make sure you have a "name" property in your request');
error.code = 400;
throw error;
} else if (!requestBody.email) {
const error = new Error('Email address not provided. Make sure you have an "email" property in your request');
error.code = 400;
throw error;
} else if (!requestBody.message) {
const error = new Error('Email content not provided. Make sure you have a "message" property in your request');
error.code = 400;
throw error;
}
return {
name: requestBody.name,
email: requestBody.email,
message: requestBody.message
};
}
/**
* Ask nodemailer to send an email using your email provider.
*
* @param {object} req Cloud Function request context.
* @param {object} req.body The request payload.
* @param {object} res Cloud Function response context.
*/
function fcontact(req, res) {
return Promise.resolve()
.then(_ => {
const payload = getPayload(req.body);
const message = {
// email address of the sender
from: config.envelope.sender,
// comma separated list or an array of recipients email addresses
to: config.envelope.recipient,
// subject of the email
subject: config.envelope.subject,
// email address that will appear on the Reply-To: field
replyTo: payload.email,
// plaintext body
text: `
Formulario de contacto:
Nombre: ${payload.name}
Email: ${payload.email}
${payload.message}
`,
// HTML body
html: `
<h3>Formulario de contacto</h3>
<p>
<b>Nombre</b>: ${payload.name}<br>
<b>Email</b>: ${payload.email}
</p>
<p>${payload.message}</p>
`,
};
return message;
})
.then(message => {
const transporter = getTransporter();
return transporter.sendMail(message);
})
.then(_ => {
// console.log(req.body);
// Send number of emails sent successfully
res.status(200).send("1");
})
.catch(err => {
console.log(`[Error] { code: ${err.code}, message: ${err.message} }`);
res.status(400).send(`Error: ${err.message}`);
});
};
/**
* Send an email using your email provider.
*
* Trigger this function by making a POST request with a payload to:
* https://[YOUR_REGION].[YOUR_PROJECT_ID].cloudfunctions.net/contact
*
* @example
* curl -X POST "https://us-central1.your-project-id.cloudfunctions.net/contact" --data '{"name":"Jane Doe","email":"[email protected]","message":"Hello World!"}'
*
* @param {object} request Cloud Function request context.
* @param {object} response Cloud Function response context.
*/
exports.contact = (request, response) => {
const fcors = cors({
origin: true,
methods: ['POST', 'OPTIONS']
});
fcors(request, response, () => fcontact(request, response));
};