P
P
prolina2019-06-11 15:10:36
Node.js
prolina, 2019-06-11 15:10:36

Getting data after submitting a React form?

React app has a page with a feedback form. After filling it out and sending it, I would like to receive the data that the person entered by email. How to implement it?
All I could find was to use nodemailer. But everywhere there are examples with information about the password and email in the public domain.
Can you please tell me how can I implement what I have in mind?
Here is an example of the code I used:

const express = require('express');
const bodyParser = require('body-parser');
const nodemailer = require('nodemailer');
const app = express()

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));

app.post('/api/form', (req, res) => {
    nodemailer.createTestAccount((err, account) => {
        const htmlEmail = `
        <h3>Contact details</h3>
        <ul>
        <li>Name: ${req.body.name}</li>
        <li>Email: ${req.body.email}</li>
        </ul>
        <h3>Message</h3>
        <p>${req.body.message}</p>
        `
        let transporter = nodemailer.createTransport({
            service: 'gmail',
            host: 'smtp.gmail.com',
            port: 587,
            // auth: {
            //     user: '[email protected]',
            //     pass: 'password'
               
            // }
        })
        let mailOptions = {
            from: req.body.email,
            to: '[email protected]',
            subject: 'New message',
            text: req.body.message,
            html: htmlEmail
        }
        transporter.sendMail(mailOptions, (err, info) => {
            if(err) {
                return console.log(err)
            }
            console.log('Message sent: %s', info.message)
            console.log('Message URL: %s', nodemailer.getTestMessageUrl(info))
        })
    })
})

const PORT = process.env.PORT || 3001;

app.listen(PORT, () => {
    console.log(`Server listening on port ${PORT}`);

})

At the same time, I receive notifications in my mail that someone wanted to log into my account, i.e., this code looks like I want to log in and send a letter. And I need letters to come to my email from different people who fill out the form.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2019-06-11
@prolina

This is how it works. Send a letter to yourself on a box like [email protected] with the subject "Appeal", in the body of the letter already indicate all the information you need about the person who filled out the form.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question