Skip to main content
Inspiring
May 19, 2008
Answered

validating email

  • May 19, 2008
  • 2 replies
  • 265 views
Hello, I have a program that loops over a comma delimited list of email addresses to send out our newsletters. I am running into the problem with CF MX that if it reaches a bad email it will crash the program and throw an error.

Does anyone know how I could go about validating the email accounts, or weed out the bad ones before I run the code to send out? Here is how I send out the mail:

<cffile action="read"
file="#ExpandPath('contacts.txt')#"
variable="contacts">

<!---loop over the list and send an email to each address--->
<cfloop list="#contacts#" index="idx" delimiters=",">

<cfmail to="#idx#"
from="#Form.MessageFrom#"
subject="#FORM.messageSubject#" type="html">


#form.message#

</cfmail>
Sending to... <cfoutput>"<strong>#idx#</strong>"<br>

</cfoutput>
</cfloop>
This topic has been closed for replies.
Correct answer brianism
Thanks Dan. I did some research and I came up with some code that fixed what I needed to do. Just wanted to share what is working.

<cffile action="read"
file="#ExpandPath('briantest.txt')#"
variable="contacts">




<!--- Create a table to output the results. --->
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<strong> Email</strong>
</td>
<td width="100">
<strong> Valid?</strong>
</td>
<td width="100">
<strong> Sent?</strong>
</td>
</tr>

<!--- Loop over the emails and validate them. --->
<cfloop list="#contacts#" index="idx" delimiters=",">

<!--- Try sending out email. --->
<cftry>

<!--- Send mail. --->
<cfmail
to="#idx#"
from="#Form.MessageFrom#"
subject="#FORM.messageSubject#" type="html">

#form.message#
</cfmail>

<!--- Set success flag. --->
<cfset blnEmailSuccess = true />

<!--- Catch email errors. --->
<cfcatch>

<!--- Email failed. Set success flag. --->
<cfset blnEmailSuccess = false />

</cfcatch>

</cftry>

<cfoutput>
<!--- Check for validity. --->
<cfset blnValid = IsValid( "email", #idx# ) />

<tr>
<td>
#idx#
</td>
<td>
#YesNoFormat( blnValid )#
</td>
<td>
#YesNoFormat( blnEmailSuccess )#
</td>
</tr>
</cfoutput>
</cfloop>
</table>

2 replies

brianismAuthorCorrect answer
Inspiring
May 20, 2008
Thanks Dan. I did some research and I came up with some code that fixed what I needed to do. Just wanted to share what is working.

<cffile action="read"
file="#ExpandPath('briantest.txt')#"
variable="contacts">




<!--- Create a table to output the results. --->
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<strong> Email</strong>
</td>
<td width="100">
<strong> Valid?</strong>
</td>
<td width="100">
<strong> Sent?</strong>
</td>
</tr>

<!--- Loop over the emails and validate them. --->
<cfloop list="#contacts#" index="idx" delimiters=",">

<!--- Try sending out email. --->
<cftry>

<!--- Send mail. --->
<cfmail
to="#idx#"
from="#Form.MessageFrom#"
subject="#FORM.messageSubject#" type="html">

#form.message#
</cfmail>

<!--- Set success flag. --->
<cfset blnEmailSuccess = true />

<!--- Catch email errors. --->
<cfcatch>

<!--- Email failed. Set success flag. --->
<cfset blnEmailSuccess = false />

</cfcatch>

</cftry>

<cfoutput>
<!--- Check for validity. --->
<cfset blnValid = IsValid( "email", #idx# ) />

<tr>
<td>
#idx#
</td>
<td>
#YesNoFormat( blnValid )#
</td>
<td>
#YesNoFormat( blnEmailSuccess )#
</td>
</tr>
</cfoutput>
</cfloop>
</table>
Inspiring
May 20, 2008
Use the validate function. Details are in the cfml reference manual. If you don't have one, the internet does.