How to send Attachments and Email using nodemailer in Node.js ? Last Updated : 06 Apr, 2023 Comments Improve Suggest changes Like Article Like Report For this purpose, we will use a package called nodemailer. It is a module that makes email sending pretty easy. For using it, you will need to install by using the following command: $ npm install nodemailer Features of nodemailer module: It has zero dependencies and heavy security.You can use it hassle-free whether it is Azure or Windows box.It also comes with Custom Plugin support for manipulating messages. In order to send an email, create a file named index.js and write down the following code: Filename: index.js javascript const nodemailer = require("nodemailer"); let sender = nodemailer.createTransport({ service: 'gmail', auth: { user: '[email protected]', pass: 'your_password' } }); let mail = { from: "[email protected]", to: "receiver'[email protected]", subject: "Sending Email using Node.js", text: "That was easy!" }; sender.sendMail(mail, function (error, info) { if (error) { console.log(error); } else { console.log("Email sent successfully: " + info.response); } }); Steps to run this program: In order to run this file, just Git Bash into your working directory and type the following command: $ nodemon index.js If you don't have Nodemon installed then just run the following command: $ node index.js To double-check its working you can go to the receiver's mail and you will get the following mail as shown below: What if you have multiple receivers? Well in that case just add the below code to your mail function: to: '[email protected], [email protected]' What if you want to send HTML-formatted text to the receiver? Well in that case just add the below code to your mail function: html: "<h1>GeeksforGeeks</h1><p>I love geeksforgeeks</p>" What if you want to send an attachment to the receiver? Well in that case just add the below code to your mail function: attachments: [ { filename: 'mailtrap.png', path: __dirname + '/mailtrap.png', cid: 'uniq-mailtrap.png' } ] The final index.js file looks like this: Filename: index.js javascript let nodemailer = require("nodemailer"); let sender = nodemailer.createTransport({ service: 'gmail', auth: { user: '[email protected]', pass: 'your_password' } }); let mail = { from: '[email protected]', to: '[email protected], [email protected]', subject: 'Sending Email using Node.js', text: 'That was easy!', html: '<h1>GeeksforGeeks</h1>< p > I love geeksforgeeks</p>', attachments: [ { filename: 'mailtrap.png', path: __dirname + '/mailtrap.png', cid: 'uniq-mailtrap.png' } ] }; sender.sendMail(mail, function (error, info) { if (error) { console.log(error); } else { console.log('Email sent successfully: ' + info.response); } }); Comment More infoAdvertise with us Next Article How to send Attachments and Email using nodemailer in Node.js ? M MohdArsalan Follow Improve Article Tags : Web Technologies Node.js Node.js-Misc Similar Reads How to send email with Nodemailer using Gmail account in Node ? Nodemailer is an npm module that allows you to send emails easily from the backend. In this article, we will cover the steps to send email using a Gmail account with the help of nodemailer. Prerequisites:NPM and NodeJSExpressJSApproach To send email with Nodemailer using gmailImport the nodemailer m 3 min read How to Send Email using Mailgun API in Node.js ? Sending an email is an essential part of any project and it can be achieved by using Mailgun API. It is very popular for sending emails. Features of Mailgun: . It is easy to get started and easy to use.It is a widely used and popular module for sending emails.Mails can also be scheduled. Installati 2 min read How to Send Email using NodeJS? Sending emails programmatically is a common requirement in many applications, especially for user notifications, order confirmations, password resets, and newsletters. In this article, we will learn how to build a simple email-sending system using NodeJS. We will use Nodemailer, a popular module for 5 min read Sending bulk emails in Node.js using SendGrid API What is SendGrid API? SendGrid is a platform for sending transactional and marketing emails to the customers. It provides scalability, reliability, and deliverability which is an important issue for an organization.Benefits of using SendGrid API: If you are using Nodemailer with Gmail then you can s 2 min read How to Send Response From Server to Client using Node.js and Express.js ? In web development, sending responses from the server to the client is a fundamental aspect of building interactive and dynamic applications. Express.js, a popular framework for Node.js, simplifies this process, making it easy to send various types of responses such as HTML, JSON, files, and more. T 4 min read How to Post Data in MongoDB Using NodeJS? In this tutorial, we will go through the process of creating a simple Node.js application that allows us to post data to a MongoDB database. Here we will use Express.js for the server framework and Mongoose for interacting with MongoDB. And also we use the Ejs for our front end to render the simple 5 min read How to read and write files in Node JS ? NodeJS provides built-in modules for reading and writing files, allowing developers to interact with the file system asynchronously. These modules, namely fs (File System), offer various methods for performing file operations such as reading from files, writing to files, and manipulating file metada 2 min read How to Create and Manipulate PDF Documents in Node.js with 'PDFKit' Module ? Creating and manipulating PDF documents programmatically is a common requirement in many web applications. Whether it's generating invoices, reports, or complex forms, the ability to create PDFs directly from your server-side code can greatly enhance the functionality of your application. In this ar 3 min read How to Access the File System in Node.js ? In this article, we looked at how to access the file system in NodeJS and how to perform some useful operations on files. Prerequisite: Basic knowledge of ES6Basic knowledge of NodeJS NodeJS is one of the most popular server-side programming frameworks running on the JavaScript V8 engine, which uses 3 min read How to Send an Image using Ajax ? Ajax stands for Asynchronous Javascript and XML and is used to make indirect requests to other origins. It can help you to perform a change to the web page dynamically.We can make an AJAX request with a special object called XMLHttpRequest which provides us with different methods to create an HTTP r 5 min read Like