-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathemail_votesubmit.js
42 lines (33 loc) · 1.3 KB
/
email_votesubmit.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
const PORT = process.env.PORT || 8080;
const ENV = process.env.ENV || 'development';
const knexConfig = require('./knexfile');
const knex = require('knex')(knexConfig[ENV]);
const api_key = process.env.MAILGUN_KEY;
const domain = process.env.DOMAIN;
const mailgun = require('mailgun-js')({ apiKey: api_key, domain: domain });
// sends notification to admin when new response is received
module.exports = function voteSubmitEmail (voteURL, voterName) {
knex('polls')
.select('admin_email', 'admin_url', 'title')
.where('vote_url', voteURL)
.then((rows) => {
const adminEmail = rows[0].admin_email;
const pollTitle = rows[0].title;
const adminURL = rows[0].admin_url;
const data = {
from: `decisionmaker@${domain}`,
to: adminEmail,
subject: `${voterName} just voted on your poll`,
text:
`Hi!
${voterName} has just voted on your poll: ${pollTitle}. Click below to see the updated results.
Admin Link: http://localhost:8080/polls/admin/${adminURL}
Share the link below to get more responses.
Voting Link: http://localhost:8080/polls/${voteURL}
Happy Polling!`,
};
mailgun.messages().send(data, (error, body) => {
if (error) console.error(error);
});
});
};