PDA

View Full Version : Cfmail address validation



jakeh
December 7th, 2009, 06:05 PM
If your application tries to send mail through cfmail but the email address is invalid, you may receive the following error: "Attribute validation error for tag CFMAIL." Below are some options to help prevent this issue.

Use ColdFusion's isValid() method to check the email address's format. Below is an example for checking an email address. Remember to replace #form.emailAddr# with your actual form's email field. Also, remember to replace "yourform.cfm" to the name of your form.


<cfif isDefined("form.submitEmail")>
<cfif isValid("email", form.emailAddr)>
<cfoutput>
<cfmail to="#form.emailAddr#" from="[email protected]"
subject="This is a test email">
<h3>The email address is valid and mail has been sent.</h3>
</cfoutput>
<cfelse>
<H3>You must supply a email address.</H2>
</cfif>
<cfelse>
</cfif>

<cfform action="yourform.cfm">
email: <cfinput type="Text" name="emailAddr"><br>
<cfinput type="submit" name="submitEmail" value="Send Email"><br>
</cfform>

Use cftry/cfcatch blocks around the cfmail code. This will allow the application to handle any cfmail errors gracefully. Remember to replace #form.mailto# with your actual form's email field.


<!--- Begin the cftry block. --->
<cftry>
<!--- Send the test email. --->
<cfmail to="#form.emailAddr#" from="[email protected]"
subject="This is a test email">
This is a test email.
</cfmail>
<cfcatch>
<!--- Insert your own code to handle the error.
This can be as simple as setting a variable or can be more robust.--->
</cfcatch>
<!--- Close the cftry block. --->
</cftry>


For more information on this please visit the following documentation pages on Adobe's site:

isValid (http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7fb9.html)
cftry (http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7ec6.html)
cfcatch (http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7ec5.html)
cfmail (http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7f8e.html)