forked from flovilmart/parse-mailgun
-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
53 lines (50 loc) · 1.2 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
var Parse = require("parse-cloud").Parse;
var Buffer = require('buffer').Buffer;
var Mailgun = {
sendEmail: function(params, options){
var url = 'https://api.mailgun.net/v2/'+Mailgun.domainName+'/messages'
console.log(url);
var p = new Parse.Promise();
var resolved;
var successCallback = function(httpResponse){
if(resolved) {
return;
}
resolved = true;
if (options && options.success) {
options.success(httpResponse.data);
}
p.resolve(httpResponse.data);
}
var errorCallback = function(httpResponse){
if(resolved) {
return;
}
resolved = true;
if (options && options.error) {
options.error(httpResponse);
}
p.reject(httpResponse);
}
Parse.Cloud.httpRequest({
url: url,
method: 'POST',
headers: {
'Authorization': 'Basic ' + Mailgun.auth
},
body: params,
success: successCallback,
error: errorCallback
}).then(successCallback, errorCallback);
}
};
module.exports = {
initialize: function(domainName, apiKey){
Mailgun.domainName = domainName;
Mailgun.apiKey = apiKey;
Mailgun.auth = new Buffer('api:' + apiKey).toString('base64');
},
sendEmail: function(params, options){
return Mailgun.sendEmail(params, options);
}
}