我正在设置nodemailer并尝试使用创建模型,控制器和邮件程序。我知道我的功能搞砸了,但我不明白如何通过transport.sendmail函数发送mailModel。我的最终目标是能够致电邮寄者发送电子邮件。也许我甚至不需要猫鼬?
我认为我的工作很难解释我的目标,我可以让Nodemailer在一个配有mailOptions的脚本中工作。但我想导出一个函数,所以我只能说sendMail(userEmail,subject,text);它不必通过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);
答案 0 :(得分:0)
您可以修改语法和定义,如下所示,适用于您
//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;
答案 1 :(得分:0)
我将解决你是否需要数据库的问题。 如果您需要保存用户的输入以便在将来恢复同一特定用户的状态 - 那么是的,您需要一个数据库。 但是,如果它是一个仅发送电子邮件的应用程序 - 并且不依赖于您所在州的任何其他内容 - 而不是您不需要数据库。
另一种选择是将数据保存在浏览器的缓存中,以便客户端保存&amp;恢复最后一个用户的输入。