Skip to main content
Known Participant
May 6, 2009
Answered

MX, DNS, SMTP - Email Verify

  • May 6, 2009
  • 1 reply
  • 1910 views

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

    This topic has been closed for replies.
    Correct answer craigkaminsky

    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!

    1 reply

    Inspiring
    May 6, 2009

    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!

    Known Participant
    May 6, 2009

    Thank you very much.


    Question: what vars do I need to change and how can I pass an email address for verification?

    TIA
    Terry

    craigkaminskyCorrect answer
    Inspiring
    May 6, 2009

    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!