简体   繁体   English

使用nodemailer / smtp.gmail.com发送电子邮件时出错

[英]Error when sending email using nodemailer / smtp.gmail.com

I'm trying to set up nodemailer for an app. 我正在尝试为应用设置nodemailer。 I'm receiving an error when I try it. 尝试时出现错误。

This is my setup: 这是我的设置:

        email = setting.email;
        password = setting.password;

        var transporter = nodemailer.createTransport({
            host: 'smtp.gmail.com',
            port: 587,
            secure: false, // true for 465, false for other ports
            auth: {
                user: '***********@gmail.com', // real email
                pass: '*********' // real password
            }});

        // var transporter = nodemailer.createTransport({
        //     service: 'gmail',
        //     auth: {
        //         user: email, // Your email id
        //         pass: password // Your password
        //     }
        // });

        var mailOptions = {// sender address
            from: email,
            to: to, // list of receivers
            subject: sub, // Subject line
            text: text, //, /// plaintext body
            html: html

        }

        //console.log(JSON.stringify(mailOptions));

        transporter.sendMail(mailOptions, function (error, info) {
            if (error) {
                console.error(error);
            } else {

                console.log(info.response);
            }
            ;
        });
    });

} catch (error) {
    console.error(error);

}

This is the first time I've tried using nodemailer. 这是我第一次尝试使用nodemailer。 I am using real email and password. 我正在使用真实的电子邮件和密码。 The error are: 错误是:

(node:18974) [DEP0025] DeprecationWarning: sys is deprecated. (节点:18974)[DEP0025] DeprecationWarning:不建议使用sys。 Use util instead. 请改用util。 Magic happens on port 5000 ERROR! 魔术发生在端口5000 ERROR上! ERROR! 错误! ERROR! 错误! ERROR! 错误! provider_analytic_daily saved. provider_analytic_daily已保存。 { Error: Connection timeout at SMTPConnection._formatError (/root/faszz/node_modules/smtp-connection/lib/smtp-connection.js:528:15) at SMTPConnection._onError (/root/faszz/node_modules/smtp-connection/lib/smtp-connection.js:514:16) at SMTPConnection. {错误:SMTPConnection._onError(/ root / faszz / node_modules / smtp-connection / lib处的SMTPConnection._formatError(/root/faszz/node_modules/smtp-connection/lib/smtp-connection.js:528:15)处的连接超时/smtp-connection.js:514:16)。 (/root/faszz/node_modules/smtp-connection/lib/smtp-connection.js:236:14) at ontimeout (timers.js:498:11) at tryOnTimeout (timers.js:323:5) at Timer.listOnTimeout (timers.js:290:5) code: 'ETIMEDOUT', command: 'CONN' } (/root/faszz/node_modules/smtp-connection/lib/smtp-connection.js:236:14)在onTimeout(timers.js:498:11)在tryOnTimeout(timers.js:323:5)在Timer.listOnTimeout (timers.js:290:5)代码:“ ETIMEDOUT”,命令:“ CONN”}

functionObj = {};

    functionsObj.getSmtpTransport = function() {
        var smtpTransport = nodemailer.createTransport({
            service: "Gmail",
            auth: {
                user: "<your email address>",
                pass: "<your pass>"
            }
        });

        return smtpTransport;
    }

    functionsObj.sendMailForPasswordReset = function(to, token) {

        var smtpTransport = functionsObj.getSmtpTransport();

        var mailOptions = {
            to: to,
            subject: "Blabla.com Reset Password",
            html: "Hi,<br> Click to link for resetting password.<br><a href='https://<blabla.com>/reset/" + urlencode(token) + "'>Click Me !!!</a>"
        }

        smtpTransport.sendMail(mailOptions, function(error, response) {
            if (error) {
                return error;

            }
            else {
                return true;
            }
        });

    };

Hi, 嗨,

The code above worked in my project without any error. 上面的代码在我的项目中正常工作,没有任何错误。 But you should allow to third party softwares to use your google account from google. 但是您应该允许第三方软件使用您从Google来的Google帐户。

Here the google's support page for this case: 在此情况下,Google的支持页面如下:

https://support.google.com/accounts/answer/6010255?hl=en https://support.google.com/accounts/answer/6010255?hl=en

I recommend that all input should be validated :) For example if you are taking "to" parameter from user. 我建议应该验证所有输入:)例如,如果您要从用户获取“ to”参数。 You should validate "to" parameter is a valid email address ? 您应该验证“收件人”参数是否为有效的电子邮件地址?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM