因此,我正在尝试使用Nodemailer模块通过nodejs发送邮件。当我使用此代码时,它确实起作用:
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
type: 'OAuth2',
user:"my_emailadres_",
clientId:"my_client_id",
clientSecret:"my_client_secret",
refreshToken:"refresh_token",
accessToken: "my_access_token",
},
});
var mailOpties = {
from: "my_email",
to: "another_email",
subject: "Hello",
generateTextFromHTML: true,
html: "<b>Hello world!</b>"
};
transporter.sendMail(mailOpties, function(error) {
if (error) {
console.log(error);
} else {
console.log('Email is verzonden');
}
transporter.close();
});
但是现在当我在联系页面上填写表格时,我试图发送电子邮件,但是它没有发送电子邮件,并且出现错误。
这是我使用nodemailer路由的新POST方法:(FYS / javascript / mail.js):
const express = require('express');
const app = express();
//gebruik body-parser om data van de contact form op te nemen
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));
const nodemailer = require('nodemailer');
app.post('/Contact', function (req, res) {
let mailOpts, transporter;
transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
type: 'OAuth2',
user: "my_email",
clientId: "my_client_id",
clientSecret: "my_client_secret",
refreshToken: "refresh_token",
accessToken: "acess_token",
},
});
mailOpts = {
from: req.body.name + ' <' + req.body.email + '>',
to: "my_email_adress",
subject: 'Nieuw bericht van Corendon klant',
text: `${req.body.name} (${req.body.email}) says: ${req.body.message}`
};
transporter.sendMail(mailOpts, function(error) {
if (error) {
res.render('contact-failure')
} else {
console.log('contact-success');
}
transporter.close();
});
});
这是我在html路由中使用的表单:(FYS / html / Contact.html)
<form action="/Contact.html" id="contact-form" method="post" role="form">
<fieldset>
<label for="name">Name *</label>
<input id="name" name="name" type="text" placeholder="Your name" required="required">
<label for="email">Email *</label>
<input id="email" name="email" type="text" placeholder="Your email" required="required">
<label for="message">Message *</label>
<textarea id="message" name="message" placeholder="Enter your message here" rows="3" required="required"></textarea>
<button type="submit">Submit</button>
</fieldset>
</form>
因此,当我提交表单时,没有电子邮件正在发送,并且出现错误:找不到404。 我认为html表单中的表单操作未很好地链接?:
<form action="/Contact.html"
在mail.js app.post中应该怎么做?
app.post('/Contact', function (req, res) {
还是我做错了什么? 任何帮助都值得赞赏 (我是使用Nodejs和javascript的新手)