无效的登录名:GMail / OAuth2 / Nodemailer

时间:2019-03-14 15:53:41

标签: node.js express oauth-2.0 gmail-api nodemailer

我的app.js文件现在出现以下错误,该错误曾经起作用,现在尝试从我的投资组合页面中的联系表单发送电子邮件时不起作用。如果有帮助,我正在使用dotenv隐藏我的密钥,并且已经引用了https://nodemailer.com/smtp/oauth2/。任何帮助将不胜感激!

   require('dotenv').config();

    let nodemailer = require('nodemailer'),
        smtpTransport = require('nodemailer-smtp-transport'),
        middlewareObj = {};

    middlewareObj.sendMail = function(req, res, next) {
        let mailOpts, transporter;
        transporter = nodemailer.createTransport(smtpTransport({
            host: 'smtp.gmail.com',
            port: 465,
            secure: true,
            auth: {
                type: 'OAuth2',
                user: process.env.USER,
                clientID: process.env.CLIENTID,
                clientSecret: process.env.CLIENTSECRET,
                refreshToken: process.env.REFRESHTOKEN,
            }
        }));
        mailOpts = {
            from: req.body.email,
            to: process.env.USER,
            subject: "New Message From My Portfolio Site",
            text: `${req.body.name} says: ${req.body.message} \n (${req.body.email})`
        };
        transporter.sendMail(mailOpts, function(error, response) {
            if (error) {
                console.log(error);
                req.flash("error", "Something went wrong. Please try again.");
                res.redirect("/#contact");
            }
            else {
                req.flash("success", "Message Sent!");
                res.redirect("/#contact");
            }
        });
    }

    module.exports = middlewareObj;

0 个答案:

没有答案