<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: ANUJ KUMAR</title>
    <description>The latest articles on Forem by ANUJ KUMAR (@anujkumaraj).</description>
    <link>https://forem.com/anujkumaraj</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1037879%2Ff1f4c811-795c-4ee2-8c87-3244a200b70e.jpeg</url>
      <title>Forem: ANUJ KUMAR</title>
      <link>https://forem.com/anujkumaraj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/anujkumaraj"/>
    <language>en</language>
    <item>
      <title>Mastering Email Sending with NodeJS and Express: A Step-by-Step Guide</title>
      <dc:creator>ANUJ KUMAR</dc:creator>
      <pubDate>Fri, 03 Mar 2023 14:32:20 +0000</pubDate>
      <link>https://forem.com/anujkumaraj/mastering-email-sending-with-nodejs-and-express-a-step-by-step-guide-4nkn</link>
      <guid>https://forem.com/anujkumaraj/mastering-email-sending-with-nodejs-and-express-a-step-by-step-guide-4nkn</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;h3&gt;
  
  
  About Email
&lt;/h3&gt;

&lt;p&gt;Email or Electronic mail now becomes a significant method of sending messages between two people or two devices. The email was thus conceived as the electronic (digital) version of, or counterpart to, mail, at a time when "mail" meant only physical mail (hence e- + mail).&lt;/p&gt;

&lt;h3&gt;
  
  
  Email in the current world
&lt;/h3&gt;

&lt;p&gt;An email later became a ubiquitous (very widely used) communication medium, to the point that in current use, an email address is often treated as a basic and necessary part of many processes in business, commerce, government, education, entertainment, and other spheres of daily life in most countries.&lt;/p&gt;

&lt;h3&gt;
  
  
  How the email server works
&lt;/h3&gt;

&lt;p&gt;Today's email systems are based on a store-and-forward model (Store and forward is a telecommunications technique in which information is sent to an intermediate station where it is kept and sent at a later time to the final destination or to another intermediate station. The intermediate station, or node in a networking context, verifies the integrity of the message before forwarding it). Email servers accept, forward, deliver, and store messages. Neither the users nor their computers are required to be online simultaneously; they need to connect, typically to a mail server or a webmail interface to send or receive messages or download them.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why do we need to send an email over NodeJS?
&lt;/h1&gt;

&lt;p&gt;Let's take a scenario where you have to send an email to a large group and you have to send an structured message like lets say in &lt;code&gt;html&lt;/code&gt; , to do so you cant use your gmail service provider api like google gmail , Microsoft outlook etc. because you can only use the services provided by these API . But if you have to make your custom functionality , Say you have to schedule your message when to send it. or you have made an app and for particular event you want to get notified over your email. and much more. You yourself can think many more utilities where Email can be your life Saver.&lt;/p&gt;

&lt;h1&gt;
  
  
  Prerequisite
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;NodeJS installed &lt;a href="https://nodejs.org/en/download/" rel="noopener noreferrer"&gt;You can install it from here&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;nodemailer installed&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Express installed&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We have done So much talking. Without further a do let's get stated with coding&lt;/p&gt;

&lt;h1&gt;
  
  
  Lets get our hand dirty
&lt;/h1&gt;

&lt;p&gt;So here we talk about email , which is in crude term 2 devices talking right. one device send message to other and vice versa and if they are taking . then it must be over some protocol which is in this case &lt;code&gt;http&lt;/code&gt; . And in nodeJS there is a &lt;code&gt;http&lt;/code&gt; module. This module is fantastic but we are going to use &lt;code&gt;express&lt;/code&gt; in our project which is build on top of &lt;code&gt;http&lt;/code&gt;. So what i mean when i say it is build on top of &lt;code&gt;http&lt;/code&gt; is, it have abstract away many complex code of &lt;code&gt;http&lt;/code&gt; , and made user friendly code for our use with enhanced functionality. Express is a minimal and flexible &lt;code&gt;Node.js&lt;/code&gt; web application framework that provides a robust set of features for web and mobile applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  creating nodemailer directory for our application
&lt;/h3&gt;

&lt;p&gt;go to the terminal for Linux or Unix(mac) users or open command prompt(cmd) or Power Shell for windows user&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir nodemailer
cd nodemailer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  installing dependencies (Express and nodemailer)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install express --save
npm install nodemailer --save
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;your package.json will look like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1668934174111%2FK-5LYcsps.png%2520align%3D" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1668934174111%2FK-5LYcsps.png%2520align%3D" alt="image.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note here --save flag is used so that the package.json will get updated with these dependencies automatically&lt;/p&gt;

&lt;p&gt;For those who don't know what package.json is , in short it contain all the modules which our app can use.&lt;/p&gt;

&lt;p&gt;if you want to know more about package.json check&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.npmjs.com/about-packages-and-modules" rel="noopener noreferrer"&gt;More info about Packages and Modules or package.json&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Let get to coding
&lt;/h3&gt;

