你会如何通过mandrill发送邮件与phantomjs?

时间:2013-05-15 19:17:05

标签: javascript phantomjs mandrill

我正在用phantomjs做一些屏幕抓取。我正在尝试实现一个错误处理系统,当刮刀出现故障时,该系统会发送带有mandrill api https://mandrillapp.com/api/docs/messages.html的电子邮件。

mandrill api采用类似下面的方法。 你会如何通过mandrill发送邮件与phantomjs?

var data = {
    "key": "VdFwNvj-dLwaI6caAh8ODg",
    "message": {
        "html": "This aggression will not stand",
        "text": "This aggression will not stand",
        "subject": "example subject",
        "from_email": "the-dude@gmail.com",
        "from_name": "The Dude",
        "to": [{
            "email": "lebowski@gmail.com",
            "name": "lebowski"
        }]
    },
    "async": false
};

$.ajax({
    type: "POST",
    url: 'https://mandrillapp.com/api/1.0/messages/send.json',
    data: data
});

2 个答案:

答案 0 :(得分:3)

我花了一些时间试图让答案毫无兴趣。我最终测试了:http://httpbin.org/post以找出为什么mandril无法处理我的请求,结果发现数据没有正确发送。 PhantomJS文档也不正确。就是这个例子:https://github.com/adrianchung/phantomjs/blob/069ab5dea4c07c61a0ac259df0ff219ade1e8225/examples/postjson.js最终给了我一个缺失的部分。 mandrill文档也非常有用:https://mandrillapp.com/api/docs/messages.JSON.html

我已完成的代码:

var page = require('webpage').create();
var email_url = 'https://mandrillapp.com/api/1.0/messages/send.json';
//email_url = 'http://httpbin.org/post'; // When testing

var email_data = {
    key: "your-key",
    "message": {
        "text": email_message_text,
        "subject": "Your subject",
        "from_email": "your email",
        "from_name": "Kris",
        "to": [{
            "email": "your email",
            "name": "Kris"
        }]
    },
    "async": false
};
var email_headers = {
    "Content-Type": "application/json"
};

page.open(
    email_url, 
    'post', 
    JSON.stringify(email_data), // You need to stringify the json or it doesn't work
    email_headers, 
    function (status) {

        if (status !== 'success') {
            PrintError('FAIL to email results');
        } else {        
            var result = page.evaluate(function () {
                        return document.body.innerText;
                    });
            Print('Email successfully sent\n' + result);
        }       
        phantom.exit();
     }
 );

答案 1 :(得分:0)

最简单的方法是启动新页面以进行RESTful调用,然后您可以使用page.evaluate来读取结果。虽然它不像jQuery ajax方法那么简单,但您可以轻松创建对象以简化其使用。

var page = require('webpage').create(),
    url = 'https://mandrillapp.com/api/1.0/messages/send.json',
    data = {
    "key": "VdFwNvj-dLwaI6caAh8ODg",
    "message": {
        "html": "This aggression will not stand",
        "text": "This aggression will not stand",
        "subject": "example subject",
        "from_email": "the-dude@gmail.com",
        "from_name": "The Dude",
        "to": [{
            "email": "lebowski@gmail.com",
            "name": "lebowski"
        }]
    },
    "async": false
};

page.open(url, 'POST', data, function (status) {
    if (status !== 'success') {
        console.log('FAIL to load the log');
    } else {
        console.log('Log success');

        var result = page.evaluate(function () {
            return document.body.innerText;
        });

        console.log("log Result: " + result);
    }       
});