-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
70 lines (59 loc) · 1.36 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
65
66
67
68
69
70
var https = require('https')
;
var validator = function(pubKey) {
this.pubKey = pubKey;
}
validator.prototype.sendBack = function(err, msg, fn) {
if (fn) {
fn.call(this, err, msg);
}
else {
console.log(err ? err : msg);
}
}
validator.prototype.validate = function(email, fn) {
// There should be a public key
if (!this.pubKey) {
this.sendBack('Provide your mailgun public key.', null, fn);
return;
}
// There should be an email
if (!email) {
this.sendBack('Provide the email to validate.', null, fn);
return;
}
var self = this;
var options = {
hostname: 'api.mailgun.net',
path: '/v2/address/validate?address='+email+'&api_key='+this.pubKey,
method: 'GET'
};
var req = https.request(options, function(res) {
//console.log("statusCode: ", res.statusCode);
var result = "";
var status = res.statusCode;
res.setEncoding('utf8');
res.on('data', function(chunk) {
result += chunk;
});
res.on('end', function(body) {
if (status != 200) {
self.sendBack('Unexpected response code '+status, null, fn);
}
else {
var json = JSON.parse(result);
if (json.message)
self.sendBack(json.message, null, fn);
else
self.sendBack(null, json, fn);
}
//console.log(chunk);
});
});
req.end();
req.on('error', function(e) {
//console.error(e);
self.sendBack(e, null, fn);
});
}
module.exports = validator;