-
Notifications
You must be signed in to change notification settings - Fork 6
/
mail.js
39 lines (37 loc) · 1.13 KB
/
mail.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
const sgMail = require('@sendgrid/mail')
sgMail.setApiKey(process.env.SENDGRID_API_KEY)
const Notify = {
sensor: (email, value) => {
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const percent = Math.round(value * 100 / 1024)
const friendlyHumidity = `${Math.round(percent)}% of humidity`;
let stateValue = {
color: "#b5c041"
}
if(percent < 40) {
stateValue.color = "#a6002c"
}
if(percent >= 40 && percent < 80) {
stateValue.color = "#b5c041"
}
if(percent >= 80 && percent <= 100) {
stateValue.color = "#2b4c09"
}
const msg = {
to: email,
from: '[email protected]',
templateId: 'd-4314a62668dc407b82a05e7fd1906e47',
dynamic_template_data: {
state: friendlyHumidity,
color: "white",
bgcolor: stateValue.color,
absolute: value
},
subject: 'We have a new status from your plant'
//text: `The humidity value is ${value}`,
//html:`The humidity value is ${value}`
}
return sgMail.send(msg);
}
}
module.exports = Notify