Node JS : Express JS : Formidable : Nodemailer - 如何在联系表单上上传 html 模板并使用 Nodemailer 发送?
Node JS : Express JS : Formidable : Nodemailer - How can i upload a html template on contact form and send it using Nodemailer?
我有一个联系表单,我想使用输入文件上传一个 html 模板并使用 Nodemailer 发送。我已经使用 formidable 来获取临时路径,但我无法将此参数传递给 sendMail 的 HTML 标记。我该如何存档?
这是我现在的app.js
const express = require('express');
const formidable = require('formidable');
var fs = require('fs');
const port = process.env.PORT || 3000;
const app = express();
app.get('/', (req, res) => {
res.send(`<h2>With<code>"express"</code>
npmpackage</h2>
<form action="/api/upload" enctype="multipart/form-data" method="post">
<div>Text field title:<input type="text" name="title"/></div>
<div>File:<input type="file" name="someExpressFiles" multiple="multiple"/>
</div>
<input type="submit" value="Upload"/>
</form>`);
});
app.post('/api/upload', (req, res, next) => {
const form = new formidable.IncomingForm({ multiples: true });
form.parse(req, (err, fields, files) => {
if (err) { next(err); return; }
res.json({
fields, files
});
});
async function main() {
let testAccount = await nodemailer.createTestAccount();
let transporter = nodemailer.createTransport({
host: "smtp.ethereal.email",
port: 587,
secure: false,
auth: {
user: testAccount.user,
pass: testAccount.pass,
},
});
let info = await transporter.sendMail({
from: '"Fred Foo " <foo@example.com>',
to: "bar@example.com, baz@example.com",
subject: "Hello ✔",
text: "Hello world?",
html: "<b>Hello world?</b>",
});
console.log("Message sent: %s", info.messageId);
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
}
});
app.listen(port, () => {
console.log('Server listening on http://localhost:3000...');
}).timeout = 1000 * 60 * 10;
解决了:
const express = require('express');
const formidable = require('formidable');
const nodemailer = require("nodemailer");
const fs = require('fs');
const path = require('path')
const port = process.env.PORT || 3001;
const app = express();
app.use('/public', express.static(path.join(__dirname, 'public')));
app.set('views', './views');
app.get('/', (req, res) => {
fs.readFile('views/index.html', function (err, data) {
res.writeHead(200, { 'Content-Type': 'text/html', 'Content-Length': data.length });
res.write(data);
res.end();
});
});
app.post('/', (req, res, next) => {
const form = new formidable.IncomingForm({ multiples: true });
form.parse(req, (err, fields, files) => {
if (err) { next(err); return; }
// res.json({
// fields, files
// });
var expressPath = fs.readFileSync(files.expressFiles.filepath).toString();
async function main() {
let testAccount = await nodemailer.createTestAccount();
let transporter = nodemailer.createTransport({
host: "smtp.ethereal.email",
port: 587,
secure: false,
auth: {
user: testAccount.user,
pass: testAccount.pass,
},
});
let info = await transporter.sendMail({
from: '"Fred Foo " <foo@example.com>',
to: "bar@example.com, baz@example.com",
subject: "Hello ✔",
text: "Hello world?",
html: "<b>Hello world?</b>",
});
console.log("Message sent: %s", info.messageId);
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
res.sendFile(__dirname + '/views/success.html')
msg = true;
}
main().catch(console.error);
});
});
app.listen(port, () => {
console.log('Server listening on http://localhost:3000...');
}).timeout = 1000 * 60 * 10;
我有一个联系表单,我想使用输入文件上传一个 html 模板并使用 Nodemailer 发送。我已经使用 formidable 来获取临时路径,但我无法将此参数传递给 sendMail 的 HTML 标记。我该如何存档?
这是我现在的app.js
const express = require('express');
const formidable = require('formidable');
var fs = require('fs');
const port = process.env.PORT || 3000;
const app = express();
app.get('/', (req, res) => {
res.send(`<h2>With<code>"express"</code>
npmpackage</h2>
<form action="/api/upload" enctype="multipart/form-data" method="post">
<div>Text field title:<input type="text" name="title"/></div>
<div>File:<input type="file" name="someExpressFiles" multiple="multiple"/>
</div>
<input type="submit" value="Upload"/>
</form>`);
});
app.post('/api/upload', (req, res, next) => {
const form = new formidable.IncomingForm({ multiples: true });
form.parse(req, (err, fields, files) => {
if (err) { next(err); return; }
res.json({
fields, files
});
});
async function main() {
let testAccount = await nodemailer.createTestAccount();
let transporter = nodemailer.createTransport({
host: "smtp.ethereal.email",
port: 587,
secure: false,
auth: {
user: testAccount.user,
pass: testAccount.pass,
},
});
let info = await transporter.sendMail({
from: '"Fred Foo " <foo@example.com>',
to: "bar@example.com, baz@example.com",
subject: "Hello ✔",
text: "Hello world?",
html: "<b>Hello world?</b>",
});
console.log("Message sent: %s", info.messageId);
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
}
});
app.listen(port, () => {
console.log('Server listening on http://localhost:3000...');
}).timeout = 1000 * 60 * 10;
解决了:
const express = require('express');
const formidable = require('formidable');
const nodemailer = require("nodemailer");
const fs = require('fs');
const path = require('path')
const port = process.env.PORT || 3001;
const app = express();
app.use('/public', express.static(path.join(__dirname, 'public')));
app.set('views', './views');
app.get('/', (req, res) => {
fs.readFile('views/index.html', function (err, data) {
res.writeHead(200, { 'Content-Type': 'text/html', 'Content-Length': data.length });
res.write(data);
res.end();
});
});
app.post('/', (req, res, next) => {
const form = new formidable.IncomingForm({ multiples: true });
form.parse(req, (err, fields, files) => {
if (err) { next(err); return; }
// res.json({
// fields, files
// });
var expressPath = fs.readFileSync(files.expressFiles.filepath).toString();
async function main() {
let testAccount = await nodemailer.createTestAccount();
let transporter = nodemailer.createTransport({
host: "smtp.ethereal.email",
port: 587,
secure: false,
auth: {
user: testAccount.user,
pass: testAccount.pass,
},
});
let info = await transporter.sendMail({
from: '"Fred Foo " <foo@example.com>',
to: "bar@example.com, baz@example.com",
subject: "Hello ✔",
text: "Hello world?",
html: "<b>Hello world?</b>",
});
console.log("Message sent: %s", info.messageId);
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
res.sendFile(__dirname + '/views/success.html')
msg = true;
}
main().catch(console.error);
});
});
app.listen(port, () => {
console.log('Server listening on http://localhost:3000...');
}).timeout = 1000 * 60 * 10;