1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
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')
var multer = require('multer')
var PassThrough = require('stream').PassThrough
var upload = require('../../node_modules/okcms/app/node_modules/okservices/oks3/upload')
/*
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 mult = multer()
var engine = new Liquid.Engine
var mg = mailgun.client({
username: 'api',
key: process.env.MAILGUN_API_KEY,
})
router.post('/send', mult.single('image'), 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 secret = req.body.secret
if (secret !== config.secret) {
return res.sendStatus(500)
}
deferToNextTick().then(function(){
console.log("upload image")
return uploadImage({
file: req.file,
})
}).then(function(url){
console.log("parse templates")
console.log(url)
var templateData = {
email: email,
}
return parseTemplates(templateData)
}).then(function(mailData){
console.log("send mail")
mailData.email = email
mailData.image = req.file
return sendMail(mailData)
}).then(function(){
console.log("store email")
if (String(track) === 'true') {
return storeEmail(email)
} else {
return Q.Promise(function(resolve, reject, notify) { resolve() })
}
}).then(function(){
console.log("all done")
}).catch(function(err){
console.error(err.stack)
})
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) {
console.log(data.file)
upload.put({
file: data.file,
preserveFilename: false,
dirname: "armory/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){
var image = new PassThrough()
image.path = 'face.jpg'
image.end(content.image.buffer)
return mg.messages.create(config.domain, {
from: config.from,
to: [content.email],
subject: config.subject,
text: content.text,
html: content.html,
inline: [image],
})
}
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
|