簡體   English   中英

NodeJs模塊導出和功能

[英]NodeJs module exports and functions

我正在設置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);

您可以按以下方式更正語法和定義,並且對您有用

//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;

我將解決您是否需要數據庫的問題。 如果您需要保存用戶的輸入以便將來恢復同一特定用戶的狀態,那么是的,您需要一個數據庫。 但是,如果它是僅發送電子郵件的應用程序-並且不依賴您所在狀態的其他任何內容-則不需要數據庫。

另一種選擇是將數據保存在瀏覽器的緩存中,以便客戶端將保存並恢復上一個用戶的輸入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM