Jonathan Corrin
Jonathan Corrin

Reputation: 1279

Error: No recipients defined when trying to use nodemailer but recipient listed

I've been trying to setup and use nodemailer on my MEAN app. Here is mail.js ... a route I'm using for my server.js file.

'use strict';
const express = require('express');
const router = express.Router();
const nodemailer = require('nodemailer');
const config = require('./config');
const Message = require('../models/message');

var transporter = nodemailer.createTransport({
  service: 'gmail',
  secure: false,
  port: 25,
  auth: {
    user: config.mailUser, //same as from in mailOptions
    pass: config.mailPass
  },
  tls: {
    rejectUnauthorized: false
  }
});

router.post('/contact', function(req, res){
  var mailOptions = new Message({
    from: '[email protected]',
    to: req.body.to,
    subject: req.body.subject,
    text: req.body.text
    //html: req.body.html
  });

  transporter.sendMail(mailOptions, function(error, info){
    if(error){
      return console.log(error);
    }
    return console.log('Message %s sent: %s', info.messageId, info.response);
  });
});

module.exports = router;

and my config.js file looks like this.

module.exports = {
  mailUser: '[email protected]',
  mailPass: 'XXXXXXXXX'
};

I'm using postman to make API calls to the backend but the result is the error stated in the headline. Anyone know why? It seems that recipient is defined.

***Update

Here is my express app

const express = require('express');
const cookieParser = require('cookie-parser');
const bodyParser = require("body-parser");
const mongoose = require('mongoose');

const appRoutes = require('./routes/app');
const keyRoutes = require('./routes/keys');
const mailRoutes = require('./routes/mail');

const app = express();
const uristring =
  process.env.MONGOLAB_URI ||
  process.env.MONGOHQ_URL ||
  'mongodb://localhost/db';


mongoose.connect(uristring, function (err, res) {
  if (err) {
    console.log ('ERROR connecting to: ' + uristring + '. ' + err);
  } else {
    console.log ('Succeeded connected to: ' + uristring);
  }
  });

app.use(express.static(__dirname + '/dist'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());

app.use(function (req,res,next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers','Origin, X-Requested-With, Content-Type, Accept');
  res.header('Access-Control-Allow-Methods', 'POST, GET, PATCH, DELETE, OPTIONS');
  next();

});

app.use('/mail', mailRoutes);
app.use('/keys', keyRoutes);
app.use('/', appRoutes);

//catch 404 and forward error handler
app.use(function (req, res, next) {
  return res.json('src/index');
});

app.listen(process.env.PORT || 8080);

module.exports = app;

and here is the request I'm sending enter image description here

enter image description here

***Update

Here is my message class.

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const schema = new Schema({
  from: {type: String, required: true},
  to: {type: String, required: true},
  subject: {type: String, required: true},
  text: {type: String, required: true},
  html: {type: String, required: false}
});

module.exports = mongoose.model('Message', schema);

Upvotes: 2

Views: 8909

Answers (1)

Marcos Casagrande
Marcos Casagrande

Reputation: 40374

The problem lies in the mongoose Schema. I'm no expert in mongoose, in fact never used it in my life but when debugging your code I found why you had troubles figuring this out:

When you print this schema using console.log

let mailOptions = new Message({
        from: '[email protected]',
        to: req.body.to,
        subject: req.body.subject,
        text: req.body.text
    //html: req.body.html
 });

it outputs the following:

{ from: '[email protected]',
  to: '[email protected]',
  subject: 'My subject',
  text: 'Email body',
  _id: 590135b96e08e624a3bd30d2 }

Which seems an ordinary object. In fact (excluding the _id part) it output the same as:

let mailOptions = {
    from: '[email protected]',
    to: req.body.to,
    subject: req.body.subject,
    text: req.body.text
    //html: req.body.html
};

But the the latter works when passing it to nodemailer.

So I tried to figure out the real identity of mailOptions (Like I'm JSON Bourne or something)

Using:

console.log(Object.assign({}, mailOptions));

I get the following, which of course doesn't look good to nodemailer.

{ '$__': 
   InternalCache {
     strictMode: true,
     selected: undefined,
     shardval: undefined,
     saveError: undefined,
     validationError: undefined,
     adhocPaths: undefined,
     removing: undefined,
     inserting: undefined,
     version: undefined,
     getters: {},
     _id: undefined,
     populate: undefined,
     populated: undefined,
     wasPopulated: false,
     scope: undefined,
     activePaths: StateMachine { paths: [Object], states: [Object], stateNames: [Object] },
     ownerDocument: undefined,
     fullPath: undefined,
     emitter: EventEmitter { domain: null, _events: {}, _eventsCount: 0, _maxListeners: 0 } },
  isNew: true,
  errors: undefined,
  _doc: 
   { _id: 590137d8f8c7152645180e04,
     text: 'Email body',
     subject: 'My subject',
     to: '[email protected]',
     from: '[email protected]' } }

Reading through the mongoose docs I found a method to convert that to a simple javascript object which works fine with nodemailer. That method is:

toObject

So summing up, you have two alternatives:

1) transporter.sendMail(mailOptions.toObject() //... if you want to use the mongoose schema (I really don't know why, but...)

2) Drop the mongoose schema and just use: (This is my recommended approach, since mongoose has nothing to do with nodemailer)

let mailOptions = {
    from: '[email protected]',
    to: req.body.to,
    subject: req.body.subject,
    text: req.body.text
    //html: req.body.html
 };

Tested both, and I'm sending the email with no problem.

Upvotes: 2

Related Questions