<?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>DEV Community: aram</title>
    <description>The latest articles on DEV Community by aram (@aramrafeq).</description>
    <link>https://dev.to/aramrafeq</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%2F234129%2F311cba84-5143-4461-88d6-ff1e3c4163fc.jpeg</url>
      <title>DEV Community: aram</title>
      <link>https://dev.to/aramrafeq</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aramrafeq"/>
    <language>en</language>
    <item>
      <title>What are the genetic algorithms?</title>
      <dc:creator>aram</dc:creator>
      <pubDate>Mon, 17 Feb 2020 13:11:12 +0000</pubDate>
      <link>https://dev.to/aramrafeq/what-are-the-genetic-algorithms-21kc</link>
      <guid>https://dev.to/aramrafeq/what-are-the-genetic-algorithms-21kc</guid>
      <description>&lt;p&gt;This is a short article that explains what are genetic algorithms in general&lt;/p&gt;

&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;One of the fields in programming world is Artificial Intelligence we as humans make very weird solutions to some weirder problems that we have in our modern society which add a little bit knowledge to our understanding, in this article we are gonna talk about a field or a branch in AI which is called genetic algorithms what are they? how do they work? in what kind of problems we use them? &lt;/p&gt;

&lt;p&gt;in simplest terms genetic algorithms are to solve those kind of problems which have multiple solutions or have a very large search space that is just unfeasible to try solving it with traditional programming.&lt;/p&gt;

&lt;h1&gt;
  
  
  The Idea
&lt;/h1&gt;

&lt;p&gt;Best way to deliver our idea is to make an example or make a story and we try to understand through it basically genetic algorithms are using the idea of evolution and the concept of natural selection if we take a population of animals in nature we may find strong individuals, weak ones, old ones...&lt;br&gt;
what is important here what are the individuals who reproduce and make a new generation&lt;/p&gt;
&lt;h1&gt;
  
  
  Problem Example
&lt;/h1&gt;

&lt;p&gt;Let's say you have a a hypothetical echo system where animals have a DNA consisted only of binary numbers and the length of DNA for every animal is 8bit(1 byte) long rather miserable animals 🤔 but assume that what our goal is to find an animal that have a DNA which is exactly like &lt;code&gt;11111111&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;in another way the problem is like this &lt;br&gt;
&lt;code&gt;in a binary string of length 8 find a string that have all bits set to 1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;to start solving those kind of problems which we know the solution but we miss the path to reaching that solution or at least approximation of the solution is acceptable we use genetic algorithms&lt;/p&gt;

&lt;p&gt;in our example what is the best way to find an animal which have a DNA of 1's ?&lt;/p&gt;

&lt;p&gt;the answer is simple &lt;code&gt;Guessing&lt;/code&gt; is our best choice we generate random DNA samples and evaluate each sample and we pick only ones that are more like our ideal animal, our animals have genomes consist of chromosomes which consist of DNA's so animals in our echo system have 1 chromosome which contains 1 DNA stripe with size of 1 byte.&lt;/p&gt;

&lt;p&gt;we talked about picking animals that are similar to our ideal animal evaluation is based on something called a &lt;code&gt;fitness value&lt;/code&gt; and &lt;code&gt;fitness function&lt;/code&gt;&lt;br&gt;
fitness value : is related to each individual(animal in our case)&lt;br&gt;
fitness function: is the function that assigns and evaluates fitness value for each individual saying this animal is more like our ideal animal ex: because it's DNA contains 5 ones so we pick that over another animal.&lt;/p&gt;
&lt;h1&gt;
  
  
  The Steps
&lt;/h1&gt;

&lt;p&gt;Steps for approaching the solution we use our ideal animal example &lt;/p&gt;
&lt;h2&gt;
  
  
  Generate initial population
&lt;/h2&gt;

&lt;p&gt;We start by making a population(an array) of 22 animals(DNA strings) randomly(math.random 😁) we call this &lt;code&gt;Generation1 or g1&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Run fitness function on g1
&lt;/h2&gt;

&lt;p&gt;We run fitness function on g1 and assign fitness value to each animal or DNA string &lt;/p&gt;
&lt;h2&gt;
  
  
  Pick best animals to reproduce
&lt;/h2&gt;

