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?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source