Calling a Nodemailer.js via Javascript (AJAX)

Asked

Viewed 59 times

0

Hello, I wonder if it is possible to run a nodemailer via javascript (AJAX).
Because it returns error and I can’t debug (because it’s on the server).
This is the right way to call the file?
Thank you

The code of Nodemailer.js

var nodemailer = require("nodemailer");

var transporter = nodemailer.createTransport({
    host: "someHost",
    port: 465,
    secure: true,
    auth: {
        user: "[email protected]",
        pass: "password"
    }
});

var mailOptions = {
    from: '"Você" <[email protected]>',
    to: "[email protected]",
    subject: "Nodejs - Teste para o tutorial ??",
    text: "O teste foi efectuado com sucesso"
}

transporter.sendMail(mailOptions, function(err, info){
    if(err){
        console.log(err); // Não retorna este console
    }else{
        console.log("Mensagem enviada com sucesso"); //Este também não
    }
});

And this is via Javascript trying to run the Node file

function enviaEmail() {
    setLoadingBoxDisplay("block");
    $.ajax({
        url: "../../scripts/nodeMailer.js",
        async: true,
        dataType: "json",
        type: "GET",
        success: function (data) {
            console.log("Sucess");
        },
        complete: function () {
            setLoadingBoxDisplay("none");
        },
        error: function (data) {
            console.log("Fail");
            //var exc = data.responseJSON;
            //window.location.reload();
        }
    });
}

  • Calling the file directly not, you have to insert the nodemailer in your web application, in the case of the nodemailer a service with nodejs, create a server and a route so that your front end application can consume it and so the nodejs takes charge of sending the email.

  • How can I insert nodemailer into a web application??

  • Are you using Node.js? The nodemailer is a module of it...

  • Yes yes I am, is the first code posted, I will search more about here

  • 1

    So the idea is that you are running a service with nodejs, create a route and this route will have the nodemailer and the upload configuration. Then you have your AJAX request the route you created...

  • Got it, I’ll test it, thanks

  • Because what I’m doing is, it has a Node file on the server and requires a path to this AJAX file, but it falls on console.log error, apparently is going wrong

  • You are making a GET direct to the file, and it is not for it that you should do and yes to a route type: /send email in this case you configure this route in your web service.

  • Right!! I’ll get back to you soon, thank you

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.