简体   繁体   English

如何解决 Nodemailer 设置的问题

[英]How to fix troubles with Nodemailer settings

I'm trying to write script which send form data to email.我正在尝试编写将表单数据发送到电子邮件的脚本。 I try to use Express and Nodemailer with node js.我尝试将 Express 和 Nodemailer 与 node js 一起使用。 I'm only beginner and can't understand why it stop the mail autorization and send letters我只是初学者,不明白为什么它会停止邮件自动化并发送信件

My code :我的代码:

const express = require('express');
const bodyParser = require('body-parser');
const mailer = require('./nodemailer');

const app = express();

const PORT = 3001;
let user;

app.use(bodyParser.urlencoded({ extended: false }));
app.post('/index', (req, res) => {
  if (!req.body.name || !req.body.phone) return res.sendStatus(400);
  const message = {
    to: '*****2020@mail.ru',
    subject: 'Congratulations! You are successfully registred on our site',
    text: `<h2>Поздравляем, Вы успешно зарегистрировались на нашем сайте! Данное письмо не требует ответа.<h2>
    <ul>
            <li>login: ${req.body.name}</li>
            <li>password: ${req.body.phone}</li>
        </ul> `,
  };
  mailer(message);
  user = req.body;
  console.log(req.body);
  res.redirect('/index');
});

app.get('/index', (req, res) => {
  if (typeof user !== 'object') return res.sendFile(`${__dirname}/dist/index.html`);
  res.send(`Заявка принята!`);
  user = undefined;
});

app.listen(PORT, () => console.log(`server listening at http://localhost:${PORT}/index`));
const nodemailer = require('nodemailer');

const transporter = nodemailer.createTransport(
  {
    host: 'smtp.gmail.com',
    port: 587,
    secure: false, // true for 465, false for other ports
    auth: {

      user: '******', // 
      pass: '****', //
    },
  },
  {
    from: 'Mailer Test <****2020@mail.ru>',
  },
);

const mailer = message => {
  transporter.sendMail(message, (err, info) => {
    if (err) return console.log(err);
    console.log('Email sent: ', info);
  });
};

module.exports = mailer;

When i sent a data i have it in terminal, but when i want sent it to mail i get a error :当我发送数据时,我将其保存在终端中,但是当我想将其发送到邮件时,出现错误:

{ Error: self signed certificate in certificate chain
    at TLSSocket.onConnectSecure (_tls_wrap.js:1058:34)
    at TLSSocket.emit (events.js:198:13)
    at TLSSocket._finishInit (_tls_wrap.js:636:8) code: 'ESOCKET', command: 'CONN' }```

Below code is working on my side, and I used that for my project.下面的代码在我这边工作,我在我的项目中使用了它。

var nodemailer = require('nodemailer');

var smtpTransport = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 465,
    secure: true, // use SSL
    auth: {
        user: EMAIL,
        pass: PASSOWRD
    }
});

var mailOptions = {
    from: `Web`,
    to: 'toEmail@gmail.com',
    subject: 'Password reset request',
    html: 'How are you'
};
smtpTransport.sendMail(mailOptions, function (err, info) {
    if (!err) console.log(err)
    console.log('Successfully Sent');
});

Enviorenment(important):环境(重要):
- node version : v13.2.0 - 节点版本:v13.2.0
- OS : Windows 10 - 操作系统:Windows 10
- nodemailer : 1.11.0 - 节点邮件程序:1.11.0


Please check your envrironment, in the past, i faced your problem, but, I changed the node-version, so I solved.请检查您的环境,过去,我遇到了您的问题,但是,我更改了节点版本,所以我解决了。

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

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