react sending email via Nodemailer using node.js
const express = require('express');
const mailerRouter = express.Router();
const Nodemailer = require('nodemailer')
mailerRouter.post('/email', (req, res) => {
const data = req.body
console.log(`${data.name}, ${data.email}, ${data.subject}, ${data.messages}`)
const output = `Name: ${data.name} <br>
Email: ${data.email}<br><br>
${data.messages}`
// file attachment
if (req.files === null) {
return res.status(400).json({ msg: 'No file uploaded' });
}
const file = req.files.file
console.log(req.files)
console.log(file)
file.mv(`${__dirname}/../clients/public/images/${file.name}`, err => {
if (err) {
console.error(err);
return res.status(500).send(err);
}
res.json({ fileName: file.name, filePath: `/images/${file.name}` });
});
// create reusable transporter object using the default SMTP transport
let transporter = Nodemailer.createTransport({
service: 'Gmail',
post: 587,
secure: false,
auth:{
user: 'your email',
pass: "password"
},
tls:{
rejectUnauthorized:false
}
});
// setup email data with unicode symbols
let mailOptions ={
from: `Your name`,
to: "list of recivers",
name: `${data.name}`,
html: output,
attachments:[{
path: `${__dirname}/../clients/public/images/${file.name}`,
}]
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (err)=>{
if(err){
return console.log(err)
}else{
console.log(`Success`)
}
})
transporter.close()
})
module.exports = mailerRouter;
Are there any code examples left?
New code examples in category Other
-
Other 2023-03-27 22:50:10 how to select the whole line in vscode with keyboard shortcut
-
Other 2022-03-27 22:45:24 income of a web developer
-
Other 2022-03-27 22:35:01 \pyrcc_main.py: File does not exist 'resources.qrc'
-
Other 2022-03-27 22:30:45 rick roll embed code
-
Other 2022-03-27 22:20:08 Circuit_04_Potentiometer
-
Other 2022-03-27 22:20:05 iterative power
-
Other 2022-03-27 22:15:11 flutter run all
-
Other 2022-03-27 22:10:05 when is karlson release
-
Other 2022-03-27 22:10:02 wp .htaccess example
-
Other 2022-03-27 22:00:08 bash pause in file read line by line