我的网站正在运行,我有一个用户表格,如果想要提出任何问题的用户或访问者可以通过填写该表格向我发送他的请求,当用户单击提交按钮时,所有数据都应该在我的 Gmail 电子邮件帐户中收到。我的表单有用户名、用户联系方式、用户电子邮件地址和文本框中用户想问我的问题的输入字段。请为我提供完整的使用 Node js Express JavaScript CSS 和 html 的分步代码。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Contact Form</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Contact Form</h1>
<form id="contact-form" action="/send-email" method="POST">
<label for="username">Name:</label>
<input type="text" id="username" name="username" required>
<label for="contact">Contact:</label>
<input type="text" id="contact" name="contact" required>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<label for="question">Question:</label>
<textarea id="question" name="question" required></textarea>
<button type="submit">Submit</button>
</form>
</body>
</html>
这是我的 Html 和服务器文件。我已经安装了nodemailer express和升级版本的node js。我有一个公共文件夹,其中 index.html 成功运行,但是当用户或我单击提交按钮发送我的请求时,我收到错误, 此页面无法正常工作如果问题仍然存在,请联系网站所有者。 HTTP 错误 405 这是我的index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Contact Form</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Contact Form</h1>
<form id="contact-form" action="/send-email" method="POST">
<label for="username">Name:</label>
<input type="text" id="username" name="username" required>
<label for="contact">Contact:</label>
<input type="text" id="contact" name="contact" required>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<label for="question">Question:</label>
<textarea id="question" name="question" required></textarea>
<button type="submit">Submit</button>
</form>
</body>
</html>
//这是我的 app.js 文件:
const express = require('express');
const bodyParser = require('body-parser');
const nodemailer = require('nodemailer');
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Serve static files from the 'public' directory
app.use(express.static('public'));
// Define a route for handling form submissions
app.post('/send-email', (req, res) => {
const { username, contact, email, question } = req.body;
// Create a nodemailer transporter using your email service provider
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'rtindiyqirtodnvt'
}
});
// Define email content
let mailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: 'New Question from Website',
text: `
User Name: ${username}
Contact: ${contact}
Email: ${email}
Question: ${question}
`
};
// Send email
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
res.status(500).send('Internal Server Error');
} else {
console.log('Email sent: ' + info.response);
res.send('Email sent successfully');
}
});
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
我希望当我或用户点击提交按钮时,我的 [电子邮件受保护] 帐户应该收到电子邮件。我配置的其他 Gmail 帐户在两步验证后获得访问密钥。
let transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: '',
pass: ''
},
tls: {
rejectUnauthorized: false
}
});
请使用此代码并关闭两因素身份验证,因为我们正在使用第三方