&lt;p&gt;What our randomly generated animals would do reproduce of course but it's not up to them to pick we do that by picking ones that have highest fitness value or there are multiple ways to do that it's called &lt;br&gt;
&lt;code&gt;Selection&lt;/code&gt; phase in academic books &lt;/p&gt;

&lt;p&gt;a. Roulette Wheel Selection(we talk about this)&lt;br&gt;
b. Rank Selection&lt;br&gt;
c. Steady State Selection&lt;br&gt;
d. Tournament Selection&lt;br&gt;
e. Elitism Selection&lt;/p&gt;

&lt;p&gt;let's say we went with Roulette Wheel Selection what this does it will assign a portion of the wheel to each individual based on their calculated fitness value the higher the value the larger are they occupy on the wheel&lt;br&gt;
then we spin the wheel and pick a spot randomly what we see is that animals with higher fitness value are most likely to get picked &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SVd_yQxM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://docs.oracle.com/javafx/2/charts/img/pie-sample.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SVd_yQxM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://docs.oracle.com/javafx/2/charts/img/pie-sample.png" alt="Roulette Wheel Selection"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  mating season 🎉
&lt;/h2&gt;

&lt;p&gt;our animals do their thing and produce offspring's this action is called &lt;code&gt;Crossover&lt;/code&gt; in academic books before we produce &lt;code&gt;Generation2 or g2&lt;/code&gt; we have another  action that is happening while our animals do &lt;code&gt;Crossover&lt;/code&gt; its called &lt;code&gt;Mutation&lt;/code&gt; just like natural beings our baby animals are subjected mutation in our case maybe in each 1,000 baby we will change DNA of 3 of them at random by 1 bit also randomly this may increase the chance of getting closer to our ideal animal after that we say that we generated &lt;code&gt;second generation g2&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Repeat
&lt;/h2&gt;

&lt;p&gt;What we did to generate g2 from g1 we repeat that process many times till we get closer and closer to our solution but when to stop is also our decision we may say that if you find an animal that is 85% like our ideal animal we stop and pick this as a solution.&lt;/p&gt;
&lt;h1&gt;
  
  
  Key points
&lt;/h1&gt;

&lt;p&gt;1- Very good at approximating solutions that are hard to program or expensive to calculate&lt;/p&gt;

&lt;p&gt;2- For very complex problems they may require many generations to produce a good approximation&lt;/p&gt;

&lt;p&gt;3- Genetic algorithms weak point is that when you have time bounded variables in your logic which means our data may change according to time&lt;/p&gt;
&lt;h1&gt;
  
  
  Actual example
&lt;/h1&gt;

&lt;p&gt;This is a demo what if we wanted to approximate a cat picture &lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/Q-CDOHkQWBw"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  Final words
&lt;/h1&gt;

&lt;p&gt;This was a humble explanation for a very interesting topic if you find any inconsistency or mistake please correct me thanks. &lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>tutorial</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>Send automated reports using Nodejs</title>
      <dc:creator>aram</dc:creator>
      <pubDate>Tue, 01 Oct 2019 06:40:35 +0000</pubDate>
      <link>https://dev.to/aramrafeq/send-automated-reports-using-nodejs-104e</link>
      <guid>https://dev.to/aramrafeq/send-automated-reports-using-nodejs-104e</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this article we will explain how you can send reports automatically and periodically using a NodeJs  whether you embed the logic inside your application or you make it a separate script its up to the developer but separate script is recommended(we use this approach), before you start you should know what is NodeJs you can benefit from this &lt;a href="https://www.vultr.com/docs/a-quick-guide-to-node-js-in-2019" rel="noopener noreferrer"&gt;A Quick Guide to Node.js in 2019&lt;/a&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Requirements
&lt;/h2&gt;

&lt;p&gt;Two packages is required for this tutorial &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;node-cron&lt;/code&gt; used for scheduling tasks&lt;/li&gt;
&lt;li&gt; &lt;code&gt;node-mailer&lt;/code&gt; used to send emails to some user list using some SMTP providers googles Gmail will be used for this purpose as it is free and easy to setup&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Project setup
&lt;/h2&gt;

