-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
120 lines (114 loc) · 3.59 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* Created by luzq on 2016/12/24.
*/
const crypto = require("crypto");
const request = require("request");
const debug = require('debug')('email');
const url = 'http://dm.aliyuncs.com/';
module.exports = function (config, cb) {
const nonce = Date.now();
const date = new Date();
const errorMsg = [];
config = config || {};
if (!config.accessKeyID) {
errorMsg.push('accessKeyID required');
}
if (!config.accessKeySecret) {
errorMsg.push('accessKeySecret required');
}
if(!config.accountName){
errorMsg.push('accountName required')
}
var param = {
};
if(config.action == 'SingleSendMail'){
if(!config.toAddress){
errorMsg.push('toAddress required')
}
param = {
AccessKeyId: config.accessKeyID,
Action: 'SingleSendMail',
Format: 'JSON',
AccountName:config.accountName,
ReplyToAddress:!!config.replyToAddress,
AddressType:typeof config.addressType == 'undefined'? 0 : config.addressType,
ToAddress:config.toAddress,
SignatureMethod: 'HMAC-SHA1',
SignatureNonce: nonce,
SignatureVersion: '1.0',
TemplateCode: config.templateCode,
Timestamp: date.toISOString(),
Version: '2015-11-23'
};
if(config.fromAlias){
param.FromAlias = config.fromAlias;
}
if(config.subject){
param.Subject = config.subject;
}
if(config.htmlBody){
param.HtmlBody = config.htmlBody;
}
if(config.textBody){
param.TextBody = config.textBody;
}
}else if(config.action == 'BatchSendMail'){
if(!config.templateName){
errorMsg.push('templateName required')
}
if(!config.receiversName){
errorMsg.push('receiversName required')
}
param = {
AccessKeyId: config.accessKeyID,
Action: 'BatchSendMail',
Format: 'JSON',
AccountName:config.accountName,
AddressType:typeof config.addressType == 'undefined'? 0 : config.addressType,
TemplateName:config.templateName,
ReceiversName:config.receiversName,
SignatureMethod: 'HMAC-SHA1',
SignatureNonce: nonce,
SignatureVersion: '1.0',
TemplateCode: config.templateCode,
Timestamp: date.toISOString(),
Version: '2015-11-23'
};
if(config.tagName){
param.TagName = config.tagName;
}
}else{
cb('error action',null);
}
if (errorMsg.length) {
return cb(errorMsg.join(','));
}
var signStr = [];
for (var i in param) {
signStr.push(encodeURIComponent(i)+'='+encodeURIComponent(param[i]));
}
signStr.sort()
signStr = signStr.join('&');
signStr = 'POST&%2F&' + encodeURIComponent(signStr);
const sign = crypto.createHmac("sha1", config.accessKeySecret + '&')
.update(signStr)
.digest('base64');
const signature = encodeURIComponent(sign);
var reqBody = ['Signature='+signature];
for (var i in param) {
reqBody.push(i+'='+param[i]);
}
reqBody = reqBody.join('&');
debug('signature: %s', signature);
debug('request body: %s', reqBody);
request({
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
uri: url,
body: reqBody,
method: 'POST'
}, function (err, res, body) {
cb(err, body);
})
};