gpt4 book ai didi

javascript - 如何解决 Nodemailer 设置问题

转载 作者:太空宇宙 更新时间:2023-11-04 01:18:48 24 4
gpt4 key购买 nike

我正在尝试编写将表单数据发送到电子邮件的脚本。我尝试将 Express 和 Nodemailer 与 Node js 一起使用。我只是初学者,不明白为什么它会停止邮件授权并发送信件

我的代码:

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;

当我发送数据时,我将其存储在终端中,但是当我想将其发送到邮件时,我收到错误:

{ 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' }```

最佳答案

下面的代码在我这边工作,我将其用于我的项目。

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
- OS : Windows 10
- nodemailer : 1.11.0

<小时/>

请检查您的环境,过去我遇到过您的问题,但是,我更改了 Node 版本,所以我解决了。

关于javascript - 如何解决 Nodemailer 设置问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60012094/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com