25

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: "myemail@gmail.com",
        to: "someoneelse@something.com",
        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: 'myemail@gmail.com',
           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.

Samuel Pearsall
  • 445
  • 1
  • 5
  • 11
  • 1
    Did you check how `mailData` var look like on server side just before running `sendMail` method? Is it the same as on client side? Everything looks ok and this kind of error is thrown when `to` field is empty. – kkochanski Aug 10 '16 at 19:40
  • @kkochanski you're right. there is something wrong with the ajax on client side, as when I do console.log(mailData.to) on the server I get undefined, so the issue is actually with my ajax call and not nodemailer itself. – Samuel Pearsall Aug 11 '16 at 08:50
  • I answered, hope that helps! – kkochanski Aug 11 '16 at 17:25

1 Answers1

32

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
}
kkochanski
  • 2,178
  • 23
  • 28