Copy link to clipboard
Copied
Can anyone point me in the direction to some code I can use to verify an email address?
I have seen so many examples that use php and .net which do not seem to work on a windows hosted server.
We have a small site that requires a valid email address to enter. Instead of letting them enter anything, we want to verify first.
TIA
Terry
Hi, Terry,
Go ahead and try this one. It's probably easier to use a straight function than a CFC!
<cfscript>
/**
* 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 boolean
*/
function verifyEmail( emailaddress )
{
var emailStatus = false;
var validationStatus = "";
var ws = createObject( "w
...Copy link to clipboard
Copied
This function should do the trick for you:
<cffunction name="runEmailCheck" displayname="Run Email Check" access="remote" output="false" returntype="boolean">
<cfargument name="emailaddress" displayname="Email Address" type="string" required="true" />
<cfset var emailStatus = false />
<cfinvoke webservice="http://www.webservicex.net/ValidateEmail.asmx?wsdl" method="IsValidEmail" returnvariable="status">
<cfinvokeargument name="Email" value="#Trim(arguments.emailaddress)#">
</cfinvoke>
<cfif Trim( status ) is "YES">
<cfset emailStatus = true />
</cfif>
<cfreturn emailStatus />
</cffunction>
It uses the ValidateEmail web service at WebServiceX.net to check if an email address exists. I tested with several emails, mixing in private domains (mysite.com) with public email services (Gmail, Yahoo, etc.) and, in all cases, I got the correct response back.
I initially placed this function in a CFC called Email Checker and called it from the following script to test:
<cfsilent>
<cfscript>
emailChecker = createObject( "component", "path.to.my.cfc.EmailChecker" );
</cfscript>
</cfsilent>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Email Checker Test Page</title>
</head>
<body>
<p><cfoutput>#emailChecker.runEmailCheck( 'someemail@email.com' )#</cfoutput></p>
</body>
</html>
Hope that helps!
Copy link to clipboard
Copied
Thank you very much.
Question: what vars do I need to change and how can I pass an email address for verification?
TIA
Terry
Copy link to clipboard
Copied
Hi, Terry,
Go ahead and try this one. It's probably easier to use a straight function than a CFC!
<cfscript>
/**
* 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 boolean
*/
function verifyEmail( emailaddress )
{
var emailStatus = false;
var validationStatus = "";
var ws = createObject( "webservice", "http://www.webservicex.net/ValidateEmail.asmx?wsdl" );
validationStatus = ws.IsValidEmail( Email=#Trim(emailaddress)# );
if( Trim( validationStatus ) is "YES" )
{
emailStatus = true;
}
return emailStatus;
}
</cfscript>
You can just throw this one in your CFML page or any other way to include in your application that you want (<cfinclude template=""/>).
In your CFML page, you can just call the function (so long as you included the function in your code):
This is good to test (just see if it's working)
<cfouput>#verifyEmail( YOUR_EMAIL_VARIABLE )#</cfoutput>
If the function above is included, you could check for a good or bad email and then act on it:
<cfscript>
emailOk = verifyEmail( YOUR_EMAIL_VARIABLE );
</cfscript>
<cfif emailOk>
<!--- email is good, do your stuff --->
<cfelse>
<!--- email is bad, do your stuff --->
</cfif>
Let me know if you have any trouble with it!
Copy link to clipboard
Copied
First I appreciate your help; very much. My lack of CF knowledge shows
All code you provided, I put in testmail.cfm
I put the cfscript code within the HEAD tags and the rest in the body.
I get this error: Invalid token @ found on line............etc.... My guess is it is referring to the cfoutput that contains my email address.
<cfoutput>#verifyEmail( tmergl@mydomain.net )#</cfoutput>
I know I'm missing something here, sorry.
TIA
Terry
NOTE: I think the '' was missing. I got a TRUE response YEAHHHH..... Next question. Can I use this code with a simple form?
Enter email address click here to verify? I guess I could just insert #FORM.email# where my email address is now correct?
Copy link to clipboard
Copied
Thank you very much. It is working and returning true or false.
I assume that I can have a page ie: testmail.cfm where the user enters their email address for verification.
I can send it to verify.cfm to get a true or false reply.
Question: how would you suggest sending them to a page based on the results?
If the response is true: goto register.cfm if is false, return them to testmail.cfm?
Does that make sense? What I'm shooting for is this: user enters valid email address. If TRUE then send user to registration page.
IF False, make them enter it again.
TIA
Terry
PS: You just helped me in minutes, what I have been googling for days.
Copy link to clipboard
Copied
Hi, Terry,
I'm very glad to know this was able to help you. Thanks!
I've attached four files for you -- rename all from .txt to .cfm before trying to run them! It's a very, very basic skeleton, one that could take myriad forms, of how this process could play out in an application.
To test it out, place all three files in a directory (keep them together) on your development web server. Then, just open email_form.txt (.cfm) in your browser and try it out. I gave it a quick test on both CF 8 and Railo 3.1, but you should be good to go with CF 6 and 7 as well (not sure what version your using).
Hopefully it'll help get you rolling!
Cheers,
Craig
Copy link to clipboard
Copied
Thanks Craig.
Going to try and test that out today.
I appreciate it very much and will let you know how it goes.
TIA
Terry
Copy link to clipboard
Copied
You are welcome! Do let me know if you hit a snag with it (I have a chill end to my week for once ).