Getting user email from form - nodemailer

Viewed 27

So I am currently trying to create a newsletter mailer, sent out upon form submit. I have gotten the nodemailer code to work, I just don't know how to get the users email from the client side, into the mailOptions object. (This is a react ecommerce website)

My index.js on the server side is as follows:

        const nodemailer = require('nodemailer');

let transporter = nodemailer.createTransport({
    service: "gmail",
    secureConnection: true, 
    auth:{
        user: process.env.DB_EMAIL,
        pass: process.env.DB_PASS
    },
    tls:{
        rejectUnauthorized:false
    }
})

//new

app.get('/submit', (req, res) => {  

    let mailOptions = {
        from:  process.env.DB_EMAIL,
        to: req.body.userEmail,
        subject:"Welcome to SkyeSkates",
        text: "Whoop Whoop! You are officially part of the team!"
    }
    transporter.sendMail(mailOptions, (err, success)=>{
        if(err){
            console.log(err);
        }else{
            console.log("email sent!");
        }
    
    
    })
}) 

And this is my form on the home page of my client side:

   <form  action="/submit" class="contact-form">
              <h1>Pssst, hey, you <br/> (yeah, you)</h1>
              <p>Want in on new arrivals, exclusive collabs, sales and more? <br/> Well, you know what to do.</p>
              <input type="text" name="userEmail" id="userEmail" placeholder='Your email, please'></input>
              <input type="submit" id="send-btn" value="Send Message"></input>
              <br/>
              <small>To see how we may use your information, take a look at our privacy policiy.</small>        
          </form> 
1 Answers

make an express.js or node.js server

const express = require('express.js')
const app = express()

    const nodemailer = require('nodemailer');

let transporter = nodemailer.createTransport({
    service: "gmail",
    secureConnection: true, 
    auth:{
        user: process.env.DB_EMAIL,
        pass: process.env.DB_PASS
    },
    tls:{
        rejectUnauthorized:false
    }
})




app.get('/submit', (req, res) => {  

    let mailOptions = {
        from:  process.env.DB_EMAIL,
        to: req.body.userEmail,
        subject:"Welcome to SkyeSkates",
        text: "Whoop Whoop! You are officially part of the team!"
    }
    transporter.sendMail(mailOptions, (err, success)=>{
        if(err){
            console.log(err);
        }else{
            console.log("email sent!");
        }
    
    
    })
app.redirect('/');

}) 


app.listen(PORT, ()=>{
  console.log("running');
}

html form :

<div className='fourth-header'>
                    <form  action="/submit" class="contact-form">
                          <h1>Pssst, hey, you <br/> (yeah, you)</h1>
                          <p>Want in on new arrivals, exclusive collabs, sales and more? <br/> Well, you know what to do.</p>
                          <input type="text" name="userEmail" id="userEmail" placeholder='Your email, please'></input>
                          <input type="submit" id="send-btn" value="Send Message"></input>
                          <br/>
                          <small>To see how we may use your information, take a look at our privacy policiy.</small>        
                      </form> 
                  </div>
Related