Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

338
Visualizações
Nodemailer not sending response

I'm working on my first express project. I'm using Nodemailer and it's sending emails just fine but I'm not getting a response. Any insights? Here is the function I'm using to send the emails. It console logs message sent just fine, but doesn't seem to respond.

exports.email = function(req, res, options, tokens) {
    receiver_id= options.email.receiver_id;
    message= options.email.message;
    sender_email = options.email.sender_email;
    sender_name = options.email.sender_name;
    slug=options.info.slug;
    org_name=options.info.org_name;
    dir_url=options.info.dir_url;    

    var https = require('https');
    str = '';
    //getting user email by id    

    path = '/api/v1/people/' + receiver_id +'?__proto__=&access_token=' + tokens[slug];
    var options = {
         host: slug + '.nationbuilder.com',
         path: path,
         method: "GET",
         json: true,
         headers: {
             "content-type": "application/json",
             "accept": "application/json"
         },
    }    

    var nb_req = https.get(options, req_callback);    

    function req_callback(response, res) {    

        response.on('data', function(chunk) {
            str += chunk;
        });    

        response.on('end', function() {
            object = JSON.parse(str);
            receiver_email = object.person.email1;
            var nodemailer = require('nodemailer');    

            var transporter = nodemailer.createTransport({
                    port:465,
                    host:"smtp.gmail.com",
                    auth: {
                        user: 'connect@alumninations.com', // Your email id
                        pass: '69$6FK$b6PQu' // Your password
                    }
                });    

                var html = '<p>From: ' + sender_email + '<br>';
                html += 'To: ' + receiver_email + '</p>';
                html += "<p>"+message + "</p>"
                html += "<p>This message was delivered by the " + org_name + " alumni directory website, " + "<a href='" + dir_url
                + "'>" + dir_url + "</p>";



                var mailOptions = {
                    from: 'connect@alumninations.com', // sender address
                    to: 'petervankoughnett@gmail.com', // list of receivers
                    subject: "A message from " + sender_name, // Subject line
                    /*text: "Message sent to", // plaintext body*/
                    html: html 
                };    

                transporter.sendMail(mailOptions, function(error, info){
                    if(error){
                        console.log(error);
                        res.json({yo: 'error'});
                    }else{
                        console.log('Message sent: ' + info.response);
                        res.sendStatus(200);
                    };
                    return res.sendStatus(200);    

                });    


        });    

    }    

}
about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You are missing res.end() after res.sendStatus(200);.

transporter.sendMail(mailOptions, function(error, info){
  if (error){
     console.log(error);
     res.json({yo: 'error'});
     res.sendStatus(500);
  }else{
     console.log('Message sent: ' + info.response);
     res.sendStatus(200);
  };
  return res.end();
}) ;
about 4 years ago · Santiago Trujillo Relatório

0

use res.status(200).json() instead:

  transporter.sendMail(mailOptions, function(error, info){
 if (error){
 console.log(error);
 res.json({yo: 'error'});
 res.sendStatus(500);
 }else{
 console.log('Message sent: ' + info.response);
 res.status(200).json({"msg": "mesage  has been sent"})
 };});
about 4 years ago · Santiago Trujillo Relatório

0

This is the asynchronous problem of JavaScript. I also have the same problem. When you call the email function outside it will not return anything so you will get 'undefined', because you returned a response within a callback function.

 router.post('/"your api path"',async(req,res)=>{
 //remaining codes
         transporter.sendMail(mailOptions, function(error, info){
                if(error){
                    console.log(error);
                    res.status(400).json({yo: 'error'});
                }else{
                    console.log('Message sent: ' + info.response);
                    res.sendStatus(200);
                };
                return res.sendStatus(200);    
        });    
}
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda