Yesterday, I spent a little time helping a fellow CF coder with a problem he had verifying email addresses. In short, he needed to check that an email address actually exists before their system allows a user to register for their services.
It was one of those questions that made me realize I often need or want to do the same thing but never bothered to explore a solution (hello, laziness!). So, I decided to get off my proverbial ass and help him with this issue.
The result was a decent function, which I submitted to CFLib.org yesterday, but don't know if/when it will be available. I've also revised it a good bit since my submission to CFLib (if it gets published, I'll update it immediately).
Function Overview
The function takes the email address as a parameter, checks it, first, with CF's IsValid() method and then calls a Web Service (ValidateEmail) at www.webservicex.net to verify the email.
Once complete, a structure is returned with the following keys:
CFSCRIPT Function
The function tested successfully on CF7 or higher and Railo 3 and higher.
You may download the script in a Zip archive from:
www.baddogsconsulting.com/projects/emailverifier/verifyEmail.cfm.zip.
I also have a CFC version of this function as well. If you're interested, let me know and I can add it to this post or send to you.
It was one of those questions that made me realize I often need or want to do the same thing but never bothered to explore a solution (hello, laziness!). So, I decided to get off my proverbial ass and help him with this issue.
The result was a decent function, which I submitted to CFLib.org yesterday, but don't know if/when it will be available. I've also revised it a good bit since my submission to CFLib (if it gets published, I'll update it immediately).
Function Overview
The function takes the email address as a parameter, checks it, first, with CF's IsValid() method and then calls a Web Service (ValidateEmail) at www.webservicex.net to verify the email.
Once complete, a structure is returned with the following keys:
- emailStatus -- boolean value indicating whether or not the email passed validation and verification
- message -- string containing a user-friendly note about theprocess
- resultCode -- string set to valid (all good), invalid (fails either validation or verification), or fail (Web Service fails)
- wsdl -- string with the Web Service used
- attemptTime -- date set to the moment when the Web Service call was made
CFSCRIPT Function
/**
* Verifies the existence of an email address by checking the provided email address against the ValidateEmail web service.
*
* @param emailaddress string containing the email address to test
* @return struct (emailResult: true/false; message: String; resultCode:String; wsdl:String; attemptTime:Date )
* @author Craig Kaminsky (www.baddogsconsulting.com)
* @version 1, May 7, 2009
*/
function verifyEmail( emailaddress )
{
// setup local vars
var email_address = Trim( emailaddress );
var results = StructNew();
var validationResponse = "";
var ws = "";
// add the default keys and values for returned struct
results.wsdl = "http://www.webservicex.net/ValidateEmail.asmx?wsdl";
results.attemptTime = Now();
// setup the web service
ws = createObject( "webservice", results.wsdl );
// before we proceed to call the web service, let's make sure it's a properly formatted email
if( IsValid( "email", email_address ))
{
// setup some exception handling just in case the Web Service is down, etc.
try
{
validationResponse = ws.IsValidEmail( Email=email_address );
}
catch( Any err )
{
results.emailResult = false;
results.message = "Web Service response error: " + err.Message;
results.resultCode = "fail";
// exit the function and return the results struct
return results;
}
// check the response from the web service
if( Trim( validationResponse ) is "YES" )
{
results.emailResult = true;
results.message = "Email address passed validation and verification.";
results.resultCode = "valid";
}
else
{
results.emailResult = false;
results.message = "Email address passed validation but failed verification.";
results.resultCode = "invalid";
}
}
else
{
results.emailResult = false;
results.message = "Email address failed validation. It is not properly formatted.";
results.resultCode = "invalid";
}
// and return the struct
return results;
}
The function tested successfully on CF7 or higher and Railo 3 and higher.
You may download the script in a Zip archive from:
www.baddogsconsulting.com/projects/emailverifier/verifyEmail.cfm.zip.
I also have a CFC version of this function as well. If you're interested, let me know and I can add it to this post or send to you.
http://www.kellermansoftware.com/p-37-net-email-validation.aspx
I'll try a couple of hotmail emails today, debugging the process, and see if I come up with some errors, too. Thanks!
http://s3.amazonaws.com/oss-projects/email.verification.cfml.zip
I supposed its a problem with the web service rather than your script.
Thanks for the tool.
If you want, please send me an email (imageaid at gmail dot com) and I can send you the zip archive now (rather than waiting for me to upload).
http://www.baddogsconsulting.com/projects/email.verification.cfml.zip
Thanks!
Craig
Thanks again for a great bit of code!
In the meantime, I implemented a basic Ajax interface for using this function (in its CFC version). You can try it at:
http://www.imageaid.net/projects/emailverifier/index.html
I also added a Zip archive of the files used so that the code can be download, edited, etc. The download link is at the bottom of the above page.
This would work really well as an ajax thing, so while a user is filling out the next text field (postal address or whatever) your function can be called to check the email address (which takes 2 to 10 secs) and then alert the user if there's a problem.
Good find, and good new CF lib. Thanks.