Tag: validate email

Validate email address

Posted by on July 18, 2010

Once in a while you’ll be forced to validate an email address (or many!) — The usual no-brainer method is to perform a quick syntactical test to ensure the address is not malformed, however even a properly formed address may be invalid or, unreachable.

Clearly we must add a second validation step… — Hey I know, let’s check the MX records!

Procedure.s Email_GetHost( eMail.s )
	If eMail
		ProcedureReturn Right( eMail, Len(eMail) - FindString(eMail, "@", 1) )
	EndIf
EndProcedure
 
Procedure.i Email_ValidateAddress( eMail.s, CompleteValidation.i = #True )
 
	Define.s pattern = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$"
	Define.i is_valid = #False
 
	Define.i handle = CreateRegularExpression( #PB_Any, pattern )
	If IsRegularExpression( handle )
 
		If MatchRegularExpression( handle, eMail )
			If CompleteValidation
 
				Dim mx.s(99)
				If getmxrr( Email_GetHost(eMail), mx() ) ; see http://gushh.net/blog/2010/07/17/get-mx-records/
					is_valid = #True
				EndIf
 
			Else
				is_valid = #True
			EndIf
		Else
				is_valid = -1
		EndIf
 
		FreeRegularExpression( handle )
	EndIf
 
	ProcedureReturn is_valid
 
EndProcedure

For the getmxrr() function click here. Or click here for the full code + example usage

The full code also contains a few other helper routines, I left them in there in case someone finds them useful.

These are the functions included in the source:

Email_GetName()
Email_GetHost()
Email_ValidateAddress()
Email_ConvertToLiteral()

Obviously you’ll have to tweak a bit the code, specially the way it handles the arrays. But overall it should be pretty usable. I might come up with a cross-platform version in the future, but so far I haven’t found a need for it.

Please double-check the example code, make sure you understand the routine could return <= 0 if validation fails (two modes basically, syntax fault and mx fault).

Feel free to leave a comment or send me your own version of the code!

Cheers!

Get MX Records

Posted by on July 17, 2010

This routine will populate an array with the MX records of a given domain name. It’s similar in functionality to getmxrr(); from PHP.

Procedure.i getmxrr( HostName.s, Array MXHosts.s(1) )
 
	Define.s buffer			= ""
	Define.s find_str 		= "mail exchanger = "
	Define.i count			= 0
	Define.i mail_exchanger		= 0
	Define.i find_strlen		= Len(find_str)
	Define.i handle 		= RunProgram( "nslookup", "-type=mx " + HostName, "",  #PB_Program_Open | #PB_Program_Read | #PB_Program_Hide )
 
	If IsProgram( handle )
		While ProgramRunning( handle )
 
			buffer 		= ReadProgramString( handle )
			mail_exchanger 	= FindString( buffer, find_str, 1 )
 
			If mail_exchanger
 
				buffer = Mid( buffer, mail_exchanger + find_strlen, Len(buffer) )
 
				If buffer
					MXHosts(count) = buffer
					count + 1
				EndIf
 
			EndIf
 
		Wend
		CloseProgram( handle )
	EndIf
 
	ProcedureReturn count
 
EndProcedure

This version is Windows only since it’s implemented on top of nslookup. Quite frankly I did not have a need to support other platforms at the time. But hopefully it’ll be useful for someone. For instance if you’re looking to validate an email address, this is the second step after the syntactical validation. (Usually a regular expression is utilized for the first step)

Usage example:

Define.i i	; iteration in case we get a result
Dim mx.s(99)	; this array will become populated with mx records
Define.i result = getmxrr( "google.com", mx() )
 
If result
 
	For i=0 To result - 1
		Debug mx(i)
	Next
 
Else
	Debug "no mx records found"
EndIf

In my case I got the following results:

google.com.s9a2.psmtp.com
google.com.s9b1.psmtp.com
google.com.s9a1.psmtp.com
google.com.s9b2.psmtp.com

Cheers!