liaosankai
10/19/2015 - 7:47 AM

Use mailgun curl send message in total.js

Use mailgun curl send message in total.js

/**
 * @see http://www.mailgun.com/#curlsample
 * @see http://docs.totaljs.com/v1.9.x/en.html#api~FrameworkUtils~Utils.request
 */
function json_mailgun() {
    var self = this;
    Utils.btoa = function(str) {
        return (str instanceof Buffer) ? str.toString('base64') : new Buffer(str.toString(), 'binary').toString('base64');
    };
    Utils.atob = function(str) {
        return new Buffer(str, 'base64').toString('binary');
    };
    var apiUrl = 'https://api.mailgun.net/v3';
    var domain = 'samples.mailgun.org';
    var apiKey = 'key-3ax6xnjp29jd6fds4gc373sgvjxteol0';
    var method = 'messages';
    Utils.request(apiUrl + '/' + domain + '/' + method, ['post'], {
        'from': 'Excited User <excited@samples.mailgun.org>',
        'to': 'devs@mailgun.net',
        'subject': 'Hello',
        'text': 'Testing some Mailgun awesomeness!'
    }, function(err, data, status, headers) {
        self.json({
            "err": err,
            "data": data,
            "status": status,
            "headers": headers
        });
    }, {
        /* cookies */
    }, {
        /* headers */
        'Authorization': 'Basic ' + Utils.btoa("api:" + apiKey)
    }, 'utf8', 10000);
}