-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathazure_sendmail.js
47 lines (39 loc) · 1.43 KB
/
azure_sendmail.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
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
context.log("request: " + JSON.stringify(req));
let name = '';
let subject = '';
let email = '';
let form_message = '';
context.log("request: " + JSON.stringify(req.body));
// Parse request body
if (req.body) {
if (req.body.name)
name = req.body.name;
if (req.body.subject)
subject = req.body.subject;
if (req.body.email)
email = req.body.email;
if (req.body.message)
form_message = req.body.message;
}
// The email body for recipients
let body_text = "Someone wants to contact you!\r\n\r\n"
+ "Name: " + name + "\nEmail: " + email + "\nMessage:\r\n" + form_message
+ "\r\n\r\n--- This email was sent using Azure Functions and SendGrid ---";
console.log("message_body: " + body_text);
var message = {
"personalizations": [ { "to": [ { "email": "<your email address>" } ] } ],
from: { email: "<your email address>" },
subject: req.body.subject,
content: [{
type: 'text/plain',
value: body_text
}]
};
context.res = {
// status: 200, /* Defaults to 200 */
body: JSON.stringify({'status': "Message sent successfully"})
};
context.done(null, {message});
}