简体   繁体   中英

Error when sending email using nodemailer / smtp.gmail.com

I'm trying to set up nodemailer for an app. 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. I am using real email and password. The error are:

(node:18974) [DEP0025] DeprecationWarning: sys is deprecated. Use util instead. Magic happens on port 5000 ERROR! ERROR! ERROR! ERROR! provider_analytic_daily saved. { 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. (/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' }

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.

Here the google's support page for this case:

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. You should validate "to" parameter is a valid email address ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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