diff options
| author | Jules Laplace <jules@okfoc.us> | 2017-05-12 17:28:33 +0200 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2017-05-12 17:28:33 +0200 |
| commit | e60253e2c14ad6fa95e5c7801b378569c3c44400 (patch) | |
| tree | 1c216753d230556d857e15b6a9c94b84ff149a10 | |
| parent | 6850b2e61448fc41eb1d486b251702ba6fa5c736 (diff) | |
sending mail
| -rw-r--r-- | index.js | 8 | ||||
| -rw-r--r-- | lib/awmail/index.js | 185 | ||||
| -rw-r--r-- | package.json | 9 | ||||
| -rw-r--r-- | templates/email.html | 7 | ||||
| -rw-r--r-- | templates/email.txt | 8 |
5 files changed, 213 insertions, 4 deletions
@@ -69,6 +69,14 @@ var app = okcms.createApp({ dirname: process.env.S3_DIRNAME, maxbytes: 1024*1024*2, }, + awmail: { + lib: require("./lib/awmail"), + apikey: process.env.MAILGUN_API_KEY, + domain: process.env.MAILGUN_DOMAIN, + secret: process.env.MAIL_SECRET, + from: 'Hansel and Gretel <mail@hanselandgretelarmory.com>', + subject: 'We Found Your Face', + }, }, }).listen(process.env.PORT || 1337) diff --git a/lib/awmail/index.js b/lib/awmail/index.js new file mode 100644 index 0000000..4bfd08e --- /dev/null +++ b/lib/awmail/index.js @@ -0,0 +1,185 @@ +var Liquid = require('liquid-node') +var fs = require('fs') +var Q = require('q') +var htmlTemplate = fs.readFileSync('templates/email.html', 'utf8') +var textTemplate = fs.readFileSync('templates/email.txt', 'utf8') +var mailgun = require('mailgun.js') +var request = require('request') + +/* + awmail: { + lib: require("./lib/awmail"), + apikey: process.env.MAILGUN_API_KEY, + domain: process.env.MAILGUN_DOMAIN, + from: 'Postmaster <mail@example.com>', + subject: 'Your Result', + } +*/ + +function AWMail (options) { + if (!(this instanceof AWMail)) return new AWMail(options) + options = options || {} + if (!options.express) + throw new Error('Express not provided to AWMail') + if (!options.config) + throw new Error('Configuration not provided to AWMail') + + var express = options.express + var router = express.Router() + var config = options.config + + var engine = new Liquid.Engine + + var mg = mailgun.client({ + username: 'api', + key: process.env.MAILGUN_API_KEY, + }) + + router.post('/send', function (req, res) { + res.header('Access-Control-Allow-Origin', '*') + res.header('Access-Control-Allow-Headers', 'X-Requested-With') + + var email = req.body.email + var track = req.body.track + var hash = req.body.hash + var secret = req.body.secret + + if (! hash || secret !== config.secret) { + return res.sendStatus(500) + } + + deferToNextTick().then(function(){ + console.log("parse templates") + var templateData = { + email: email, + hash: hash, + } + return parseTemplates(templateData) + }).then(function(mailData){ + console.log("send mail") + mailData.email = email + return sendMail(mailData) + }).then(function(){ + console.log("store email") + if (track) { + return storeEmail(email) + } else { + return Q.Promise(function(resolve, reject, notify) { resolve() }) + } + }).then(function(){ + console.log("all done") + }).catch(function(err){ + console.log(err) + }) + + res.sendStatus(200) + }) + + router.get('/test', function (req, res) { + var hash = 'test' + var email = 'julescarbon@gmail.com' + deferToNextTick().then(function(){ + console.log("parse templates") + var templateData = { + email: email, + hash: hash, + } + return parseTemplates(templateData) + }).then(function(mailData){ + console.log("send mail") + mailData.email = email + return sendMail(mailData) + }).then(function(){ + console.log("store email") + return storeEmail(email) + }).then(function(){ + console.log("all done") + }).catch(function(err){ + console.log(err) + }) + + res.sendStatus(200) + }) + + function deferToNextTick(){ + return Q.Promise(function(resolve, reject, notify) { + process.nextTick(function(){ + resolve() + }) + }) + } + +/* + function uploadImage (data){ + return Q.Promise(function(resolve, reject, notify) { + upload.put({ + file: data.file, + preserveFilename: false, + dirname: "nars/mail", + types: { + 'image/gif': 'gif', + 'image/jpeg': 'jpg', + 'image/jpg': 'jpg', + 'image/png': 'png', + }, + unacceptable: function(err){ + reject(new Error("S3 error: " + err)) + }, + success: function(url){ + resolve(url) + } + }) + }) + } +*/ + + function parseTemplates (data){ + return Q.Promise(function(resolve, reject, notify) { + engine.parseAndRender(textTemplate, data).then(function(textResult){ + engine.parseAndRender(htmlTemplate, data).then(function(htmlResult){ + resolve({ + text: textResult, + html: htmlResult + }) + }).catch(function(){ reject(new Error("Error building text template")) }) + }).catch(function(){ reject(new Error("Error building html template")) }) + }) + } + + function sendMail (content){ + return mg.messages.create(config.domain, { + from: config.from, + to: [content.email], + subject: config.subject, + text: content.text, + html: content.html, + }) + } + + function storeEmail (mail){ + return Q.Promise(function(resolve, reject, notify) { + var data = {} + data['Email'] = mail + data['entry.1571194529'] = mail + data['fvv'] = "1" + + var url = "https://docs.google.com/forms/d/e/1FAIpQLSfBdSrjLyoZwnttbeQ5v_kuW8n9k9CGWfXDSHTNixHOlvsxCg/formResponse" + + request({ url: url, qs: data }, function (err, response, body) { + if (err || response.statusCode !== 200) { + reject(err) + } else { + resolve() + } + }) + }) + } + + this._router = router +} + +AWMail.prototype.middleware = function () { + return this._router +} + +module.exports = AWMail diff --git a/package.json b/package.json index 3eb3cdf..a80c43e 100644 --- a/package.json +++ b/package.json @@ -6,9 +6,10 @@ "scripts": {}, "author": "Undisclosed LLC", "dependencies": { - "okcms": "git+ssh://git@ghghgh.us/~/okcms.git#v0.2.5" + "liquid-node": "^2.6.1", + "mailgun.js": "^2.0.1", + "okcms": "git+ssh://git@ghghgh.us/~/okcms.git#v0.2.5", + "request": "^2.81.0" }, - "devDependencies": { - } + "devDependencies": {} } - diff --git a/templates/email.html b/templates/email.html new file mode 100644 index 0000000..f2f00f4 --- /dev/null +++ b/templates/email.html @@ -0,0 +1,7 @@ +HANSEL AND GRETEL +<br><br> +<img src='{{url}}'> +<br><br> +<a href='https://hanselgretelarmory.com/photo/{{hash}}'>Download your photo</a> +<br><br> + diff --git a/templates/email.txt b/templates/email.txt new file mode 100644 index 0000000..b36490b --- /dev/null +++ b/templates/email.txt @@ -0,0 +1,8 @@ +HANSEL AND GRETEL + +We found your face! + +<a href='https://hanselgretelarmory.com/photo/{{hash}}'>Download your photo</a> + +ARMORY ON PARK + |
