Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const path = require('path');

const database = require('./config/key').MongoURI;
var indexRoute = require('./routes/index');
var emailRoute = require('./routes/email');

// Set up the app with express
const app = express();
Expand Down Expand Up @@ -55,6 +56,7 @@ db.on('error', console.error.bind(console, 'Mongodb connection error:'));

// API ROUTES
app.use('/', indexRoute);
app.use('/email', emailRoute);

// Express will serve up 404 file if a route is not recognized
app.get('*', (req, res) => {
Expand All @@ -72,4 +74,4 @@ app.use(function(err, req, res, next) {
res.render('error');
});

module.exports = app;
module.exports = app;
41 changes: 41 additions & 0 deletions config/emailFunction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const path = require("path");
const fs = require("fs");

var api_key = 'd79d63126502f0d15e98e0865652ba10-7caa9475-388729451';
var domain = 'sandbox787f8f71d66e4b36ae02fef66edd2005.mialgun.org';
var mailgun = require('mailgun-js')({ apiKey: api_key, domain: domain });


var sendEmail = function sendEmail(res, receiver) {

const filename = path.normalize(path.join(__dirname, "../email-templates/email.hbs"));
const emailTemplate = fs
.readFileSync(filename)
.toString()
.replace(/{{email}}/, receiver);
const data = {
from: "FeeTracker <noreply@feetracker.com>",
to: receiver,
subject: "FeeTracker Subscription",
html: emailTemplate.replace(/{{email}}/, receiver)
};

mailgun.messages().send(data, (error, body) => {
if (error) {
console.log('Email not sent')

return res.status(400).json({
status: 'error',
message: 'email not sent',
});
} else {
console.log('Email sent');
return res.status(200).json({
status: 'success',
message: 'email sent',
});

}
})
}
module.exports = sendEmail;
20 changes: 20 additions & 0 deletions controllers/email.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var cron = require('node-cron');
var sendEmailFunction = require('../config/emailFunction');

const emailDb = require('../models/promise').EmailDb;

const Email = {
async sendEmail(req, res, next) {
try {
var task = cron.schedule('* * * * Monday', () => {
var receiver = req.body.email;

sendEmailFunction(res, receiver)
console.log('STARTED task');
})
} catch (err) {
return res.status(400).send(error);
}
},
};
module.exports = Email;
213 changes: 213 additions & 0 deletions email-templates/email.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">

<head>
<title>DevAlert</title>
<!--[if !mso]><!-- -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!--<![endif]-->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<style type="text/css">
#outlook a {
padding: 0;
}

.ReadMsgBody {
width: 100%;
}

.ExternalClass {
width: 100%;
}

.ExternalClass * {
line-height: 100%;
}

body {
margin: 0;
padding: 0;
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
}

table,
td {
border-collapse: collapse;
mso-table-lspace: 0pt;
mso-table-rspace: 0pt;
}

img {
border: 0;
height: auto;
line-height: 100%;
outline: none;
text-decoration: none;
-ms-interpolation-mode: bicubic;
}

p {
display: block;
margin: 13px 0;
}
</style>
<!--[if !mso]><!-->
<style type="text/css">
@media only screen and (max-width:480px) {
@-ms-viewport {
width: 320px;
}
@viewport {
width: 320px;
}
}
</style>
<!--<![endif]-->
<!--[if mso]>
<xml>
<o:OfficeDocumentSettings>
<o:AllowPNG/>
<o:PixelsPerInch>96</o:PixelsPerInch>
</o:OfficeDocumentSettings>
</xml>
<![endif]-->
<!--[if lte mso 11]>
<style type="text/css">
.outlook-group-fix { width:100% !important; }
</style>
<![endif]-->
<!--[if !mso]><!-->
<link href="https://fonts.googleapis.com/css?family=Roboto:300,500" rel="stylesheet" type="text/css">
<style type="text/css">
@import url(https://fonts.googleapis.com/css?family=Roboto:300,500);
</style>
<!--<![endif]-->
<style type="text/css">
@media only screen and (min-width:480px) {
.mj-column-per-100 {
width: 100% !important;
max-width: 100%;
}
.mj-column-per-45 {
width: 45% !important;
max-width: 45%;
}
}
</style>
<style type="text/css">
@media only screen and (max-width:480px) {
table.full-width-mobile {
width: 100% !important;
}
td.full-width-mobile {
width: auto !important;
}
}
</style>
</head>

<body>
<div>
<!--[if mso | IE]><table align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:600px;" width="600" ><tr><td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;"><![endif]-->
<div style="Margin:0px auto;max-width:600px;">
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="width:100%;">
<tbody>
<tr>
<td style="direction:ltr;font-size:0px;padding:0px;text-align:center;vertical-align:top;">
<!--[if mso | IE]><table role="presentation" border="0" cellpadding="0" cellspacing="0"><tr><td class="" style="vertical-align:top;width:600px;" ><![endif]-->
<div class="mj-column-per-100 outlook-group-fix" style="font-size:13px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;">
<table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%">
<tr>
<td align="center" style="font-size:0px;padding:10px 25px;word-break:break-word;">
<table border="0" cellpadding="0" cellspacing="0" role="presentation" style="border-collapse:collapse;border-spacing:0px;">
<tbody>
<tr>
<td style="width:550px;">
<a href="https://feetracker.herokuapp.com" target="_blank">
<img height="auto" src="#" style="border:0;display:block;outline:none;text-decoration:none;height:auto;width:100%;"
width="550">
</a>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
</div>
<!--[if mso | IE]></td></tr></table><![endif]-->
</td>
</tr>
</tbody>
</table>
</div>
<!--[if mso | IE]></td></tr></table><table align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:600px;" width="600" ><tr><td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;"><![endif]-->
<div style="Margin:0px auto;max-width:600px;">
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="width:100%;">
<tbody>
<tr>
<td style="direction:ltr;font-size:0px;padding:0px;text-align:center;vertical-align:top;">
<!--[if mso | IE]><table role="presentation" border="0" cellpadding="0" cellspacing="0"><tr><td class="" style="vertical-align:top;width:270px;" ><![endif]-->
<div class="mj-column-per-45 outlook-group-fix" style="font-size:13px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;">
<table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%">
<tr>
<td align="center" style="font-size:0px;padding:0px;word-break:break-word;">
<div style="font-family:Roboto, Helvetica, sans-serif;font-size:18px;font-weight:500;line-height:24px;text-align:center;color:#616161;">Fee Tracker</div>
</td>
</tr>
<tr>
<td style="font-size:0px;padding:10px 25px;word-break:break-word;">
<p style="border-top:solid 2px #616161;font-size:1;margin:0px auto;width:100%;"></p>
<!--[if mso | IE]><table align="center" border="0" cellpadding="0" cellspacing="0" style="border-top:solid 2px #616161;font-size:1;margin:0px auto;width:220px;" role="presentation" width="220px" ><tr><td style="height:0;line-height:0;"> &nbsp;
</td></tr></table><![endif]-->
</td>
</tr>
</table>
</div>
<!--[if mso | IE]></td></tr></table><![endif]-->
</td>
</tr>
</tbody>
</table>
</div>
<!--[if mso | IE]></td></tr></table><table align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:600px;" width="600" ><tr><td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;"><![endif]-->
<div style="Margin:0px auto;max-width:600px;">
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="width:100%;">
<tbody>
<tr>
<td style="direction:ltr;font-size:0px;padding:0px;padding-top:10px;text-align:center;vertical-align:top;">
<!--[if mso | IE]><table role="presentation" border="0" cellpadding="0" cellspacing="0"><tr><td class="" style="vertical-align:top;width:600px;" ><![endif]-->
<div class="mj-column-per-100 outlook-group-fix" style="font-size:13px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;">
<table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%">
<tr>
<td align="left" style="font-size:0px;padding:10px 25px;word-break:break-word;">
<div style="font-family:Roboto, Helvetica, sans-serif;font-size:16px;font-weight:300;line-height:24px;text-align:left;color:#616161;">
<p>Hello!</p>
<p>You subscribed to the Fee Tracker weekly newsletter. You would be getting mails from us.</p>

<p>Cheers,</p>
<h5>Fee Tracker Team</h5>

<a href="https://feetracker.herokuapp.com/unsubscribe/{{email}}" style="margin-top:20px; color:#1114cc; font-size:12px;">
unsubscribe from newsletter</a>

</div>
</td>

</tr>
</table>
</div>
<!--[if mso | IE]></td></tr></table><![endif]-->
</td>
</tr>
</tbody>
</table>
</div>

<!--[if mso | IE]></td></tr></table><![endif]-->
</div>
</body>

</html>
12 changes: 12 additions & 0 deletions models/email.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const mongoose = require('mongoose');

const emailSchema = new mongoose.Schema({
email: {
type: String,
required: true,
},
});

const EmailSchema = mongoose.model('EmailSchema', emailSchema);

module.exports = { EmailSchema };
20 changes: 20 additions & 0 deletions models/promise.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// import models
const Sample = require('./sample');
const Email = require('./email');

class Example {
/**
Expand Down Expand Up @@ -61,8 +62,27 @@ class Example {
});
});
}
};

class EmailDb {
/**
* @param {string} text
* @returns {object} Return all
*/
static findAll(param) {
return new Promise((resolve, reject) => {
Email.findAll(param)
.then(res => {
resolve(res);
})
.catch(err => {
reject(err);
});
});
}
}

module.exports = {
Example,
EmailDb
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
"hbs": "^4.0.4",
"http-errors": "^1.7.2",
"lodash": "^4.17.11",
"mailgun-js": "^0.22.0",
"mongoose": "^5.5.2",
"morgan": "^1.9.1",
"node-cron": "^2.0.3",
"validator": "^10.11.0"
}
}
7 changes: 7 additions & 0 deletions routes/email.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var express = require('express');
var Email = require('../controllers/email');
var router = express.Router();

router.post('/', Email.sendEmail);

module.exports = router;