-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·64 lines (60 loc) · 1.43 KB
/
index.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
64
#!/usr/bin/env node
var fs = require('fs');
var domain = 'mg.barcampgb.org'
var config = require('rc')('bcgbmailer', {
domain: domain
});
if (!config.apiKey) {
error('No api key specified!');
}
var mailgun = require('mailgun-js')(config);
var opts = require('nomnom')
.option('list', {
position: 0,
help: 'List to send to',
choices: ['test', 'updates', 2016],
required: true
})
.option('subject', {
abbr: 's',
help: 'Email subject',
required: true
})
.option('body', {
abbr: 'b',
help: 'Email body file in templates',
required: true
})
.option('force', {
abbr: 'f',
help: 'Send the email',
flag: true
})
.nocolors()
.parse();
sendEmail(opts.list, opts.subject, opts.body, !opts.force);
function sendEmail (list, subject, body, dryRun) {
var data = {
to: list + '@' + domain,
from: 'BarCamp Green Bay <info@' + domain + '>',
subject: subject,
text: fs.readFileSync(__dirname + '/templates/' + body, 'utf-8')
};
console.log(data);
if (!data.text) {
error('No email body!');
}
if (dryRun) {
return;
}
mailgun.messages().send(data, function(err, res) {
if (err) {
error(err);
}
console.log(res);
});
}
function error (message) {
console.error('error: ' + message);
process.exit(1);
}