From 2e21d2ec31067fc15f72769942b2428ccfbe7536 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Je=CC=81ro=CC=82me=20Loi=CC=88?= Date: Wed, 24 Sep 2014 13:43:45 +0200 Subject: [PATCH] switch to mailgun for mails, READ THE README, breaking changes (backup config.js) --- .gitignore | 1 + README.md | 4 ++++ config.js => config-sample.js | 9 +++++---- package.json | 2 +- server.js | 1 + src/email-client.js | 6 +++--- src/email-submit.js | 36 ++++++++++++++++++++++------------- src/mail.js | 0 8 files changed, 38 insertions(+), 21 deletions(-) rename config.js => config-sample.js (60%) create mode 100644 src/mail.js diff --git a/.gitignore b/.gitignore index fb280fe..24f43ca 100644 --- a/.gitignore +++ b/.gitignore @@ -17,4 +17,5 @@ results node_modules html/assets/js/email* /config.json +config.js diff --git a/README.md b/README.md index 763b3e5..5ff5d86 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ # SquatConf website. +```cp config-sample.js config.js``` + +edit congif with the mailgun api key + ```npm i && npm start``` \ No newline at end of file diff --git a/config.js b/config-sample.js similarity index 60% rename from config.js rename to config-sample.js index 12b69c4..f78ed8f 100644 --- a/config.js +++ b/config-sample.js @@ -6,11 +6,12 @@ module.exports = require('rc')(name, { db_opts: { valueEncoding: 'json' } , db_path: join(cwd, 'db', name) , port: 8000 - , host: "http://test.squatconf.eu" + , host: 'http://test.squatconf.eu' + , mailjetKey: 'key-...' , email: { - from : "no-reply@squatconf.eu" - , subject : "Hello, everyone is welcome at SquatConf.." - , bodyText : "Please verify that you wish to signup by following this link\n%link%\nYou can ignore this message if you DID NOT request to signup at our website\nhttp://squatconf.eu\n\nThe next event is in Paris, we hope to see you there !!\n\nKind regards from the team,\nSquatConf Paris 2014" + from : 'no-reply@squatconf.eu' + , subject : 'Hello, everyone is welcome at SquatConf..' + , bodyText : 'Please verify that you wish to signup by following this link\n%link%\nYou can ignore this message if you DID NOT request to signup at our website\nhttp://squatconf.eu\n\nThe next event is in Paris, we hope to see you there !!\n\nKind regards from the team,\nSquatConf Paris 2014' } }) diff --git a/package.json b/package.json index 1f173df..16c3855 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "ecstatic": "^0.5.4", "level": "^0.18.0", "load-svg": "^1.0.0", - "nodemailer": "^1.3.0", + "mailgun": "^0.5.0", "rc": "^0.5.1", "stack": "^0.1.0", "tiny-route": "^2.1.1", diff --git a/server.js b/server.js index 267e133..754e73d 100644 --- a/server.js +++ b/server.js @@ -18,6 +18,7 @@ if(!fs.existsSync(config.db_path)){ }); } + stack.errorHandler = function error(req, res, err) { res.statusCode = 302 res.setHeader('Location', '/') diff --git a/src/email-client.js b/src/email-client.js index d008f5e..efa385f 100644 --- a/src/email-client.js +++ b/src/email-client.js @@ -10,9 +10,9 @@ process.nextTick(function() { , status_msg = document.getElementById('status-msg') , logoCont = document.getElementById('logoCont') - loadsvg('/assets/img/squatconf_baguette.svg', function (err, svg) { - logoCont.appendChild(svg); - }); + loadsvg('/assets/img/squatconf_baguette.svg', function (err, svg) { + logoCont.appendChild(svg); + }); for (var n = 0, l = form.childNodes[1].childNodes.length; n < l; n++) { var el = form.childNodes[1].childNodes[n] diff --git a/src/email-submit.js b/src/email-submit.js index 5c0e887..3c8d133 100644 --- a/src/email-submit.js +++ b/src/email-submit.js @@ -2,6 +2,11 @@ var sanitize = require('xss-escape') , rn = require('./rng') , ip = require('./ip-trace') , config = require('../config') + , Mailgun = require('mailgun').Mailgun + , mg = new Mailgun(config.mailjetKey); + + + module.exports = function(db) { return function (req, res, next) { @@ -33,29 +38,34 @@ module.exports = function(db) { if (err) return error(err) // db write OK.. - var nodemailer = require('nodemailer') - , transporter = nodemailer.createTransport() - , url = config.host +'/confirm' + var url = config.host +'/confirm' , qstr = '?email='+ email +'&token='+ obj.token , link = url + qstr +'\n\n' - - var opts = { + , opts = { from : config.email.from , to : email , subject: config.email.subject , text : config.email.bodyText.replace(/\%link\%/, link) } - transporter.sendMail(opts, function(err, data) { - if (err) return error(err) + mg.sendText( + opts.from + , email + , opts.subject + , opts.text + , {} + , function(err) { + if (err) console.log('Guru meditation: ' + err); + else{ + res.statusCode = 302 + res.setHeader('Location', '/') + console.log('email sent..', opts)} + return res.end() + }); + + - // validation email sent - console.log('email sent..', opts) - res.statusCode = 302 - res.setHeader('Location', '/') - return res.end() - }) }) } else { diff --git a/src/mail.js b/src/mail.js new file mode 100644 index 0000000..e69de29