-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (38 loc) · 1.25 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
43
44
45
// Create references for libraries
var express = require('express');
var http = require('http');
var firebase = require('firebase');
var dotenv = require('dotenv');
var mailgun = require('mailgun-js');
// Express server setup
var app = express();
var server = http.createServer(app);
dotenv.load();
// Authenticate with firebase
firebase.initializeApp({
serviceAccount: "firebase-credentials-pm.json",
databaseURL: "https://mutant-hours-pm.firebaseio.com"
});
var rootRef = firebase.database().ref();
// Authenticate with mailgun
var mailgunClient = mailgun({ apiKey: process.env.MAILGUN_KEY, domain: process.env.MAILGUN_DOMAIN });
//Listen for new emails being added to firebase and send email thought mailgun
var emailsRef = rootRef.child('emails');
emailsRef.on('child_added', function(snapshot){
var email = snapshot.val();
var emailData = {
from: '<postmaster@' + process.env.MAILGUN_DOMAIN + '>',
to: email.emailAddress,
subject: 'Thanks for signing up!',
text: 'Welcome to Mutant Office Hours.'
};
mailgunClient.messages().send(emailData, function(error, body){
console.log(body);
if (error) {
console.log(error);
}
});
});
server.listen(3030, function(){
console.log('Listening on http://localhost:3030');
});