注册送短信

DEMO: Mail/Send - 邮件发送

代码示例

var request = require('request');
var crypto = require('crypto');
var fs = require('fs');

var appid = "input your appid";
var appkey = "input your appkey";
var api = "https://api-v4.mysubmail.com/mail/send";
var attach1 = fs.createReadStream("/Users/duan/Desktop/12455.png");
var attach2 = fs.createReadStream("/Users/duan/Desktop/12345.png");

//生成加密签名

function BuildSignature(params){
    var sorted = [];
    for(var key in params) {
        sorted[sorted.length] = key;
    }
    sorted.sort();
    var tempDict = {};
    for(var i = 0; i < sorted.length; i++) {
        tempDict[sorted[i]] = params[sorted[i]];
    }
    var signStr = "";
    for(var key in tempDict) {
        signStr += key + '=' + tempDict[key] + '&'; 
    }
    signStr = signStr.substring(0, signStr.length-1);
    signStr = appid + appkey + signStr + appid + appkey; 
    var md5sum = crypto.createHash('md5');
    md5sum.update(signStr);
    return md5sum.digest('hex');
}

//无加密示例

request.post({
    url: api, 
    formData: {
        "appid":appid,
        "signature":appkey, 
        "from" : "submail@yilinglingyi.xyz",
        "subject" : "来自 SUBMAIL 的端午祝福",
        "html" : "<body><h1>您好,祝您阖家欢乐,万事如意</h1></body>",
        "to" : "514******@qq.com",
        "attachments[]" : [
            attach1,
            attach2
        ],
    }
}, function optionalCallback(err, httpResponse, body) {
    if (err) {
        return console.error(err);
    }
    console.log(body);
});

//加密示例

request({
    uri: "https://api-v4.mysubmail.com/service/timestamp",
    method: 'GET'
}, function(error, response, body) {
    var result = JSON.parse(body);
    var requestParams = {}
    requestParams['timestamp'] = result["timestamp"];
    requestParams['sign_type'] = "md5";
    requestParams['appid'] = appid;
    requestParams['sign_version'] = "2";
    requestParams['to'] = "514******@qq.com";
    requestParams['from'] = "submail@yilinglingyi.xyz";
    requestParams['subject'] = "来自 SUBMAIL 的端午祝福";
    requestParams['signature'] = BuildSignature(requestParams);
    requestParams['html'] = "<body><h1>您好,祝您阖家欢乐,万事如意</h1></body>";
    requestParams['attachments[]'] = [attach1,attach2];
    
    request.post({
        url: api, 
        formData: requestParams
    }, function optionalCallback(err, httpResponse, body) {
        if (err) {
            return console.error(err);
        }
        console.log(body);
    });
});