&lt;p&gt;Start by creating new NodeJs project&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;create a folder &amp;amp; cd to the folder using command line something like &lt;code&gt;exampleFolder&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;run &lt;code&gt;npm init&lt;/code&gt; this will ask you couple of questions after you finish there will be a file called &lt;code&gt;package.json&lt;/code&gt; alternatively if you don't want to answer questions type &lt;code&gt;npm init -y&lt;/code&gt; this will answer yes for all question, &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;install &lt;code&gt;node-cron&lt;/code&gt; type &lt;code&gt;npm i node-cron --save&lt;/code&gt; in the console&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;install &lt;code&gt;nodemailer&lt;/code&gt; type &lt;code&gt;npm i nodemailer --save&lt;/code&gt; in the console&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After the installation you should see a folder called &lt;code&gt;node_modules&lt;/code&gt; and generated &lt;code&gt;package.json&lt;/code&gt; should look something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
       &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"exampleFolder"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
       &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
       &lt;/span&gt;&lt;span class="nl"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
       &lt;/span&gt;&lt;span class="nl"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"index.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
       &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="nl"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"echo &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;Error: no test specified&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt; &amp;amp;&amp;amp; exit 1"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"keywords"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"author"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"license"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ISC"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"dependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
           &lt;/span&gt;&lt;span class="nl"&gt;"node-cron"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^2.0.3"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
           &lt;/span&gt;&lt;span class="nl"&gt;"nodemailer"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^6.3.0"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Let's start coding
&lt;/h2&gt;

&lt;p&gt;We need an entry point for our application(script) start by creating a file called &lt;code&gt;main.js&lt;/code&gt; and add &lt;code&gt;"start": "node main.js"&lt;/code&gt; line to &lt;code&gt;package.json&lt;/code&gt; under &lt;code&gt;"scripts"&lt;/code&gt; property so it becomes like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
       &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"exampleFolder"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
       &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
       &lt;/span&gt;&lt;span class="nl"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
       &lt;/span&gt;&lt;span class="nl"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"index.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
       &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="nl"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"echo &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;Error: no test specified&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt; &amp;amp;&amp;amp; exit 1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="nl"&gt;"start"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"node main.js"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"keywords"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"author"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"license"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ISC"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"dependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
           &lt;/span&gt;&lt;span class="nl"&gt;"node-cron"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^2.0.3"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
           &lt;/span&gt;&lt;span class="nl"&gt;"nodemailer"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^6.3.0"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add &lt;code&gt;console.log("Hello world")&lt;/code&gt; inside &lt;code&gt;main.js&lt;/code&gt; then in the console type &lt;code&gt;npm start&lt;/code&gt; this should output &lt;code&gt;Hello world&lt;/code&gt; to the console this is used to make sure our application runs&lt;/p&gt;

&lt;h3&gt;
  
  
  Configure nodemailer
&lt;/h3&gt;

