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

标签 javascript node.js sendmail webix

我想通过单击 UI 中的按钮从 webix 应用程序发送电子邮件,这将通过 ajax 调用向后端的 Node 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 调用向 Node 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 本身发送电子邮件而不将请求发送到 Node 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 也有同样的情况,有它需要的特定签名。传入的函数应该具有 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');
}

关于javascript - 如何使用后端 Node JS 服务器的 sendmail 从 Webix 应用程序发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43574901/

相关文章:

javascript - 如何在 html 5 Canvas 上的圆圈内旋转图片?

javascript - 获取innerHTML AS HAS BEEN CHANGED

mysql - 找不到模块 `mysql` node.js

node.js - 如何为IE7编译dust js服务器端渲染

node.js - Npm 安装在 Windows 10 上挂起并失败

javascript - ajax 在前面完成后执行函数

javascript - 使用 JW Player、SWFObject 和 FancyZoom 在隐藏的 div 中不在 IE 中播放视频

php - 从 mysql 创建 excel 并在 php 中将其作为附件电子邮件发送

email - 从 bash 邮件中的地址文本设置

PHP Mail 函数执行时间超过 60 秒(使用 sendmail)