2017-04-26 109 views
2

我一直試圖在我的MEAN應用程序上設置和使用nodemailer。 這裏是mail.js ...我用於我的server.js文件的路線。錯誤:嘗試使用nodemailer時未定義收件人,但收件人列出

'use strict'; 
const express = require('express'); 
const router = express.Router(); 
const nodemailer = require('nodemailer'); 
const config = require('./config'); 
const Message = require('../models/message'); 

var transporter = nodemailer.createTransport({ 
    service: 'gmail', 
    secure: false, 
    port: 25, 
    auth: { 
    user: config.mailUser, //same as from in mailOptions 
    pass: config.mailPass 
    }, 
    tls: { 
    rejectUnauthorized: false 
    } 
}); 

router.post('/contact', function(req, res){ 
    var mailOptions = new Message({ 
    from: '[email protected]', 
    to: req.body.to, 
    subject: req.body.subject, 
    text: req.body.text 
    //html: req.body.html 
    }); 

    transporter.sendMail(mailOptions, function(error, info){ 
    if(error){ 
     return console.log(error); 
    } 
    return console.log('Message %s sent: %s', info.messageId, info.response); 
    }); 
}); 

module.exports = router; 

和我的config.js文件看起來像這樣。

module.exports = { 
    mailUser: '[email protected]', 
    mailPass: 'XXXXXXXXX' 
}; 

我正在使用郵遞員對後端進行API調用,但結果是標題中聲明的錯誤。有人知道爲什麼看來收件人是定義的。

***更新

這裏是我的快遞應用

const express = require('express'); 
const cookieParser = require('cookie-parser'); 
const bodyParser = require("body-parser"); 
const mongoose = require('mongoose'); 

const appRoutes = require('./routes/app'); 
const keyRoutes = require('./routes/keys'); 
const mailRoutes = require('./routes/mail'); 

const app = express(); 
const uristring = 
    process.env.MONGOLAB_URI || 
    process.env.MONGOHQ_URL || 
    'mongodb://localhost/db'; 


mongoose.connect(uristring, function (err, res) { 
    if (err) { 
    console.log ('ERROR connecting to: ' + uristring + '. ' + err); 
    } else { 
    console.log ('Succeeded connected to: ' + uristring); 
    } 
    }); 

app.use(express.static(__dirname + '/dist')); 
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({extended: false})); 
app.use(cookieParser()); 

app.use(function (req,res,next) { 
    res.header('Access-Control-Allow-Origin', '*'); 
    res.header('Access-Control-Allow-Headers','Origin, X-Requested-With, Content-Type, Accept'); 
    res.header('Access-Control-Allow-Methods', 'POST, GET, PATCH, DELETE, OPTIONS'); 
    next(); 

}); 

app.use('/mail', mailRoutes); 
app.use('/keys', keyRoutes); 
app.use('/', appRoutes); 

//catch 404 and forward error handler 
app.use(function (req, res, next) { 
    return res.json('src/index'); 
}); 

app.listen(process.env.PORT || 8080); 

module.exports = app; 

,這裏是我送 enter image description here

enter image description here

***更新

請求

這裏是米y消息類。

const mongoose = require('mongoose'); 
const Schema = mongoose.Schema; 

const schema = new Schema({ 
    from: {type: String, required: true}, 
    to: {type: String, required: true}, 
    subject: {type: String, required: true}, 
    text: {type: String, required: true}, 
    html: {type: String, required: false} 
}); 

module.exports = mongoose.model('Message', schema); 
+0

顯示您正在執行的請求,並請快速設置。 –

+0

我編輯了我的答案,其中包含我的快遞應用程序和郵遞員請求 –

+0

檢查我編輯的答案,檢查是否在郵遞員請求中設置了「Content-Type:application/json」。 –

回答

1

問題在於貓鼬綱要。我是貓鼬的專家,其實從來沒有使用過它在我的生活,但在調試代碼時,我發現你爲什麼總有麻煩搞清楚了這一點:

打印時使用console.log

let mailOptions = new Message({ 
     from: '[email protected]', 
     to: req.body.to, 
     subject: req.body.subject, 
     text: req.body.text 
    //html: req.body.html 
}); 

它這個模式輸出如下:

{ from: '[email protected]', 
    to: '[email protected]', 
    subject: 'My subject', 
    text: 'Email body', 
    _id: 590135b96e08e624a3bd30d2 } 

這似乎是一個普通的對象。實際上(不包括_id部分)它輸出的結果如下:

let mailOptions = { 
    from: '[email protected]', 
    to: req.body.to, 
    subject: req.body.subject, 
    text: req.body.text 
    //html: req.body.html 
}; 

但後者在將它傳遞給nodemailer時起作用。

所以我試圖找出的mailOptions的真實身份(像我JSON伯恩或東西)

使用:

console.log(Object.assign({}, mailOptions)); 

我得到以下,這當然沒有按」對nodemailer看起來不錯。

{ '$__': 
    InternalCache { 
    strictMode: true, 
    selected: undefined, 
    shardval: undefined, 
    saveError: undefined, 
    validationError: undefined, 
    adhocPaths: undefined, 
    removing: undefined, 
    inserting: undefined, 
    version: undefined, 
    getters: {}, 
    _id: undefined, 
    populate: undefined, 
    populated: undefined, 
    wasPopulated: false, 
    scope: undefined, 
    activePaths: StateMachine { paths: [Object], states: [Object], stateNames: [Object] }, 
    ownerDocument: undefined, 
    fullPath: undefined, 
    emitter: EventEmitter { domain: null, _events: {}, _eventsCount: 0, _maxListeners: 0 } }, 
    isNew: true, 
    errors: undefined, 
    _doc: 
    { _id: 590137d8f8c7152645180e04, 
    text: 'Email body', 
    subject: 'My subject', 
    to: '[email protected]', 
    from: '[email protected]' } } 

通過貓鼬文檔閱讀中,我發現,將其轉換成一個簡單的JavaScript對象,它與nodemailer正常工作的方法。這方法是:

toObject

所以總結起來,你有兩個選擇:

1)transporter.sendMail(mailOptions.toObject() //...,如果你想用貓鼬架構(我真的不知道爲什麼,但。 ..)

2)下降貓鼬模式,只需使用:(這是我推薦的方法,因爲貓鼬無關與nodemailer)

let mailOptions = { 
    from: '[email protected]', 
    to: req.body.to, 
    subject: req.body.subject, 
    text: req.body.text 
    //html: req.body.html 
}; 

測試了,而且我發送電子郵件沒有問題。

+0

謝謝你的迴應。我已經將頭文件設置爲application/json。檢查我的編輯郵件的屏幕截圖 –

+0

@JonathanCorrin你是否得到'req.body.to'的任何值我複製了你的代碼(只有express和router部分)並且工作正常。 –

+0

向我們展示您的消息類,看起來您在那裏遇到了問題。 –

相關問題