0

I'm setting up nodemailer and trying to use create a model, controller,and mailer. I know I have my functions messed up but I don't understand how to send the mailModel through the transport.sendmail function. My end goal is to be able to call mailer to send an email. Maybe I don't even need Mongoose ?

I think I did a poor job explaining my goal, I can get Nodemailer to work in one script with assigned mailOptions. but I want to export a function so I can just say sendMail(userEmail,subject, text); It doesn't have to be through mongoose or mongoDB.

//model.js
var mongoose = require('mongoose');
var mailSchema = mongoose.Schema;

var newMailSchema = new mailSchema( {
from: '"me" <[email protected]>', // sender address
to: '', // list of receivers
subject: '', // Subject line
text: '', // plain text body
html: '<b></b>' // html body
});

module.exports = mongoose.model(newMailSchema);

 //controller.js
'use strict';
const nodemailer = require('nodemailer');

// create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport({
host: 'smtp-mail.outlook.com',
port: 587,
secure: false, // secure:true for port 465, secure:false for port 587
auth: {
    user: '[email protected]',
    pass: 'password'
}
});


// send mail with defined transport object
var sender = function(){
transporter.sendMail(mailModel, (error, info) => {
if (error) {
    return console.log(error);
}
console.log('Message %s sent: %s', info.messageId, info.response);
});

};
exports.sender = sender;


//mailer.js
var sendMail = require('./controller');
var newMailModel = require('./model');

var mailModel = new newMailModel({
from: '"me" <[email protected]>', // sender address
to: '[email protected]', // list of receivers
subject: 'Hi', // Subject line
text: 'Foo', // plain text body
html: '<b>Bar</b>' // html body
});
sendMail.sender(mailModel);
4
  • So what is your goal ? Get recipient from MongoDB and then send email to that person ?
    – Janne
    Jun 17, 2017 at 4:35
  • Yes, a user inputs their information and I save it into mongoDB, then i want to be able to get their info and send them an email. So that I can just do sendMail(to, subject,text)
    – rudster
    Jun 17, 2017 at 11:58
  • The user that you mentioned - Is it a registered user - or a guest that using your application?
    – chenop
    Jun 17, 2017 at 13:29
  • registered user, I already have their email saved so I have no problem getting that information. I want to be able to call something like this sendMail(userEmail,subject,text); and it'll email them.
    – rudster
    Jun 17, 2017 at 13:34

2 Answers 2

0

You Correct Your Syntax and Definition as below and will work for u

//model.js
var mongoose = require('mongoose');
var mailSchema = mongoose.Schema;

var newMailSchema = new mailSchema( {
    from: {type:String,default:'[email protected]'},
    to: String,
    subject: String,
    text: String,
    html: String
});

module.exports = mongoose.model('MailSchema',newMailSchema);

//controller.js

var newMailModel = require('./model');
const nodemailer = require('nodemailer');
exports.SendMail = function(req,res){
    var transporter = nodemailer.createTransport({
        host: 'smtp-mail.outlook.com',
        port: 587,
        secure: false, // secure:true for port 465, secure:false for port 587
        auth: {
            user: '[email protected]',
            pass: 'password'
        }
    });
    var mailOptions = {
        from: 'me@gmail,com', // sender address
        to: '[email protected]', // list of receivers
        subject: 'Hi', // Subject line
        text: 'Foo', // plaintext body
        html:'<b>Bar</b>'
    };
    transporter.sendMail(mailOptions, (error, info) => {
        if (error) {
            return console.log(error);
        }else {
            console.log('Message %s sent: %s', info.messageId, info.response);
            var mailModel = new newMailModel({
                from: mailOptions.from,
                to: mailOptions.to,
                subject: mailOptions.subject,
                text: mailOptions.text,
                html: mailOptions.html,
            });
            mailModel.save();
            res.send('Mail Sent Successfully');
        }
    });
}

//router.js

var express = require('express'),
    router = express.Router(),
    controller = require('./controller.js');

router.post('/MailExample',controller.SendMail);
module.exports = router;
3
  • Ratan, this is good information but I don't want to post, I just want be able to call sendMail function to do something like sendMail(to, subject,text)
    – rudster
    Jun 17, 2017 at 12:18
  • you can use same way without routing Jun 17, 2017 at 12:44
  • thats what i'm having trouble doing, would I even need mongoose in that case?
    – rudster
    Jun 17, 2017 at 13:16
0

I'll address the question whether you need a db. If you need to save the user's inputs in order to restore a state for the same particular user in the future - so yes you need a db. But if it is an application that just send an email - and does not depend on anything else in your state - than you do not need a db.

Another option is to save data in the browser's cache so the client side will save & restore the last user's input.

1
  • Then I don't need a DB,I want to export the function of nodemailer so I can just say sendMail(userEmail,subject,text); and it'll email them
    – rudster
    Jun 17, 2017 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.