gpt4 book ai didi

javascript - 如何通过nodemailer发送邮件

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

我正在尝试通过我的网站向我自己的其他帐户发送一封简单的邮件。但这不起作用。我正在使用 Node 邮件程序并严格遵循文档。

我尝试检查任何类型的语法错误或外部因素。

//Send Mail
router.post('/volunteer', ensureAuthenticated, async(req, res) => {

// const { name, email, city } = req.body;
//Create the transporter
require('dotenv').config();
const nodemailer = require('nodemailer');

let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL,
pass: process.env.PASSWORD
}
});

//set up mailOptions
let mailOptions = {
from: 'readsocially001@gmail.com',
to: 'vaibhav.ag.001@gmail.com',
subject: 'Volunteer',
text: 'HEY'
};

transporter.sendMail(mailOptions, function (err, data) {
if (err) {
console.log("some error");
} else {
console.log("SENT");
}

});
res.redirect('/volunteer');
});

它只是进入 if(err) 语句并打印一些错误。实际错误是:

Error: Missing credentials for "PLAIN"
at SMTPConnection._formatError (/Users/vaibhav2001/Documents/test/ReadSocially/node_modules/nodemailer/lib/smtp-connection/index.js:781:19)
at SMTPConnection.login (/Users/vaibhav2001/Documents/test/ReadSocially/node_modules/nodemailer/lib/smtp-connection/index.js:452:38)
at connection.connect (/Users/vaibhav2001/Documents/test/ReadSocially/node_modules/nodemailer/lib/smtp-transport/index.js:271:32)
at SMTPConnection.once (/Users/vaibhav2001/Documents/test/ReadSocially/node_modules/nodemailer/lib/smtp-connection/index.js:209:17)
at Object.onceWrapper (events.js:286:20)
at SMTPConnection.emit (events.js:198:13)
at SMTPConnection._actionEHLO (/Users/vaibhav2001/Documents/test/ReadSocially/node_modules/nodemailer/lib/smtp-connection/index.js:1309:14)
at SMTPConnection._processResponse (/Users/vaibhav2001/Documents/test/ReadSocially/node_modules/nodemailer/lib/smtp-connection/index.js:940:20)
at SMTPConnection._onData (/Users/vaibhav2001/Documents/test/ReadSocially/node_modules/nodemailer/lib/smtp-connection/index.js:746:14)
at TLSSocket.SMTPConnection._onSocketData (/Users/vaibhav2001/Documents/test/ReadSocially/node_modules/nodemailer/lib/smtp-connection/index.js:189:46)
at TLSSocket.emit (events.js:198:13)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11)
at TLSSocket.Readable.push (_stream_readable.js:224:10)
at TLSWrap.onStreamRead [as onread] (internal/stream_base_commons.js:94:17) code: 'EAUTH', command: 'API' }

最佳答案

根据官方文档,您不能使用普通凭据:https://nodemailer.com/usage/using-gmail/要将nodemailer与Gmail一起使用,您需要创建一个应用程序并使用oauth,如下所示:

    host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
type: 'OAuth2',
user: 'user@example.com',
accessToken: 'ya29.Xx_XX0xxxxx-xX0X0XxXXxXxXXXxX0x'
}
});

更多示例:https://nodemailer.com/smtp/oauth2/#examples

希望对你有帮助

关于javascript - 如何通过nodemailer发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57843542/

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