-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
92 lines (72 loc) · 2.18 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* index.js: Sendgrid SMTP API Worker
*
* (C) 2012 Jacob Williams.
*/
"use strict";
var async = require( 'async' ),
https = require( 'https' ),
querystring = require( 'querystring' ),
_ = require( 'underscore' ),
config = require ( 'config' );
var API_ENDPOINT = "sendgrid.com",
HTTP_VERB = "POST",
REQUEST_URI = "/api/mail.send.json";
var send = function send( params, callback ) {
callback = callback || function() { };
params = params || {};
//
// required params
//
if ( ! params.to ) return callback( { message : "missing recipient" } );
if ( ! params.from ) return callback( { message : "missing sender" } );
if ( ! params.subject ) return callback( { message : "missing subject" } );
if ( ! params.text && ! params.html ) return callback( { message : "missing body" } );
if ( params.toname && _.isArray(params.to)
&& ( ! _.isArray(params.toname) || params.toname.length !== params.to.length) ) {
return callback( { message: "tonames length does not match to length" } );
}
var data = {
api_user : config.api_user,
api_key : config.api_key,
to : params.to,
from : params.from,
subject : params.subject,
text : params.text,
html : params.html,
bcc : params.bcc,
replyTo : params.replyTo
}
var postData = querystring.stringify(data);
var requestOptions = {
host : API_ENDPOINT,
method : HTTP_VERB,
path : REQUEST_URI,
headers : {
'Content-Type' : 'application/x-www-form-urlencoded',
'Content-Length' : postData.length
}
}
var req = https.request( requestOptions );
req.on( 'response', function ( response ) {
var body = "";
response.setEncoding( 'utf8' );
response.on( 'data', function ( chunk ) {
body += chunk;
});
response.on( 'end', function () {
var json = JSON.parse(body);
if (json.message == "success") {
return callback( null, json )
} else {
return callback( json.error );
}
});
}); // req.on 'response'
req.on( 'error', function ( error ) {
return callback( error );
});
req.write(postData);
req.end();
}; // makeRequest
crosstalk.on( 'sendgrid.smtp.send', send );