-
Notifications
You must be signed in to change notification settings - Fork 1
/
mandrill.js
63 lines (52 loc) · 1.92 KB
/
mandrill.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
///////////////////////////////////////////////////
//////////////// Mandrill Settings ////////////////
///////////////////////////////////////////////////
var apiKey = 'XXXXXXXXXXXXXXX'
var fromEmail = '[email protected]'
var options = {
host: 'www.mandrillapp.com',
port: 80,
path: '/api/1.0/messages/send.json',
method: 'POST'
};
///////////////////////////////////////////////////
var mandrill = require('mandrill-api/mandrill');
var mandrill_client = new mandrill.Mandrill(apiKey);
var fs = require('fs');
var mandrill = function (santaToChild, contactBook, emailSubject, emailBody) {
this.santaToChild = santaToChild
this.contactBook = contactBook
this.emailSubject = emailSubject
this.emailBody = emailBody
this.result = 'No errors, successfully sent emails.'
};
mandrill.prototype.sendEmails = function () {
for (santa in this.santaToChild) {
var childName = this.santaToChild[santa]
var santasEmail = this.contactBook[santa]
// there should be a better way to do this, i.e. feed arguments into html file?
var message = {
"from_email": fromEmail,
"to": [
{
"email": santasEmail,
"name" : santa,
"type": "to"
},
],
"autotext": "true",
"subject": this.emailSubject,
"html": "<html><body><p><strong>You're giving a gift to: " + childName + "!</strong></p>" + this.emailBody + "<p>This message was made possible by <a href='https://github.com/wickyvinn/secret-santa'>wickyvinn's secret santa app!</a> </p></body></html>"
};
mandrill_client.messages.send({"message": message}, function(result) {
this.result = result;
console.log("completed sending email to " + santasEmail)
}, function(e) {
// instead, throw a 400.
this.result = e.name
console.log("error result: " + this.result);
});
};
return this.result
};
module.exports = mandrill;