summaryrefslogtreecommitdiff
path: root/lib/awmail/index.js
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2017-05-12 17:28:33 +0200
committerJules Laplace <jules@okfoc.us>2017-05-12 17:28:33 +0200
commite60253e2c14ad6fa95e5c7801b378569c3c44400 (patch)
tree1c216753d230556d857e15b6a9c94b84ff149a10 /lib/awmail/index.js
parent6850b2e61448fc41eb1d486b251702ba6fa5c736 (diff)
sending mail
Diffstat (limited to 'lib/awmail/index.js')
-rw-r--r--lib/awmail/index.js185
1 files changed, 185 insertions, 0 deletions
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