如何使用后台节点JS服务器的sendmail发送来自Webix应用程序的电子邮件
问题描述:
我想通过单击UI中的按钮发送来自webix应用程序的电子邮件,该按钮将通过ajax调用发送发送请求到后端的节点JS服务器。 的webix部分看起来像下面:如何使用后台节点JS服务器的sendmail发送来自Webix应用程序的电子邮件
{ id:'tb',
view: 'toolbar',
cols: [
{view:"button", id:"mail_btn", type:"icon", label:"SendEmail", tooltip:"Send an email", width:100, on: {onItemClick:function(){sendEmail()}} },
]
}
回调函数:
function sendEmail() {
var bodypart = {"message" : "This is a test mail"};
$.ajax({
type: 'POST',
url: '/appl/email',
data: bodypart,
success: function (data) {
console.log("success");
},
error: function(err){
console.log(err);
}
});
}
}
上面AJAX调用将请求发送到所述节点JS我在哪里使用sendmail NPM包来实现这一点。代码如下所示:
var sendmail = require('sendmail')();
app.post('/appl/email', sendmail());
function sendEmail() {
sendmail({
from: '[email protected]',
to: '[email protected]',
subject: 'test sendmail',
html: 'Mail of test sendmail ',
}, function(err, reply) {
console.log(err && err.stack);
console.dir(reply);
});
}
不过,我得到以下错误:
Error: Route.post() requires callback functions but got a [object Undefined]
有没有办法从webix自己发送电子邮件,而不发送到节点JS服务器的请求? 或者如何使用sendmail npm包来实现这个我尝试的方式?
任何帮助,将不胜感激。
答
你的问题不像你使用sendmail的方式,而是你使用快递路线的方式。
这里是一个示例代码,我只是鞭打,给了我你的代码中得到的同样的错误。
const express = require('express');
const app = express();
app.get('/', doSomething());
function doSomething() {
console.log('this is a sample test');
}
app.listen(3000,() => console.log('server is running'));
问题是app.get
,同样会为app.post
是真实的,有它需要一定的签名。传入的函数应该有req
和res
参数。您还可以选择最后添加next
参数。
这是我上面的代码将如何修复。
const express = require('express');
const app = express();
app.get('/', (req, res) => {
doSomething();
res.json('success');
});
function doSomething() {
console.log('this is a sample test');
}
感谢您指出错误。我纠正了它,现在它正在工作。 –