[英]NodeJs module exports and functions
I'm setting up nodemailer and trying to use create a model, controller,and mailer. 我正在设置nodemailer并尝试使用创建模型,控制器和邮件程序。 I know I have my functions messed up but I don't understand how to send the mailModel through the transport.sendmail function. 我知道我的函数搞砸了,但是我不明白如何通过transport.sendmail函数发送mailModel。 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. 我认为我在解释目标方面做得很差,我可以让Nodemailer在一个分配了mailOptions的脚本中工作。 but I want to export a function so I can just say sendMail(userEmail,subject, text); 但是我想导出一个函数,所以我只能说sendMail(userEmail,subject,text); It doesn't have to be through mongoose or mongoDB. 不必通过mongoose或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. 另一种选择是将数据保存在浏览器的缓存中,以便客户端将保存并恢复上一个用户的输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.