Skip to content

Commit

Permalink
updated dependencies and fixed resulting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
thepiwo committed Mar 26, 2019
1 parent efc68dd commit db910f8
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 54 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
.idea/
5 changes: 2 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
FROM mhart/alpine-node:6
FROM node:10-alpine
RUN apk add --update git bash

COPY . /src
WORKDIR /src

RUN npm install
RUN npm install -g forever

EXPOSE 3003
CMD ["forever", "server.js"]
CMD ["node", "server.js"]
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ Installation
------------

* register Sendgrid Event-Notifications to your /webhook url
* docker build -t breakout/mailer .
* docker run --name mailer --link mailer-mongo:mongo -d \
-e MAILER_AUTH_TOKEN='shared-mailing-token' \
-e MAILER_MONGO_DATABASE='mails' \
-e MAILER_PORT=3003 \
-p 3003:3003 breakout-mailer
* `docker run --name mailer-mongo -p 27017:27017 -d mongo`
* `docker build -t breakout/mailer .`
```
docker run --name mailer --link mailer-mongo:mongo -d
-e MAILER_SENDGRID_KEY='' \
-e MAILER_AUTH_TOKEN='' \
-e MAILER_TEMPLATE_BUTTON_ID='9caf72ad-992d-4f52-b443-a6cbb909b8b7' \
-e MAILER_TEMPLATE_ID='caaef3c0-7e00-495e-9e91-3681c0f28eb1' \
-p 3003:3003 breakout-mailer
```

Usage
-----
Expand Down Expand Up @@ -185,4 +189,4 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
along with this program. If not, see <http://www.gnu.org/licenses/>.
69 changes: 36 additions & 33 deletions mailer.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
'use strict';

var sendgrid = require('sendgrid')(process.env.MAILER_SENDGRID_KEY);
const sgMail = require('@sendgrid/mail');
var uuid = require('node-uuid');
var mongo = require('./mongoose.js');

sgMail.setApiKey(process.env.MAILER_SENDGRID_KEY);

var mailObject = mongo.mongoose.model('mails',
mongo.mongoose.Schema({
id: String,
campaign_code: String,
tos: [{email: String, isbcc: Boolean, events: [{timestamp: Number, message: String, email: String}]}],
substitutions: [],
subject: String,
body: String,
from: String,
attachments: [String],
create_date: Number,
update_date: Number
})
}, { strict: false })
);


