简体   繁体   中英

NodeJs module exports and functions

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" <me@gmail.com>', // 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: 'me@hotmail.com',
    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" <me@hotmail.com>', // sender address
to: 'you@gmail.com', // list of receivers
subject: 'Hi', // Subject line
text: 'Foo', // plain text body
html: '<b>Bar</b>' // html body
});
sendMail.sender(mailModel);

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:'me@gmail.com'},
    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: 'me@hotmail.com',
            pass: 'password'
        }
    });
    var mailOptions = {
        from: 'me@gmail,com', // sender address
        to: 'you@gmail.com', // 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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM