Icontem

File: testEmailValidation.js

Recommend this page to a friend!
  Classes of Manuel Lemos  >  E-mail validation  >  testEmailValidation.js  >  Download  
File: testEmailValidation.js
Role: Example script
Content type: text/plain
Description: Script to test the e-mail validation object from the command line
Class: E-mail validation
Validate e-mail addresses using NodeJS
Author: By
Last change: Added support to set the SMTP server socket timeout.
Added support to whitelist email domains.
Added support to validate email users, domains and servers against black
lists.
Date: 4 years ago
Size: 2,004 bytes
 

Contents

Class file image Download
/*jslint node: true, sloppy: true, white: true */

/*
 * testEmailValidation.php
 *
 * @(#) $Id: testEmailValidation.js,v 1.4 2014/04/05 12:10:27 mlemos Exp $
 *
 */

var email, validation, emailValidation;

if(process.argv.length < 3)
{
	console.log('It was not specified the e-mail address for validation.');
}
else
{
	/*
	 * Load the e-mail validation module
	 */
	emailValidation = require('./emailValidation');

	/*
	 * Configure the path of the sockets module
	 */
	emailValidation.socketsModule = './sockets';

	var validation = new emailValidation.validation();
	
	/*
	 * E-mail address of local user to simulate e-mail delivery
	 */
	validation.localAddress = 'localuser@localhost';

	/*
	 * Output debug information
	 */
	validation.debug = true;

	/*
	 * Output debug information about network socket communication
	 */
	validation.debugSockets = false;

	/*
	 * Function to output debug information
	 */
	validation.debugOutput = console.log;

	/*
	 * Timeout for network socket communication in seconds
	 */
	validation.timeout = 15;

	var email = process.argv[2];
	validation.emailDomainsWhitelistFile = 'emaildomainswhitelist.csv';
	validation.invalidEmailUsersFile = 'invalidemailusers.csv';
	validation.invalidEmailDomainsFile = 'invalidemaildomains.csv';
	validation.invalidEmailServersFile = 'invalidemailservers.csv';
	validation.validate(email, function (result)
	{
		if(result.valid === undefined)
		{
			console.log('Error: ' + result.error);
		}
		else
		{
			if(result.valid === null)
			{
				console.log('It was not possible to determine whether the address ' + email + ' is valid' + (result.error ? ': ' + result.error : '.'));
			}
			else
			{
				console.log('The address ' + email + ' is ' + (result.valid ? 'valid' : 'invalid') + '.');
				if(!result.valid && result.status === validation.EMAIL_VALIDATION_STATUS_TYPO_IN_DOMAIN)
				{
					console.log('It may be a typing mistake. The correct email address may be ' + result.suggestions[0] + ' .');
				}
			}
		}
	});
}