Sunday, 23 April 2017

How to send an email from a Webix application using sendmail of node JS server at the backend

I want to send an email from a webix application by clicking a button in the UI, which will send a post request through an ajax call to the node JS server at the backend. The webix part looks like below:

{   id:'tb',
    view: 'toolbar',
    cols: [

    {view:"button", id:"mail_btn", type:"icon", label:"SendEmail", tooltip:"Send an email",  width:100, on: {onItemClick:function(){sendEmail()}} },     
       ]
}

The callback function:

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

The above ajax call sends a request to the node JS where I am using sendmail npm package to achieve this. The code looks like below :

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

    }

However, I am getting below error :

Error: Route.post() requires callback functions but got a [object Undefined]

Is there a way to send email from webix itself without sending the request to node JS server ? Or else how to use the sendmail npm package to achieve this the way I am trying ?

Any help would be appreciated.



via A.G.Progm.Enthusiast

No comments:

Post a Comment