Skip to content

ayeniblessing101/home_sessions_day_2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

home_sessions_day_2

Project Details
Language: JavaScript
TDD tool: Jasmine
Author: Ayeni Blessing
Version: 0.0.1

Contains:


Car Class, test spec and program
Reverse String function, test spec and program
CLI Application to send emails

CLI Application

Description

A CLI Application called 'Dispatch' that reads a csv file, accepts the sender's details and send Email to
all email addresses in the csv file using SendGrid API


Required Modules

  • Commander.js
  • CSV
  • Inquirer.js
  • Chalk.js
  • Async
  • SendGrid

dispatch.js

#!/usr/bin/env node const program = require('commander'); const csv = require('csv'); const fs = require('fs'); const chalk = require('chalk'); const inquirer = require('inquirer'); const async = require('async');

program .version('0.0.1') .option('-l, --list [list]', 'List of customers in CSV') .parse(process.argv)

let questions = [ { type : "input", name : "sender.email", message : "Sender's email address - " }, { type : "input", name : "sender.name", message : "Sender's name - " }, { type: "input", name : "subject", message : "Subject - " }

];

let contactList = []; let parse = csv.parse; let stream = fs.createReadStream(program.list) .pipe(parse({ delimiter : ',' }));

let __sendEmail = function (to, from, subject, callback) { let template = "Wishing you a Merry Christmas and a " + "prosperous year ahead. P.S. Toby, I hate you."; let helper = require('sendgrid').mail; let fromEmail = new helper.Email(from.email, from.name); let toEmail = new helper.Email(to.email, to.name); let body = new helper.Content("text/plain", template); let mail = new helper.Mail(fromEmail, subject, toEmail, body);

let sg = require('sendgrid')(process.env.SENDGRID_API_KEY); let request = sg.emptyRequest({ method: 'POST', path: '/v3/mail/send', body: mail.toJSON(), });

sg.API(request, function(error, response) { if (error) { return callback(error); } callback(); }); };

stream .on("error", function(err){ return console.error(err.message); }) .on('data', function (data) { let name = data[0] + " " + data[1]; let email = data[2]; contactList.push({name: name, email :email}); }) .on("end", function(){ inquirer.prompt(questions).then(function (ans){ async.each(contactList, function (recipient, fn){ __sendEmail(recipient, ans.sender, ans.subject, fn); }, function (err) { if (err) { return console.error(chalk.red(err.message)); } console.log(chalk.green('Success')); }); });

});

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published