DEV Community

loading...
Cover image for Js Node (NodeMailer)

Js Node (NodeMailer)

Dimitris Chitas
・3 min read

Hello there guys,i hope my post finds you well and with appetite for programming during the week.

Today my subject is NodeMailer a module which stands for transporting email in javascript logic.

It is a node module so you can easily install it with yarn or
npm,i will go ahead with npm.

So, first things first just simple open up your CLI and type

npm install nodemailer

Requirements

Node.js v6.0.0 or newer. That’s it.

Nodemailer is a module for Node.js applications to allow easy as cake email sending. The project got started back in 2010 when there was no sane option to send email messages, today it is the solution most Node.js users turn to by default.

Usage

Setting Up

To send an email you need a transport object which will include some additions about the email,the recipient,the adress and some other stuff like that.

So we are creating our object

let trasnporter = nodemailer.createTransport(transport[, defaults])

Above we see a variable which stores the nodemailer object,which is responsible to send the email as
long as it uses the createTransport method to define our email parameters transport[,defaults] i will explain you real quick
those two params.

  1. Transport is the transport configuration object, connection url or a transport plugin instance.
  2. Defaults is an object that defines default values for mail options.

Send Email

Once you have a transporter object you can send mail with it:

transporter.sendMail(data[, callback])

Where :

data defines the mail content (see Message Configuration for possible options).

Callback is an optional callback function to run once the message is delivered or it failed.

Also it has more stuff you can receive and work with check here
NodeMailer Usage

If callback argument is not set then the method returns a Promise object. Nodemailer itself does not use Promises internally but it wraps the return into a Promise for convenience.

Message Configuration

Your message and his body should look like my example bellow :

var message = {
from: "test_sender@server.com",
to: "test_seceiver@sender.com",
subject: "NodeMailer",
text: "Hello this is plain text example",
html: "<p>This is html plain text example</p>"
};

As we can see above we created message variable which is initiated as object with the rest key-pair values.Those are the most common fields

1.from - The email address of the sender. All email addresses can be plain ‘sender@server.com’ or formatted '“Sender Name”
sender@server.com', see Address object for details.

2.to - Comma separated list or an array of recipients email addresses that will appear on the To: field

3.cc - Comma separated list or an array of recipients email addresses that will appear on the Cc: field

4.bcc - Comma separated list or an array of recipients email addresses that will appear on the Bcc: field

5.subject - The subject of the email

6.text - The plaintext version of the message as an Unicode string, Buffer, Stream or an attachment-like object ({path: ‘/var/data/…'})

7.html - The HTML version of the message as an Unicode string, Buffer, Stream or an attachment-like object ({path: ‘http://…'})

8.attachments - An array of attachment objects (see Using attachments for details). Attachments can be used for embedding images as well.

A large majority of emails sent look a lot like this, using only a few basic fields,you can check here for more advanched fields:
Mail Fields.

Is coming also with an Ap NodemailerApp is the ultimate cross platform email debugging app.

App includes local SMTP and POP3 servers, a sendmail replacement, catchall email domain service, AMP4Email renderer and it imports emails from EML files, EMLX files, large MBOX files from Gmail takeout, Maildir folders and Postfix queue files for inspection and preview. Ever wanted to view the actual HTML source of a nicely designed email instead of some garbled rfc822 text? Just open the HTML tab of an email to see it.
Check Here...Nodemailer App

That's all we have for today for more information check the documentation on github link.

Have a nice workday guys, in case for further explanation do not hesitate to contact me or find me in github or linkedin.
GitHub : https://github.com/feco2019
Linkedin : https://www.linkedin.com/in/dimitris-chitas-930285191/

Discussion (0)