如何使用后端节点JS服务器的sendmail从Webix应用程序发送电子邮件

时间:2017-04-23 18:27:36

标签: javascript node.js sendmail webix

我想通过点击UI中的按钮从webix应用程序发送电子邮件,该按钮将通过ajax调用向后端的节点JS服务器发送post请求。 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: 'xyz@support.com',
        to: 'abc@support.com',
        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包来实现这一点我正在尝试?

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

您的问题不在于您使用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.getapp.post也是如此,它需要一定的签名。传入的函数应该具有reqres参数。您也可以选择最后添加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');
}