react add data from MongoDB to email

pretty new to react only been doing it for a couple of weeks and I’m working on a project for personal use to send an email to my email using nodemailer which I have managed to do. the next part I want to do is add data to the email that will come from my MongoDB database like the order number, customer name and status of the job I’ve searched high and low on youtube and google and not really finding anything on the issue

also, it only runs when I type node server.js and then it automatically sends the email which I don’t want I want it to run when submit is clicked when a status is updated in the database.

Here is the code for what I have on server.js

require('dotenv').config();

const nodemailer = require('nodemailer');


let transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: process.env.EMAIL,
    pass: process.env.PASSWORD
    }
});


let mailOptions = {
    from: 'group2021@gmail.com',
    to: 'edge@gmail.com',
    subject: 'Project Update',
    text: 'Hello {{name}} please find this email as an update to you project.'
};

transporter.sendMail(mailOptions, function(err, data) {
    if(err) {
        console.log('Error Occured!', err);
    } else {
        console.log('Email Sent!')
    }
});

24 thoughts on “react add data from MongoDB to email”

  1. I’m not sure how your application looks like, I assume it’s SPA react application.

    I suggest you to create simple http server using Expressjs and creating endpoint which you will call from the client (react app) e.g. (the code is not tested is just an example)

    const express = require('express');
    const app = express();
    const port = 3000;
    const nodemailer = require('nodemailer');
    
    app.get('/mail/:someID', async (req, res) => {
        // someID is identifier to find data in db
        // it will come from localhost:PORT/mail/>>someID<<
        const { someID } = req.params; 
        let data;
        try {
            data = await mongoCol.FindOne({
                /* query */
            }); // reads data from mongo
        } catch (err) {
            return res.status(500).json(err);
        }
    
        // prepare content
        var text =
            'Hello {{name}} please find this email as an update to you project.\n' + data;
    
        let transporter = nodemailer.createTransport({
            service: 'gmail',
            auth: {
                user: process.env.EMAIL,
                pass: process.env.PASSWORD
            }
        });
    
        let mailOptions = {
            from: 'group2021@gmail.com',
            to: 'edge@gmail.com',
            subject: 'Project Update',
            text: text
        };
    
        transporter.sendMail(mailOptions, function (err, data) {
            if (err) {
                console.log('Error Occured!', err);
                return res.status(500).json(err);
            } else {
                console.log('Email Sent!');
                return res.sendStatus(200);
            }
        });
    });
    
    app.listen(port, () => {
        console.log(`Example app listening at http://localhost:${port}`);
    });
    
    
    

    You should add some kind of authorization to not allow other people to send email by your server.

    also, it only runs when I type node server.js and then it automatically sends the email which I don’t want

    This happens because your code is not in function and any time you import or start file (module) it will execute.

    Reply

Leave a Comment