-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmailgun.js
63 lines (56 loc) · 1.36 KB
/
mailgun.js
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
'use strict'
const fs = require('fs')
const os = require('os')
const request = require('request');
function email(mailgun, emailData) {
return new Promise((resolve, reject) => {
mailgun.messages().send(emailData, function (error, body) {
if(error) {
reject(error)
} else {
resolve(body);
}
});
})
}
function getHttpFile(url, filename, cb) {
request(url)
.on('error', function(err) {
cb(err)
})
.on('end', function() {
cb()
})
.pipe(fs.createWriteStream(filename))
}
function isValidMailgunConfig(config) {
return Boolean(config.apiKey &&
config.domain &&
config.from &&
config.to &&
config.subject)
}
function createMailgunClient(config) {
var mailgun = require('mailgun-js')({
apiKey: config.apiKey,
domain: config.domain
});
return mailgun
}
function createEmailData(config, filename, text) {
var emailData = {
from: config.from,
inline: filename,
subject: `${new Date().getTime()} ${config.subject}`,
text: `${text}\n\n${new Date()}\n${os.hostname()}`,
to: config.to,
};
return emailData
}
module.exports = {
createEmailData,
createMailgunClient,
email,
getHttpFile,
isValidMailgunConfig
}