1

我正在用 phantomjs 进行一些屏幕抓取。我正在尝试实现一个错误处理系统,该系统在刮板失败时使用 mandrill api https://mandrillapp.com/api/docs/messages.html发送电子邮件。

mandrill api 采用如下所示的 post 方法。您将如何使用 phantomjs 通过 mandrill 发送邮件?

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
});
4

2 回答 2

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();
     }
 );
于 2014-10-15T00:26:56.367 回答
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);
    }       
});
于 2013-05-19T03:49:03.590 回答