无法在nodejs中发送电子邮件

时间:2016-12-02 05:38:19

标签: javascript node.js nodemailer

我正在使用nodemail npm包。

我配置了这样的选项:

function feedback(req, res, next){
    console.log('feed back given....', req.body);
    smtpTrans = nodemailer.createTransport('SMTP', {
        service: 'Gmail',
        host: 'smtp.gmail.com',
        port: 587,
        secure: false,
        ignoreTLS: false,
        tls: { rejectUnauthorized: true },
        debug: false,
        auth: {
            user: "xxxxxx@gmail.com",
            pass: "xxxxxx" 
        }
    });

    //Mail options
    mailOpts = {
        from: from: req.body.email,
        to: 'xxxxxx@gmail.com',
        subject: 'EMAIL FROM Rsc-student: ' + req.body.subject,
        text: req.body.message
    };

    smtpTrans.sendMail(mailOpts, function (error, response) {
      //Email not sent
      if (error) {
          res.send(error);
          console.log('error sending mail');
      }
      else {
          res.send(response);
          console.log('success sending mail');
      }
    });
}

如果我没错,我配置正确但仍无法发送邮件。它打印错误案例

2 个答案:

答案 0 :(得分:1)

from:删除额外mailOpts

答案 1 :(得分:1)

按照以下3个步骤操作:

  1. 登录您的Gmail帐户。

  2. 请点击此链接allow gmail to send mail over less secure app

  3. 选择选项选项。
  4. 它对我有用。我希望它会对你有所帮助。