Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 418
  • Last Modified:

JavaScript to validate e-mails

Hello,

I´m writing a javascript in order to deny sending e-mails, from non local e-mail addresses. My e-mail server store the e-mail addresses and other information in text files, I need a javascript to check if the e-mail address used in the from is included in the file where my e-mail server store all the information, at moment I´ve this but is not working, I need to read line by line the mailserver file, but I don´t know how to do that. May someone point me in the right direction? Below I paste what I have so far.

// filter.js
var content = WScript.Arguments(0) ;
var envelope = content.substr(0,content.length-7) + "envelope.new" ;
var fs = WScript.CreateObject( "Scripting.FileSystemObject" ) ;
var ts = fs.OpenTextFile( envelope , 1 , false ) ;
var e = ts.ReadAll() ;
ts.Close() ;
var re = new RegExp( "X-MailRelay-From: \(.*\)" ) ;
var email = (e.match(re)[1]).replace(/[\n\r]/g, '') ;

if( email != "abuse@localhost.tld" ) // wrong
{
	fs.DeleteFile( content ) ;
	fs.DeleteFile( envelope ) ;
	WScript.Quit( 100 )
}
WScript.Quit( 0 ) ;

Open in new window


***

Each line on the file containing all the domains in my e-mail server look like this:
email@domain.tld	1	password	domain.tld	USER	
email1@domain.tld	1	password	domain.tld	USER

Open in new window


***

Thanks
Martín
0
mzanlongo
Asked:
mzanlongo
  • 2
1 Solution
 
Michel PlungjanIT ExpertCommented:
Would it not be better to loop and use readLine instead of readAll?
Also seeing you are using the filesystem object, it looks like you are on intranet... In the real world I would not expose all email addresses and users like that
0
 
ahoffmannCommented:
do I understand correctly that you use server-side javascript?
0
 
mzanlongoAuthor Commented:
Finally I´ve opened the domain.tab file into a variable and used the fn() indexOf in order to know if the string is present within the variable, below I paste the solution.

var content = WScript.Arguments(0) ;
var envelope = content.substr(0,content.length-7) + "envelope.new" ;
var fs = WScript.CreateObject( "Scripting.FileSystemObject" ) ;
var ts = fs.OpenTextFile( envelope , 1 , false ) ;
var e = ts.ReadAll() ;
ts.Close() ;
var re = new RegExp( "X-MailRelay-From: \(.*\)" ) ;
var email = (e.match(re)[1]).replace(/[\n\r]/g, '') ;
var domain = email.split("@") ;
var dom_file = fs.OpenTextFile( "C:\\Program Files (x86)\\Parallels\\Plesk\\Mail Servers\\Mail Enable\\Config\\domain.tab" , 1 , false ) ;
var dom_list = dom_file.ReadAll();
var exist = dom_list.indexOf(domain[1]);

if( exist == -1 )
{
      fs.DeleteFile( content ) ;
      fs.DeleteFile( envelope ) ;
      WScript.Quit( 100 )
}
WScript.Quit( 0 ) ;
0
 
mzanlongoAuthor Commented:
This solution cover the problem I´ve described initially.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now