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:

  1. emailStatus -- boolean value indicating whether or not the email passed validation and verification
  2. message -- string containing a user-friendly note about theprocess
  3. resultCode -- string set to valid (all good), invalid (fails either validation or verification), or fail (Web Service fails)
  4. wsdl -- string with the Web Service used
  5. 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.

Comments

asava samuel
Here is a good .net component to verify email addresses:
http://www.kellermansoftware.com/p-37-net-email-validation.aspx
Craig Kaminsky
@Jason: if you feel like it, check out the zip archive I finally uploaded to S3. It's a bit more enhanced email verification utility.

I'll try a couple of hotmail emails today, debugging the process, and see if I come up with some errors, too. Thanks!
Craig Kaminsky
New repo for projects:
http://s3.amazonaws.com/oss-projects/email.verification.cfml.zip
Jason
hi mate, im trying with your email verification tool but somehow my hotmail address is considered invalid?

I supposed its a problem with the web service rather than your script.

Thanks for the tool.
Craig Kaminsky
@anon: yes, I will!! I had an issue with my previous host and took both of my sites off their servers since they were jacking up the prices (by almost double!).

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).
Anonymous
I am sorry, but I was interested in your ajax e-mail verification tool, and was wondering if you could make it downloadable again? Thank you for your generosity!
Craig Kaminsky
@pgpguy: sorry about that. moving sites and such around and have had little time to take care of it this summer. I uploaded a set zip file to:
http://www.baddogsconsulting.com/projects/email.verification.cfml.zip

Thanks!
Craig
pgpguy
Craig- can't get to the imageaid.net url. Is there another way to reach the cfc?

Thanks again for a great bit of code!
Craig Kaminsky
The original function I wrote, which is but a skeleton of the above is on CFlib: http://www.cflib.org/udf/verifyEmail I hope to update it shortly (not sure of the process).

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.
Craig Kaminsky
This comment has been removed by the author.
Craig Kaminsky
@gary: thanks, very much. It's funny that you mentioned an Ajax implementation. My CFC version that I mention is for just that reason! I'm setting it up as a mini-project and will be uploading it shortly. If you're interested, check back Friday (5/8) for the download link.
Gary Fenton
Cool. This web service actually asks the mail server if the mailbox exists. The others I've come across only ping the mail server which doesn't check for invalid mailbox names.

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.