I am trying to make a form where you can send images. I want these images to get send in the mail using nodemailer. I've figured out how to grab information from my form by using express and body parser and then putting that in my mail. This ofcourse didn't work for my images and either told me something was wrong in my app.js or it did send but gave undefined in the mail I recieved where the image should go.
I've tried looking for some kind of solution or answer but couldn't find one. So I would be grateful if someone could send me a link with a answer to my question or could help and explain it here to me.
Thanks!
My form:
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="public/css/style.css">
</head>
<body>
<div class="contact">
<ul>
<h2 class="centerFormName">Form</h2>
{{msg}}
<form method="POST" action="send" enctype="multipart/form-data">
<label>Placeholder Image:</label>
<input type="file" name="file" id="file"/>
</p>
<p class="full">
<label>Message:</label>
<textarea class="inputcss" name="message" rows="5"></textarea>
</p>
<p class="full">
<button type="submit">Submit</button>
</p>
</form>
</ul>
</div>
</body>
</html>
My app.js
const express = require('express');
const fileUpload = require('express-fileupload');
const bodyParser = require('body-parser');
const exphbs = require('express-handlebars');
const path = require('path');
const nodemailer = require('nodemailer');
const app = express();
// View engine setup
app.engine('handlebars', exphbs());
app.set('view engine', 'handlebars');
// Static folder
app.use('/public', express.static(path.join(__dirname, 'public')));
// Body Parser Middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.get('/', (req, res) => {
res.render('contact');
});
app.post('/send', (req, res) => {
//output
const output = `
<p>You have a new Mail</p>
<h3>Mail</h3>
<ul>
<li>Voornaam: ${req.body.name}</li>
<li>Achternaam: ${req.body.achternaam}</li>
<li>Email: ${req.body.email}</li>
</ul>
<h3>Message</h3>
<p>${req.body.message}</p>
`;
//Trnasporter
const transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
type: 'OAuth2',
user: 'xander@mail.com',
clientId: '',
clientSecret: '',
refreshToken: '',
accessToken: '',
},
});
//Mail options
let mailOptions = {
from: 'Xander <Xander@mail.com>', // sender address
to: req.body.email, // list of receivers
subject: 'Hi', // Subject line
html: output // html body
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
res.render('contact', {msg:'Email has been sent'});
});
});
app.listen(3000, () => console.log('Server started...'));
Note I've made the form a bit shorter here.