Skip to content
Browse files

Add email validation

  • Loading branch information...
1 parent e3d9f29 commit 1a5ce3d7fdba4eb939bd572e87fff574712368b6 @BerkeleyTrue BerkeleyTrue committed
Showing with 40 additions and 13 deletions.
  1. +3 −1 package.json
  2. +37 −12 src/index.js
View
4 package.json
@@ -14,10 +14,12 @@
"license": "BSD-3-Clause",
"dependencies": {
"dotenv": "^1.2.0",
+ "lodash": "^3.10.1",
"nodemailer": "^1.8.0",
"nodemailer-ses-transport": "^1.3.0",
"nodemailer-smtp-pool": "^1.1.3",
- "rx": "^4.0.6"
+ "rx": "^4.0.6",
+ "validator": "^4.2.0"
},
"devDependencies": {
"babel": "^5.8.23",
View
49 src/index.js
@@ -1,5 +1,7 @@
/* eslint-disable no-process-exit */
-import { Observable } from 'rx';
+import _ from 'lodash';
+import { Observable, Scheduler } from 'rx';
+import { isEmail } from 'validator';
import nodemailer from 'nodemailer';
import ses from 'nodemailer-ses-transport';
import dotenv from 'dotenv';
@@ -16,30 +18,53 @@ const options = {
const mailOptions = {
from: 'Quincy <team@freecodecamp.com>',
- subject: data.subject,
- text: data.text
+ subject: data.subject
};
+const createText = _.template(data.text);
const transporter = nodemailer.createTransport(ses(options));
const send$ = Observable.fromNodeCallback(transporter.sendMail, transporter);
+const emailLength = emails.length;
+let counter = 0;
-Observable.from(emails)
- // batch every 10000
- .bufferWithCount(10000)
- // wait 1 sec between batches
- .delay(1000)
- .flatMap(emails => {
+const startTime = Date.now();
+let endTime;
+let lastEmail;
+function getPercent(val) {
+ const percent = (val / emailLength) * 100;
+ return Math.round(percent * 100) / 100;
+}
+
+Observable.from(emails, null, null, Scheduler.default)
+ .filter(email => isEmail(email))
+ .flatMap(email => {
const filledOptions = Object.assign(
{},
mailOptions,
- { to: emails.join(', ').replace(/,$/, '') }
+ {
+ to: email,
+ text: createText({ email })
+ }
);
return send$(filledOptions);
})
+ .doOnNext(email => {
+ lastEmail = email;
+ counter += 1;
+ console.log('%d percent done', getPercent(counter));
+ })
.count()
+ .doOnNext(() => endTime = Date.now())
.subscribe(
- count => console.log('sent %d so far', count),
- err => { throw err; },
+ count => console.log(
+ 'sent %d emails in %d seconds',
+ count,
+ endTime - startTime
+ ),
+ err => {
+ console.log('err on last email %s', lastEmail);
+ throw err;
+ },
() => {
console.log('process complete');
process.exit(0);

0 comments on commit 1a5ce3d

Please sign in to comment.
Something went wrong with that request. Please try again.