我正在尝试使用phantomjs创建一个应用程序,这需要mailgun服务来发送电子邮件。由于没有官方的mailgun phantomjs库,我在电子邮件中附加文件时遇到了一些麻烦。电子邮件已成功发送,但我没有看到任何附件。
以下是代码:
function ObjToQs(obj) {
var str = "";
for (key in obj) {
str += key + '=' + obj[key] + '&';
}
str = str.slice(0, str.length - 1);
return str;
}=
var page = require('webpage').create(),
url = 'https://api.mailgun.net/v3/sandboxbxxxxxxxxxxxxxxxxxxxxxxxx.mailgun.org/messages',
data = {
from: "Ganesh <mail@gmail.com>",
to: "email@gmail.com",
subject: "subject!",
text: "Body",
attachment: '/path/test.txt'
};
console.log(ObjToQs(data));
page.customHeaders = {'Authorization': 'Basic ' + btoa('api:key-xxxxxxxx')};
page.open(url, 'post', ObjToQs(data), function (status) {
if (status !== 'success') {
console.log('FAIL to load the log');
console.log(status);
} else {
console.log('Log success');
var result = page.evaluate(function () {
return document.body.innerText;
});
console.log("log Result: " + result);
phantom.exit();
}
});
我该怎么办?
谢谢!