NodeMailer No Recipients Defined

Samuel Pearsall picture Samuel Pearsall · Aug 10, 2016 · Viewed 20.7k times · Source

When trying to send an email from a node.js server using nodemailer, I get the following error:

ERROR: Send Error: No recipients defined

My code is as follows:

client side:

var mailData = {
        from: "[email protected]",
        to: "[email protected]",
        subject: "test",
        text: "test email"
    };

  $.ajax({
        type: 'POST',
        data: mailData,
        contentType: 'application/json',
        url: 'http://localhost:8080/sendEmail',
        success: function(mailData) {
            console.log('successfully posted to server.');
        }
    });

server side:

var nodemailer = require('nodemailer');

app.post('/sendEmail', function(mailData) {

   var transporter = nodemailer.createTransport({
       service: 'gmail',
       auth: {
           user: '[email protected]',
           pass: 'mypassword'
       },
   });

   transporter.sendMail(mailData, function(error, info) {
       if (error) {
           console.log(error);
           return;
       }
       console.log('Message sent');
       transporter.close();
   });

});

This is in line with the nodemailer docs, and the SMTP handshake always finishes successfully, so I know that this is not an issue with the transporter object.

Answer

kkochanski picture kkochanski · Aug 11, 2016

This kind of error occurs when value assigned to key to in object given to sendMail method is empty. As we see it means that something wrong happens and you don't get the same data on the server-side, as you sent in client.

Also I recommend to check if you are accessing good variable on your server-side action. Probably POST params are accessible in different way - please check this in your framework documentation.

For example in HapiJS I retrieve POST params in that way:

exports.someAction = function (request, reply) {
    var postParams = request.payload
}