&lt;p&gt;We will start by configuring nodemailer follow these steps &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;create a file called &lt;code&gt;reportSender.js&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;paste this script inside the file
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;        &lt;span class="kd"&gt;const&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;defaultMailingList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;example1@vultr.com,example2@vultr.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;senderEmail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sender.example@gmail.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;senderPassword&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;gmail_app_password&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// gmail app password&lt;/span&gt;
        &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;sendMail&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;defaultMailingList&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;transporter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nodemailer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createTransport&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
                    &lt;span class="na"&gt;service&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Gmail&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="na"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;senderEmail&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="na"&gt;pass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;senderPassword&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="p"&gt;},&lt;/span&gt;
                &lt;span class="p"&gt;});&lt;/span&gt;

                &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="na"&gt;from&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`report sender &amp;lt;&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;senderEmail&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;gt;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="nx"&gt;to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="nx"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="na"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="na"&gt;html&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="p"&gt;};&lt;/span&gt;

                &lt;span class="nx"&gt;transporter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sendMail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{});&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="c1"&gt;// handle errors here&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that nodemailer provides handful of transports including custom ones &lt;/p&gt;

&lt;p&gt;Values are hardcoded i would recommend using something like &lt;code&gt;dotenv&lt;/code&gt; which loads &lt;code&gt;.env&lt;/code&gt; file and creates environment variables based on the environment variable but for sake of simplicity we did that here,&lt;/p&gt;

&lt;p&gt;What this file does is that it exports an object with key &lt;code&gt;sendMail&lt;/code&gt; which is a function that configures node mailer and setup a generic method for sending our reports as we please for future uses you can add extra keys to main object for example you want the report to be sended via telegram by a bot you can add the logic in this file &lt;/p&gt;

&lt;p&gt;you can get gmail app password following this link &lt;a href="https://support.google.com/mail/answer/185833?hl=en" rel="noopener noreferrer"&gt;get app password&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Test if node mailer can send emails for us
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;after you change these variables in a correct way [&lt;code&gt;senderEmail&lt;/code&gt;,&lt;code&gt;senderPassword&lt;/code&gt;,&lt;code&gt;defaultMailingList&lt;/code&gt;]&lt;/li&gt;
&lt;li&gt;&lt;p&gt;open &lt;code&gt;main.js&lt;/code&gt; and import &lt;code&gt;sendMail&lt;/code&gt; from &lt;code&gt;reportSender.js&lt;/code&gt; using this syntax &lt;code&gt;const {sendMail} = require('./reportSender')&lt;/code&gt; note that no file extension is required when importing&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;make a call to &lt;code&gt;sendMail&lt;/code&gt; function so it can send and email example &lt;code&gt;sendMail("Hello world", "this is email body it can contain html also")&lt;/code&gt; if you didnt update &lt;code&gt;defaultMailingList&lt;/code&gt; to contain your email you can pass the email after msg body&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;the complete body of our &lt;code&gt;main.js&lt;/code&gt; is something like this&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;sendMail&lt;/span&gt;&lt;span class="p"&gt;}&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;./reportSender&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sending email...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nf"&gt;sendMail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello world&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;this is email body it can contain html also&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;email sent ✓&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F0oqe9pok7g9x4o3kazqf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F0oqe9pok7g9x4o3kazqf.png" alt="received email"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With this we can send emails to some specific emails as you can predict that you can somehow generate a string which contains a valid html and this html is generated using data from our database then you can send reports or even data to your mailing list&lt;br&gt;
if you are wondering what is a good module for database connection i would recommend using &lt;code&gt;kenxjs&lt;/code&gt;&lt;br&gt;
the only thing remains is to automate sending reports(emails) with that comes &lt;code&gt;node-cron&lt;/code&gt; module&lt;/p&gt;
&lt;h2&gt;
  
  
  Configure node-cron
&lt;/h2&gt;

&lt;p&gt;Configuring node-cron is an easy task you tell it when do you want for some action to take place then you provide the action definition and whenever the call triggers it will call this action, ction is a function&lt;/p&gt;

&lt;p&gt;for sake of this tutorial let's say i want to receive a report every 1 min&lt;br&gt;
what you can do according to their &lt;a href="https://www.npmjs.com/package/node-cron" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; is something like this&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="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;cron&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;node-cron&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;cron&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;schedule&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;* * * * *&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;running a task every minute&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can test this code just comment everything inside &lt;code&gt;main.js&lt;/code&gt; then wait for 1 min and you should see &lt;code&gt;running a task every minute&lt;/code&gt; output every 1 min on the console as you can see the callback is our action and its the place that we want to implement and generate our reports then send the emails (generated reports)&lt;/p&gt;

&lt;h2&gt;
  
  
  Combine our nodemailer and node-cron
&lt;/h2&gt;

&lt;p&gt;As you can see by combining just two modules we could make an automated system that can send reports or even anything that we want periodically so lte's show our final &lt;code&gt;main.js&lt;/code&gt;&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;sendMail&lt;/span&gt;&lt;span class="p"&gt;}&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;./reportSender&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;cron&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;node-cron&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;cron&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;schedule&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;* * * * *&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="cm"&gt;/* 
                generate your report here then send the report using 
                any reportSender logic that you implemnted email, telegram bot,...
            */&lt;/span&gt;
            &lt;span class="nf"&gt;sendMail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`hello world &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;this is email body it can contain html also&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Final words
&lt;/h2&gt;

&lt;p&gt;The idea have alot of potential so you could get creative and make anything you want, the reason i recommended making this as a separate script is that it become another process on the CPU so simply terminate the process if you want to stop the service, in production you have your separate system this script works as an assistant and provides a nice simple feature it could be run using something like &lt;code&gt;pm2&lt;/code&gt; which is strongly recommend.&lt;/p&gt;

</description>
      <category>node</category>
      <category>devops</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
