-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathaction.js
44 lines (38 loc) · 1.63 KB
/
action.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
console.log("OpenWhisk Action Fired!");
let helper = require('sendgrid').mail;
let SG_KEY = 'ADD SendGrid API Here'; //SendGrid API Key
let to_email = new helper.Email('[email protected]'); //Add your email here
function main(params) {
let subject = `OpenWhiskContact FROM: ${params["myName"]}`;
let from_email = new helper.Email(`${params["myEmail"]}`);
// Content with full information
let sysData = new Date();
let content = `Submitted at ${sysData} -------------------------------- \n\n`;
content += `Name: ${params["myName"]}\n\n`;
content += `From: ${params["myEmail"]}\n\n`;
content += `Suggested Repo: ${params["myUrl"]}\n\n`;
content += `Description: ${params["myDescription"]}\n\n`;
let mailContent = new helper.Content('text/plain', content);
let mail = new helper.Mail(from_email, subject, to_email, mailContent);
let sg = require('sendgrid')(SG_KEY);
// Processing the form, if success then send success message back to Frontend and if error then send the error message
return new Promise((resolve, reject) => {
let request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: mail.toJSON()
});
sg.API(request, function(error, response) {
if(error) {
console.log(error.response.body);
reject({error: error.message})
} else {
console.log(response.statusCode);
console.log(response.body);
console.log(response.headers);
resolve({result: 'success'});
}
});
});
}
exports.main = main;