Node.js Tutorial: Send an Email with Attachment

Node.js Tutorial: Send an Email with Attachment

Node.js is an open-source server environment. It’s free and runs on various platform such as Windows, Linux, Mac OS X, etc. Node.js was written initially by Ryan Dahl in 2009. It’s written in C, C++, and JavaScript. It can generate dynamic page content, collect form data, manage data in your database, manage files on a server, and etc… In this tutorial. You will learn how to send email using node js.

There are many modules for sending E-Mails, such as Nodemailer. emailjs, express-mailer, and etc. In this article, we use Nodemailer module.

JavaScript Tutorial: Introduction for beginners (Basic)

The Nodemailer Module

Official Page: https://nodemailer.com/about/

Creating a Project Directory

First, create a directory for our project. then enter the following command to create a package.json 

npm init -y
node.js send an email

Install Nodemailer module

npm install nodemailer --save
node.js send an email

Include the module

var emailer = require('nodemailer');

Send an Email

Now create a .js file and add the following code and save it.

sendemail.js

var emailer = require('nodemailer');

var transport = emailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'youremail@gmail.com',
    pass: 'yourpassword'
  }
});

var maildata= {
  from: 'youremail@gmail.com',
  to: 'sentmail@domain.com',
  subject: 'Sending Email using Node.js :)',
  text: 'Hello World!'
};

transport.sendMail(maildata, function(error, a){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + a.response);
  }
})

Now execute node.js file

node.js send an email
node.js send an email

Send an Email with Attachment

node.js send an email

Change the maildata object as follows. Then save the file.

var maildata= {
  from: 'youremail@gmail.com',
  to: 'sentmail@domain.com',
  subject: 'Sending Email using Node.js :)',
  text: 'Hello World!',
  attachments: [
    {
       path: 'data/text.txt' //pathOfTheAttachmet
    }
  ]
};
var emailer = require('nodemailer');

var transport = emailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'youremail@gmail.com',
    pass: 'yourpassword'
  }
});

var maildata= {
  from: 'youremail@gmail.com',
  to: 'sentmail@domain.com',
  subject: 'Sending Email using Node.js :)',
  text: 'Hello World!',
  attachments: [
    {
       path: 'data/text.txt'
    }
  ]
};

transport.sendMail(maildata, function(error, a){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + a.response);
  }
})

Now execute node.js file

Send an Email with Multiple Attachments

Change the maildata object as follows. Then save the file.

var maildata= {
  from: 'youremail@gmail.com',
  to: 'sentmail@domain.com',
  subject: 'Sending Email using Node.js :)',
  text: 'Hello World!',
  attachments: [
    {
       path: 'data/text.txt'
    },
    {
        path: 'data/image.png'
    },
    {
        name: 'test.txt',
        path: 'data/secret'
     }
  ]
};
var emailer = require('nodemailer');

var transport = emailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'youremail@gmail.com',
    pass: 'yourpassword'
  }
});

var maildata= {
  from: 'youremail@gmail.com',
  to: 'sentmail@domain.com',
  subject: 'Sending Email using Node.js :)',
  text: 'Hello World!',
  attachments: [
    {
       path: 'data/text.txt'
    },
    {
        path: 'data/image.png'
    },
    {
        name: 'test.txt',
        path: 'data/secret'
     }
  ]
};

transport.sendMail(maildata, function(error, a){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + a.response);
  }
})

Now execute node.js file

Hello Friends,

Hope this post (Node.js Tutorial: Send an Email with Attachment) will help you to learn something, If you enjoy my work then please share my posts with your friends and anyone who might be interested in web development and don’t forget to subscribe my mailing list.

If you had any problem with this post, please do mention it in the comment section.

Like ctechf Facebook fan page : https://www.facebook.com/ctechf

Best Free eBooks to Learn Python Programming

Free eBooks to Learn Python, Here are 13 of the best Python eBooks for python lovers.
Download Now

What is Python?

Python is a scripting language and that is used for creating engineering-analysis tools, web development (server-side), software development, system scripting, and animation software. It designed by Guido van Rossum and first released in 1991. It’s an interpreted, High-level. General-purpose language. Python runs on Windows, Mac OS, and Linux.

Python Tutorial: Introduction for Beginners (Part 1)
Python Tutorial: Introduction for Beginners (Part 2)
Python Tutorial: How to Connect MySQL Database in Python
Python Tutorial: Python GUI Programming (Tkinter)
Python Tutorial: Send Emails Using Python
Python Tutorial: Django Tutorial for Beginners

How to be a Programmer

Liked it? Take a second to support CtechF on Patreon!

Leave a Reply

3 Shares
Share
Tweet
Share
Pin
Share