Expand Down Expand Up @@ -57,66 +60,66 @@ var sendMail = function (req, res, next) {
//Timestamp - 2 to fix async time issues
var start_date = getTimestamp() - 2;

var email = new sendgrid.Email();
var email = {};

var to = [];

var buttonTexts = [];
var buttonUrls = [];
var headTitles = [];

if (mail.tos !== undefined) {
email.to = mail.tos;

mail.tos.forEach(function (elem) {
email.addTo(elem);
to.push({email: elem, isbcc: false, events: []});
buttonTexts.push(mail.buttonText);
buttonUrls.push(mail.buttonUrl);
headTitles.push(mail.subject);
});
}

if (mail.bccs !== undefined) {
email.bcc = mail.bccs;

mail.bccs.forEach(function (elem) {
console.log(elem);
email.addBcc(elem);
to.push({email: elem, isbcc: true, events: []});
});
}

email.setSubject(mail.subject);
email.setHtml(mail.html);
email.setFrom("[email protected]");
email.setFromName("BreakOut");
email.subject = mail.subject;
email.html = mail.html;
email.from = {
name: 'BreakOut e.V.',
email: '[email protected]',
};

if (mail.files !== undefined) {
/*if (mail.files !== undefined) {
mail.files.forEach(function (elem) {
//TODO download/upload Files
email.addFile(elem);
});
}
}*/

var id = uuid.v4();
email.addUniqueArg("mailer_id", id);

var status = "creating";
var error = "";
email.customArgs = {"mailer_id": id};

email.addFilter('templates', 'enable', 1);
email.addSubstitution('-headTitle-', headTitles);
//email.addFilter('templates', 'enable', 1);

email.substitutionWrappers = ['-', '-'];
if (mail.buttonText && mail.buttonUrl) {
email.addFilter('templates', 'template_id', process.env.MAILER_TEMPLATE_BUTTON_ID);
email.addSubstitution('-buttonText-', buttonTexts);
email.addSubstitution('-buttonUrl-', buttonUrls);
email.templateId = process.env.MAILER_TEMPLATE_BUTTON_ID;
email.substitutions = {
"buttonText": mail.buttonText,
"buttonUrl": mail.buttonUrl,
"headTitle": mail.subject,
};
} else {
email.addFilter('templates', 'template_id', process.env.MAILER_TEMPLATE_ID);
email.templateId = process.env.MAILER_TEMPLATE_ID;
email.substitutions = {
"headTitle": mail.subject
};
}

sendgrid.send(email, function (err, json) {
var status = "creating";
sgMail.send(email, function (err, json) {
var mongomail = new mailObject({
id: id,
tos: to,
subject: mail.subject,
substitutions: email.substitutions,
campaign_code: mail.campaign_code,
body: mail.html,
attachments: mail.files,
Expand All @@ -129,12 +132,12 @@ var sendMail = function (req, res, next) {
console.error(err);
res.json({error: err.message});
} else {
if (json.message === 'success') {
if (json[0].statusMessage === 'Accepted') {
status = "started";
console.log("Sent " + mail.subject + "-Mail to: " + JSON.stringify(mail.tos));
res.json({success: 'ok', mailer_id: id});
} else {
status = json.message;
status = json.statusMessage;
console.error(json);
res.json({error: json});
}
Expand Down
15 changes: 10 additions & 5 deletions mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ var mongoose = require('mongoose');
const MAILER_MONGO_USER = process.env.MAILER_MONGO_USER || "";
const MAILER_MONGO_PASSWORD = process.env.MAILER_MONGO_PASSWORD || "";
const MAILER_MONGO_DATABASE = process.env.MAILER_MONGO_DATABASE || "mails";
const MAILER_MONGO_HOST = `${process.env.MONGO_PORT_27017_TCP_ADDR}:${process.env.MONGO_PORT_27017_TCP_PORT}`;
const MAILER_MONGO_ADDR = process.env.MONGO_PORT_27017_TCP_ADDR || "localhost";
const MAILER_MONGO_PORT = process.env.MONGO_PORT_27017_TCP_PORT || "27017";

const MAILER_MONGO_HOST = `${MAILER_MONGO_ADDR}:${MAILER_MONGO_PORT}`;

const URL = buildMongoUrl(MAILER_MONGO_USER, MAILER_MONGO_PASSWORD, MAILER_MONGO_DATABASE, MAILER_MONGO_HOST);

// Build connection URL dependent on whether a user is specified or not
Expand All @@ -32,7 +36,8 @@ var opt = {
// auth: {
// authdb: 'admin'
// },
server: {auto_reconnect: true}
autoReconnect: true,
useNewUrlParser: true
};

mongoose.connect(URL, opt);
Expand All @@ -53,16 +58,16 @@ db.on('disconnected', function () {
setTimeout(function () {
console.log('reconnecting to MongoDB');
lastReconnectAttempt = new Date().getTime();
mongoose.connect(URL, opt);
mongoose.createConnection(URL, opt);
}, delay);
}
else {
console.log('reconnecting to MongoDB');
lastReconnectAttempt = now;
mongoose.connect(URL, opt);
mongoose.createConnection(URL, opt);
}
});

module.exports = {
mongoose: mongoose
};
};
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node ./bin/www"
"start": "node $NODE_DEBUG_OPTION server.js"
},
"dependencies": {
"body-parser": "~1.17.1",
"express": "~4.15.2",
"mongoose": "^4.3.2",
"@sendgrid/mail": "^6.3.1",
"body-parser": "^1.18.3",
"express": "^4.16.4",
"mongoose": "^5.4.20",
"morgan": "^1.6.1",
"node-uuid": "^1.4.7",
"sendgrid": "2.0.0"
"node-uuid": "^1.4.7"
}
}

0 comments on commit db910f8

Please sign in to comment.