How can I send an email in Node js after successfully storing a record in MongoDB

0 votes
With the help of code can you tell me How can I send an email in Node.js after successfully storing a record in MongoDB?
Mar 12 in Node-js by Ashutosh
• 23,230 points
81 views

1 answer to this question.

0 votes

You can use Nodemailer to send an email in Node.js after successfully storing a record in MongoDB.

Steps:

Install Dependencies

npm install nodemailer mongoose

Code Example

const mongoose = require('mongoose');

const nodemailer = require('nodemailer');

// MongoDB Connection

mongoose.connect('mongodb://localhost:27017/testDB', { useNewUrlParser: true, useUnifiedTopology: true });

// Define Schema & Model

const UserSchema = new mongoose.Schema({ email: String, name: String });

const User = mongoose.model('User', UserSchema);

// Nodemailer Transporter

const transporter = nodemailer.createTransport({

    service: 'gmail',

    auth: {

        user: 'your-email@gmail.com',

        pass: 'your-email-password'

    }

});

// Function to Save Data and Send Email

async function saveUserAndSendEmail(userData) {

    try {

        const user = await User.create(userData);

        // Email Options

        const mailOptions = {

            from: 'your-email@gmail.com',

            to: user.email,

            subject: 'Welcome!',

            text: `Hello ${user.name}, your record has been saved successfully!`

        };

        // Send Email

        await transporter.sendMail(mailOptions);

        console.log('Email Sent Successfully!');

    } catch (error) {

        console.error('Error:', error);

    }

}

// Example Usage

saveUserAndSendEmail({ email: 'test@example.com', name: 'John Doe' });

answered Mar 12 by Tanvi

Related Questions In Node-js

0 votes
1 answer

How do I manage MongoDB connections in a Node.js web application?

When the Node.js application starts, create a ...READ MORE

answered Jun 10, 2022 in Node-js by Neha
• 9,020 points
2,116 views
0 votes
1 answer

How can i get the extension of the image in node.js?

Hello @kar You can do the following to ...READ MORE

answered Jul 16, 2020 in Node-js by Niroj
• 82,840 points
2,263 views
0 votes
1 answer

How can I use an http proxy with node.js http.Client?

Hello @kartik, You can use request, I just found ...READ MORE

answered Jul 17, 2020 in Node-js by Niroj
• 82,840 points
10,823 views
0 votes
1 answer

How can I get the browser language in node.js?

Hello @kartik, You can use req.headers["accept-language"] to get the language/locale ...READ MORE

answered Oct 16, 2020 in Node-js by Niroj
• 82,840 points
4,559 views
0 votes
1 answer

How to use async functions effectively in React components?

To use async functions effectively in React ...READ MORE

answered Mar 12 in Node-js by Sahil
58 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How does Babel differ from JSX?

Feature Babel JSX Definition A JavaScript compiler that transforms ES6+ code ...READ MORE

answered Mar 12 in Java-Script by Sahil
90 views
0 votes
1 answer

How do I use ES6 features like destructuring in a Node.js application?

To use ES6 features like destructuring in ...READ MORE

answered Dec 17, 2024 in Node-js by Navya
126 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP