I have a contact form and i want to upload a html template using the input file and send it with Nodemailer. I have used formidable to get a temporary path but im not able to pass this parameter to the HTML tag of sendMail. How can i archive this ?
This is my current 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;
Solved it :
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;