NodeJS – Send email by Nodemailer

nodejs-send-email-by-nodemailer-example

In the tutorial, we show you how to send email from Node.js applications by Nodemailer module.

Nodemailer Module

Nodemailer is licensed under MIT license. It provides an easy way to send email.

To download and install it, we use below commandline:


$npm install nodemailer --save

Nodemailer Send Mail


var nodemailer = require('nodemailer');

var transporter = nodemailer.createTransport({
  // example with google mail service
  host: 'smtp.gmail.com',
  port: 587,
  secure: false, // true for 465, false for other ports
  auth: {
	user: 'send-email@gmail.com', // replace by your email to practice
    pass: '*****' // replace by your-password
  }
});

var mailOptions = {
  from: 'send-mail@gmail.com',
  to: 'to-email-1@gmail.com, to-email-2@gmail.com',
  subject: 'Sending Email using Nodemailer',
  
  /*
     for plain text body
	 ->	text: 'Just Testing!'
  */
  
  // html body
  html: '

Hello world!

The mail has been sent from Node.js application!

' }; transporter.sendMail(mailOptions, (error, info) => { if (error) return console.log(error); console.log('Email sent: ' + info.response); });

Results:

-> Logs on console:


node server.js
Email sent: 250 2.0.0 OK 1524409947 v12sm14584252pgs.21 - gsmtp

Nodejs - sending mail with Nodemailer

Nodemailer Attachment File

Attachments option in the message object that contains an array of attachment objects. Attachment object’s properties:

  • filename: the name of the attached file
  • content: String, Buffer or a Stream contents
  • path: path to the file
  • href: URL to the file
  • contentType: optional content type for the attachment
  • contentDisposition: optional content disposition type for the attachment, defaults to ‘attachment’
  • cid: optional content id for using inline images in HTML message source
  • encoding: Example values: ‘base64’, ‘hex’, ‘binary’ etc
  • headers: custom headers for the attachment node
  • raw: optional value that overrides entire contents of current mime node including mime headers

Example case:


var message = {
    ...
    attachments: [
		{	// filename and content type is derived from path	   
            path: '/path/to/file.txt'
        },
        {   // utf-8 string as an attachment
            filename: 'text.txt',
            content: 'JSA-Nodemailer!'
        },
        {   // file on disk as an attachment
            filename: 'text.txt',
            path: '/path/to/file.txt'
        },
        {   // define custom content type for the attachment
            filename: 'text.bin',
            content: 'JSA-Nodemailer!',
            contentType: 'text/plain'
        },
        {   // use URL as an attachment
            filename: 'license.txt',
            path: 'https://raw.github.com/nodemailer/nodemailer/master/LICENSE'
        },
        {   // data uri as an attachment
            path: 'data:text/plain;base64,aGVsbG8gd29ybGQ='
        },
        {   // use pregenerated MIME node
            raw: 'Content-Type: text/plain\r\n' +
                 'Content-Disposition: attachment;\r\n' +
                 '\r\n' +
                 'JSA-Nodemailer!'
        }
    ]
}

Practice:


var nodemailer = require('nodemailer');

var transporter = nodemailer.createTransport({
  host: 'smtp.gmail.com',
  port: 587,
  secure: false, // true for 465, false for other ports
  auth: {
	user: 'send-email@gmail', // replace by your email
    pass: '*****' // replace by your password
  }
});

var mailOptions = {
  from: 'javasampleapproach@gmail.com',
  to: 'javasampleapproach@gmail.com, technologyforchangetheworld@gmail.com',
  subject: 'Nodemailer with Attachment File',
  
  html: '

Attachments

', attachments: [ { // utf-8 string as an attachment filename: 'text.txt', content: 'JSA-Nodemailer!' }, { filename: 'logo', path: 'jsalogo.png' } ] }; transporter.sendMail(mailOptions, (error, info) => { if (error) { return console.log(error); } console.log('Email sent: ' + info.response); });

-> Result:

nodejs-nodemail-attachments

One thought on “NodeJS – Send email by Nodemailer”

Leave a Reply

Your email address will not be published. Required fields are marked *