簡體   English   中英

如何使nodemailer正常工作?

[英]How do I get nodemailer to work?

我無法弄清楚如何使nodemailer正常工作,我也插入了我的gmail憑據,並嘗試向自己發送電子郵件,但沒有給我發送任何郵件,也沒有出錯,所以我很困惑。 我的代碼中可能缺少某些內容...

這是電子郵件文件:

var nodemailer = require('nodemailer');
var config = require('./config/config');

var smtpTransport = nodemailer.createTransport("SMTP",{
    service: "Gmail",
    auth: {
        user: config.mailer.auth.user,
        pass: config.mailer.auth.pass
    }
});

var EmailAddressRequiredError = new Error('email address required');


exports.sendOne = function(templateName, locals, fn) {
    if(!locals.email) {
        return fn(EmailAddressRequiredError);
    }

    if(!locals.subject) {
        return fn(EmailAddressRequiredError);
    }

    // template
    var transport = smtpTransport;
    transport.sendMail({
        from: config.mailer.defaultFromAddress,
        to: locals.email,
        subject: locals.subject,
        html: html,
        text: text
    }, function (err, responseStatus) {
        if(err) {
            return fn(err);
        } 
        return fn(null, responseStatus.message, html, text);
    });

};

這是發送電子郵件的路由文件:

exports.forgotPasswordPost = function(req, res, next) {
    console.log("Forgot Password Post");
    if(req.body.email === '') {
        console.log('err');
    } else {
    crypto.randomBytes(48, function(ex, buf) {
        var userToken = buf.toString('hex');
        console.log(userToken);
        User.findOne({email: (req.body.email)}, function(err, usr) {
            if(err || !usr) {
                res.send('That email does not exist.');             
            } else {
                console.log(usr);
                //just call the usr found and set one of the fields to what you want it to be then save it and it will update accordingly
                usr.token = userToken;
                usr.tokenCreated = new Date ();
                usr.save(function(err, usr){
                //  res.redirect('login', {title: 'Weblio', message: 'Your token was sent by email. Please enter it on the form below.'});
                    console.log(usr);
                });

                console.log(usr);
                var resetUrl = req.protocol + '://' + req.host + '/password_reset/' + usr.token;
                console.log(resetUrl);
                var locals = {
                resetUrl: resetUrl,
                };
                console.log(locals);
                mailer.sendOne('password_reset', locals, function(err, email) {
                    console.log('email sent');
                    res.redirect('successPost');
                });
            }




        });
    });
    }
};

除了這里的東西,我還需要其他東西嗎?

我沒有正確識別當地人。

這是對我有用的代碼:

var nodemailer = require('nodemailer');
var config = require('./config/config');

var smtpTransport = nodemailer.createTransport("SMTP",{
   // host: "smtp.gmail.com",
  //  secureConnection: true,
   // port: 465,
    service: "Gmail",
    //debug : true,
    auth: {
        user: config.mailer.auth.user,
        pass: config.mailer.auth.pass
    }


});

var EmailAddressRequiredError = new Error('email address required');


exports.sendOne = function(template, locals, err) {
    var message = {
        from: config.mailer.defaultFromAddress,
        to: locals.email,
        subject: locals.subject,
        html: locals.html,
        text: locals.text
    };
    console.log('hitlocal email');
    console.log(message);
    //console.log(message.to.locals.email);
    if(!locals.email) {
    //    console.log('email err');
    }

    if(!locals.subject) {
        console.log('subj err');
    }

    // template
    var transport = smtpTransport;
   // console.log('hit here');
   // console.log(transport);
    transport.sendMail(message, function(err) {
        if(err) {
            console.log('email js error');
            console.log(err);
        }
        console.log('Message sent')

       //return fn(null, responseStatus.message, html, text);
    });

};

這是路由文件:

var locals = {
                    email: 'first last <' + req.body.email + '>',
                    subject: 'Password Reset',
                    html: '<p> Please go to the following link to reset your password.' + resetUrl + '</p>',
                    text: 'Texty Text it'
                };  
                console.log('locals spot here');
                console.log(locals.email);
                mailer.sendOne('forgotPassword', locals, function(err) {

                    console.log('email sent');

                });

我認為您需要指向Gmail服務器而不是Gmail。 我忘了它到底是什么,但是像gmail.smtp一樣

暫無
暫無

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

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