&lt;p&gt;Create a file called &lt;code&gt;index.js&lt;/code&gt;, and open it in your favorite IDE(integrated development environment).&lt;/p&gt;

&lt;p&gt;add the following line of code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// importing express &lt;/span&gt;
&lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//importing nodemailer&lt;/span&gt;
&lt;span class="nx"&gt;nodemailer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;nodemailer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// creating an instance of express&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;here we have importing express and nodemailer in our &lt;code&gt;index.js&lt;/code&gt; file. and creating instance of express of app, creating of instance means just like in object oriented programming , we declared class and create an instance of that class. Here also &lt;code&gt;express = require('express');&lt;/code&gt; if class and &lt;code&gt;app = express();&lt;/code&gt; is instance of that class&lt;/p&gt;

&lt;p&gt;Next comes using nodemailer transporter object creation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let transporter = nodemailer.createTransport({
    host: "smtp.gmail.com",
    port: 587,
    secure: false, // true for 465, false for other ports
    auth: {
      user: //"User_ID", 
      pass: //"User_Password", 
    },
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;SMTP is the main transport in Nodemailer for delivering messages. SMTP is also the protocol used between different email hosts, so its truly universal. Almost every email delivery provider supports SMTP based sending, even if they mainly push their API based sending. APIs might have more features but using these also means vendor lock-in while in case of SMTP you only need to change the configuration options to replace one provider with another and you’re good to go.&lt;/p&gt;

&lt;p&gt;Note that in this example i have used gmail host, if you have different host please change it accordingly their SMTP configuration.&lt;/p&gt;

&lt;p&gt;In place of &lt;code&gt;User_ID&lt;/code&gt; place your email address, and in Place of your User_password place your account &lt;code&gt;password&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const message = await transporter.sendMail({
    from: '"Anuj " &amp;lt;Anujkumar.sk639@gmail.com&amp;gt;', // sender address
    to: "user, &amp;lt;anujkumaraj639@gmail.com&amp;gt;", // list of receivers
    subject: "Email from nodeJS server", // Subject line
    text: "Hi from nodeJS server", // plain text body
    html: "&amp;lt;b&amp;gt;Hi from nodeJS server&amp;lt;/b&amp;gt;", // html body
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we have use &lt;code&gt;await&lt;/code&gt; here, which is asynchronous or &lt;code&gt;async&lt;/code&gt; function , which will stop the further execution and execute the &lt;code&gt;transporter.sendMail&lt;/code&gt;, which will send the email&lt;/p&gt;

&lt;p&gt;Lets see what &lt;code&gt;transporter.sendMail&lt;/code&gt; contain. It have&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;from&lt;/code&gt; contain sender_name , and sender_address under arrow bracket&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;to&lt;/code&gt; contain receiver_name and receiver_address under arrow bracket&lt;br&gt;&lt;br&gt;
It's important to note that we can add as many address as we want ( which ois less then what our host email provider support at a time) by saperated by space eg &lt;a href="mailto:user1@host.com"&gt;user1@host.com&lt;/a&gt; &lt;a href="mailto:user2@host.com"&gt;user2@host.com&lt;/a&gt; &lt;a href="mailto:user3@host.com"&gt;user3@host.com&lt;/a&gt; .......&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;subject&lt;/code&gt; to have subject of the email&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;text&lt;/code&gt; it contain plane text which you will send , suppose if recever device cant read html format then it will read plant text content present in text&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;html&lt;/code&gt; we can add our html content which will rendered by receiver device&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log("Message sent: %s", message.messageId);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will print the conformation if the message was sent in terminal.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1668939160642%2FhVhCEfZ28.png%2520align%3D" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1668939160642%2FhVhCEfZ28.png%2520align%3D" alt="image.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Challenges You may Face
&lt;/h1&gt;

&lt;p&gt;The main challange i have face is with authorization of my email id , as i have used 2 factor authentication in gmail account, I was not able to connect with my email host to send messages while creating** transport Object** , So i have to create app password in my gmail account and used it as my user_password in transport object You can check solution for this &lt;a href="https://community.nodemailer.com/using-gmail/" rel="noopener noreferrer"&gt;problem here&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;In this blog we have learn to send email using nodeJS, you can experement as many functionality as you want, using &lt;a href="https://nodemailer.com/about/" rel="noopener noreferrer"&gt;nodemailer documentations&lt;/a&gt; . This is basic nodeJS email sender.&lt;/p&gt;

&lt;p&gt;you can check my &lt;a href="https://github.com/Anuj-Kumar-AJ/Sending-Email-using-nodeJS-and-Express/blob/main/index.js" rel="noopener noreferrer"&gt;github reposotory&lt;/a&gt;. I have used version control git in this project so you can check how this project build step by step. If this repo updated in future, I will keep git file for you to build project step by step by reading &lt;code&gt;README.md&lt;/code&gt; file for that commit.&lt;/p&gt;

</description>
      <category>quantum</category>
      <category>machinelearning</category>
      <category>learning</category>
      <category>github</category>
    </item>
  </channel>
</rss>
