-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
42 lines (42 loc) · 1.04 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
'use strict';
module.exports.handler = (event, context, callback) => {
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const body = JSON.parse(event.body)
const to = body.to;
const from = body.from;
const subject = body.subject;
const html_body = body.html_body;
const text_body = body.text_body;
const msg = {
to: to,
from: from,
subject: subject,
text: text_body,
html: html_body
};
sgMail.send(msg).then(data => {
// Celebrate
const response = {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*', // Required for CORS support to work
},
body: JSON.stringify({
data: data
})
};
callback(null, response);
})
.catch(error => {
// Log friendly error
const response = {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*', // Required for CORS support to work
},
body: JSON.stringify(error.response.body)
};
callback(null, response);
});
};